Logging
日志中间件
注意
在下次版本更新时,会迁移到 go-slim/slim/middleware
里面。
type LoggerConfig
// LoggerConfig defines the config for Logger middleware.
type LoggerConfig struct {
// Tags to construct the logger format.
//
// - time_unix
// - time_unix_milli
// - time_unix_micro
// - time_unix_nano
// - time_rfc3339
// - time_rfc3339_nano
// - time_custom
// - id (Request ID)
// - remote_ip
// - uri
// - host
// - method
// - path
// - route
// - protocol
// - referer
// - user_agent
// - status
// - error
// - latency (In nanoseconds)
// - latency_human (Human readable)
// - bytes_in (Bytes received)
// - bytes_out (Bytes sent)
// - header:<NAME>
// - query:<NAME>
// - form:<NAME>
// - custom (see CustomTagFunc field)
//
// Example "${remote_ip} ${status}"
//
// Optional. Default value DefaultLoggerConfig.Format.
Format string `yaml:"format"`
// Optional. Default value DefaultLoggerConfig.CustomTimeFormat.
CustomTimeFormat string `yaml:"custom_time_format"`
// CustomTagFunc is function called for `${custom}` tag to output user implemented text by writing it to buf.
// Make sure that outputted text creates valid JSON string with other logged tags.
// Optional.
CustomTagFunc func(c Context, buf *bytes.Buffer) (int, error)
// Output is a writer where logs in JSON format are written.
// Optional. Default value os.Stdout.
Output io.Writer
pool *sync.Pool
}
日志打印中间件配置结构体。
func Logging
func Logging() MiddlewareFunc
返回默认日志打印中间件,它在非 windows 系统下,将会在控制台输出带颜色的日志信息。
func LoggingWithConfig
func LoggingWithConfig(config LoggerConfig) MiddlewareFunc
使用配置返回日志中间件