Documentation ¶
Index ¶
- Constants
- Variables
- func DefaultLogFormatter(param LogFormatterParams) string
- func DisableConsoleColor()
- func ForceConsoleColor()
- func HandlerWithConfig(conf LoggerConfig, next http.Handler) http.Handler
- func HandlerWithFormatter(f LogFormatter, next http.Handler) http.Handler
- func HandlerWithFormatterAndName(routerName string, f LogFormatter, next http.Handler) http.Handler
- func HandlerWithName(routerName string, next http.Handler) http.Handler
- func HandlerWithWriter(out io.Writer, next http.Handler, notlogged ...string) http.Handler
- func Logger(next http.Handler) http.Handler
- func LoggerWithConfig(conf LoggerConfig) func(next http.Handler) http.Handler
- func LoggerWithFormatter(f LogFormatter) func(next http.Handler) http.Handler
- func LoggerWithFormatterAndName(routerName string, f LogFormatter) func(next http.Handler) http.Handler
- func LoggerWithName(routerName string) func(next http.Handler) http.Handler
- func LoggerWithWriter(out io.Writer, notlogged ...string) func(next http.Handler) http.Handler
- func RemoteIP(r *http.Request) string
- func RequestBodyLogFormatter(param LogFormatterParams) string
- func RequestHeaderLogFormatter(param LogFormatterParams) string
- func ResponseBodyLogFormatter(param LogFormatterParams) string
- func ResponseHeaderLogFormatter(param LogFormatterParams) string
- func ShortLogFormatter(param LogFormatterParams) string
- type FormatterFunction
- type LogFormatter
- type LogFormatterParams
- type LoggerConfig
- type Proxy
- type ProxyType
- type ResponseWriter
Constants ¶
const ( // ProxyGoogleAppEngine when running on Google App Engine. Trust X-Appengine-Remote-Addr // for determining the client's IP ProxyGoogleAppEngine = "X-Appengine-Remote-Addr" // ProxyCloudflare when using Cloudflare's CDN. Trust CF-Connecting-IP for determining // the client's IP ProxyCloudflare = "CF-Connecting-IP" // ProxyDefaultType represents default proxy type ProxyDefaultType = "" )
Supported proxies
Variables ¶
var DefaultErrorWriter io.Writer = os.Stderr
DefaultErrorWriter is the default io.Writer used by httplog to debug errors
var DefaultLogFormatterWithRequestHeader = ChainLogFormatter(DefaultLogFormatter, RequestHeaderLogFormatter)
DefaultLogFormatterWithHeaders is a combination of default log formatter and header log formatter
var DefaultLogFormatterWithRequestHeadersAndBody = ChainLogFormatter(DefaultLogFormatter, RequestHeaderLogFormatter, RequestBodyLogFormatter)
DefaultLogFormatterWithHeadersAndBody is a combination of default log formatter, header log formatter and json body
var DefaultLogFormatterWithResponseHeader = ChainLogFormatter(DefaultLogFormatter, ResponseHeaderLogFormatter)
DefaultLogFormatterWithHeader is a combination of default log formatter and header log formatter
var DefaultLogFormatterWithResponseHeadersAndBody = ChainLogFormatter(DefaultLogFormatter, ResponseHeaderLogFormatter, ResponseBodyLogFormatter)
DefaultLogFormatterWithHeadersAndBody is a combination of default log formatter, header log formatter and json body
var DefaultRemoteIPHeaders = []string{"X-Forwarded-For", "X-Real-IP"}
Default proxy remote IP headers
var DefaultWriter io.Writer = os.Stdout
DefaultWriter is the default io.Writer used by httplog for middleware output Logger() and Body(). To support coloring in Windows use:
import "github.com/mattn/go-colorable" httplog.DefaultWriter = colorable.NewColorableStdout()
var FullFormatterWithRequestAndResponseHeadersAndBody = ChainLogFormatter(DefaultLogFormatter, RequestHeaderLogFormatter, RequestBodyLogFormatter, ResponseHeaderLogFormatter, ResponseBodyLogFormatter)
Functions ¶
func DefaultLogFormatter ¶
func DefaultLogFormatter(param LogFormatterParams) string
DefaultLogFormatter is the default log format function Logger middleware uses.
func DisableConsoleColor ¶
func DisableConsoleColor()
DisableConsoleColor disables color output in the console.
func ForceConsoleColor ¶
func ForceConsoleColor()
ForceConsoleColor force color output in the console.
func HandlerWithConfig ¶ added in v1.3.0
func HandlerWithConfig(conf LoggerConfig, next http.Handler) http.Handler
HandlerWithConfig instance a Logger handler with config and next handler.
func HandlerWithFormatter ¶ added in v1.3.0
func HandlerWithFormatter(f LogFormatter, next http.Handler) http.Handler
HandlerWithFormatter instance a Logger handler with the specified log format function and next handler.
func HandlerWithFormatterAndName ¶ added in v1.3.0
HandlerWithFormatterAndName instance a Logger handler with the specified log format function and next handler.
func HandlerWithName ¶ added in v1.3.0
HandlerWithName instance a Logger handler with the specified prefix name and next handler.
func HandlerWithWriter ¶ added in v1.3.0
HandlerWithWriter instance a Logger handler with the specified writer buffer and next handler. Example: os.Stdout, a file opened in write mode, a socket...
func Logger ¶
Logger instances a Logger middleware that will write the logs to console. By default, gin.DefaultWriter = os.Stdout.
func LoggerWithConfig ¶
func LoggerWithConfig(conf LoggerConfig) func(next http.Handler) http.Handler
LoggerWithConfig instance a Logger middleware with config.
func LoggerWithFormatter ¶
func LoggerWithFormatter(f LogFormatter) func(next http.Handler) http.Handler
LoggerWithFormatter instance a Logger middleware with the specified log format function.
func LoggerWithFormatterAndName ¶
func LoggerWithFormatterAndName(routerName string, f LogFormatter) func(next http.Handler) http.Handler
LoggerWithFormatterAndName instance a Logger middleware with the specified log format function.
func LoggerWithName ¶
LoggerWithName instance a Logger middleware with the specified name prefix.
func LoggerWithWriter ¶
LoggerWithWriter instance a Logger middleware with the specified writer buffer. Example: os.Stdout, a file opened in write mode, a socket...
func RemoteIP ¶
RemoteIP parses the IP from Request.RemoteAddr, normalizes and returns the IP (without the port).
func RequestBodyLogFormatter ¶ added in v1.1.0
func RequestBodyLogFormatter(param LogFormatterParams) string
RequestBodyLogFormatter format function with JSON body output or text
func RequestHeaderLogFormatter ¶ added in v1.1.0
func RequestHeaderLogFormatter(param LogFormatterParams) string
HeaderLogFormatter format function with headers output.
func ResponseBodyLogFormatter ¶ added in v1.1.0
func ResponseBodyLogFormatter(param LogFormatterParams) string
ResponseBodyLogFormatter format function with JSON body output or text
func ResponseHeaderLogFormatter ¶ added in v1.1.0
func ResponseHeaderLogFormatter(param LogFormatterParams) string
ResponseHeaderLogFormatter format function with headers output.
func ShortLogFormatter ¶
func ShortLogFormatter(param LogFormatterParams) string
Types ¶
type FormatterFunction ¶
type FormatterFunction = func(param LogFormatterParams) string
func ChainLogFormatter ¶
func ChainLogFormatter(formatters ...FormatterFunction) FormatterFunction
ChainLogFormatter chain a list of log formatters
type LogFormatter ¶
type LogFormatter func(params LogFormatterParams) string
LogFormatter gives the signature of the formatter function passed to LoggerWithFormatter you can use predefined, like httplog.DefaultLogFormatter or httplog.ShortLogFormatter or you can create your custom
type LogFormatterParams ¶
type LogFormatterParams struct { Request *http.Request // Router prints router name in the log. // If you have more than one router it is useful to get one's name in a console output. RouterName string // TimeStamp shows the time after the server returns a response. TimeStamp time.Time // StatusCode is HTTP response code. StatusCode int // Latency is how much time the server cost to process a certain request. Latency time.Duration // ClientIP calculated real IP of requester, see Proxy for details. ClientIP string // Method is the HTTP method given to the request. Method string // Path is a path the client requests. Path string // BodySize is the size of the Response Body BodySize int // Body is a body content, if body is copied Body []byte // Response header ResponseHeader http.Header // contains filtered or unexported fields }
LogFormatterParams is the structure any formatter will be handed when time to log comes
func (*LogFormatterParams) IsOutputColor ¶
func (p *LogFormatterParams) IsOutputColor() bool
IsOutputColor indicates whether can colors be outputted to the log.
func (*LogFormatterParams) MethodColor ¶
func (p *LogFormatterParams) MethodColor() string
MethodColor is the ANSI color for appropriately logging http method to a terminal.
func (*LogFormatterParams) ResetColor ¶
func (p *LogFormatterParams) ResetColor() string
ResetColor resets all escape attributes.
func (*LogFormatterParams) StatusCodeColor ¶
func (p *LogFormatterParams) StatusCodeColor() string
StatusCodeColor is the ANSI color for appropriately logging http status code to a terminal.
type LoggerConfig ¶
type LoggerConfig struct { // Optional. Default value is httplog.DefaultLogFormatter Formatter LogFormatter // Output is a writer where logs are written. // Optional. Default value is httplog.DefaultWriter. Output io.Writer // SkipPaths is an url path array which logs are not written. // Could be a regexp like: /user/payment/* // Optional. SkipPaths []string // HideHeader is a header keys array which value should be masked with ****. // Optional. HideHeaderKeys []string // ProxyHandler is a instance of Proxy struct with could get remote IP using proxy data // Default is default httplog.NewLogger() // If you run you instance on Google App engine or Cloudflare, // you need to create explicit Proxy instance with httplog.NewLoggerWithType(...) ProxyHandler *Proxy // Router prints router name in the log. // If you have more than one router it is useful to get one's name in a console output. RouterName string // CaptureBody saves response body copy for debug purposes CaptureBody bool }
LoggerConfig defines the config for Logger middleware.
type Proxy ¶
type Proxy struct { RemoteIPHeaders []string // contains filtered or unexported fields }
func NewProxy ¶
func NewProxy() *Proxy
NewProxy creates and returns default proxy with default params
func NewProxyWithType ¶
NewProxy creates and returns proxy with specific type
func NewProxyWithTypeAndHeaders ¶
NewProxy creates and returns proxy with specific type and proxy headers
func (*Proxy) ClientIP ¶
ClientIP implements one best effort algorithm to return the real client IP. It calls c.RemoteIP() under the hood, to check if the remote IP is a trusted proxy or not. If it is it will then try to parse the headers defined in Engine.RemoteIPHeaders (defaulting to [X-Forwarded-For, X-Real-Ip]). If the headers are not syntactically valid OR the remote IP does not correspond to a trusted proxy, the remote IP (coming from Request.RemoteAddr) is returned.
type ResponseWriter ¶
type ResponseWriter interface { http.ResponseWriter http.Hijacker http.Flusher http.CloseNotifier // Status returns the status code of the response or 0 if the response has // not been written Status() int // Written returns whether or not the ResponseWriter has been written. Written() bool // Size returns the size of the response body. Size() int // Before allows for a function to be called before the ResponseWriter has been written to. This is // useful for setting headers or any other operations that must happen before a response has been written. Before(func(ResponseWriter)) // Optional copy of response body // If you need to log or save full response bodies - use it // But extra memory and CPU will be used for that Body() []byte // Manually set Status and Size if you need, written is set as well after call Set(status, size int) }
ResponseWriter is a wrapper around http.ResponseWriter that provides extra information about the response. It is recommended that middleware handlers use this construct to wrap a responsewriter if the functionality calls for it.
func NewResponseWriter ¶
func NewResponseWriter(rw http.ResponseWriter) ResponseWriter
NewResponseWriter creates a ResponseWriter that wraps an http.ResponseWriter
func NewResponseWriterWithBody ¶
func NewResponseWriterWithBody(rw http.ResponseWriter) ResponseWriter
NewResponseWriterWithBody creates a ResponseWriter that wraps an http.ResponseWriter and copy the body of response. The body is not copied if you use ReadFrom for obvious reason
func NewWriter ¶
func NewWriter(rw http.ResponseWriter, captureBody bool) ResponseWriter
NewWriter creates a ResponseWriter that wraps an http.ResponseWriter and optionaly capture body