Documentation ¶
Index ¶
- Constants
- Variables
- func ConcurrentWriter(w io.Writer) io.Writer
- func Debug(msg string, fields ...interface{})
- func DebugContext(ctx context.Context, msg string, fields ...interface{})
- func Error(msg string, fields ...interface{})
- func ErrorContext(ctx context.Context, msg string, fields ...interface{})
- func Fatal(msg string, fields ...interface{})
- func FatalContext(ctx context.Context, msg string, fields ...interface{})
- func FormatTime(t time.Time) (result [23]byte)
- func FormatTimeString(t time.Time) string
- func Info(msg string, fields ...interface{})
- func InfoContext(ctx context.Context, msg string, fields ...interface{})
- func JSON(v interface{}) string
- func NewContext(ctx context.Context, logger Logger) context.Context
- func NewRequest(req *http.Request, logger Logger) *http.Request
- func Output(calldepth int, level Level, msg string, fields ...interface{})
- func OutputContext(ctx context.Context, calldepth int, level Level, msg string, ...)
- func SetBytesBufferPool(pool BytesBufferPool)
- func SetDefaultOptions(opts []Option)
- func SetFormatter(formatter Formatter)
- func SetLevel(level Level) error
- func SetLevelString(str string) error
- func SetOutput(output io.Writer)
- func Warn(msg string, fields ...interface{})
- func WarnContext(ctx context.Context, msg string, fields ...interface{})
- func XML(v interface{}) string
- type BytesBufferPool
- type Entry
- type Formatter
- type Level
- type Logger
- func FromContext(ctx context.Context) (lg Logger, ok bool)
- func FromContextOrNew(ctx context.Context, new func() Logger) (lg Logger, ctx2 context.Context, isNew bool)
- func FromRequest(req *http.Request) (lg Logger, ok bool)
- func FromRequestOrNew(req *http.Request, new func() Logger) (lg Logger, req2 *http.Request, isNew bool)
- func MustFromContext(ctx context.Context) Logger
- func MustFromRequest(req *http.Request) Logger
- func New(opts ...Option) Logger
- func WithField(key string, value interface{}) Logger
- func WithFieldContext(ctx context.Context, key string, value interface{}) Logger
- func WithFields(fields ...interface{}) Logger
- func WithFieldsContext(ctx context.Context, fields ...interface{}) Logger
- type NoopLogger
- func (NoopLogger) Debug(msg string, fields ...interface{})
- func (NoopLogger) Error(msg string, fields ...interface{})
- func (NoopLogger) Fatal(msg string, fields ...interface{})
- func (NoopLogger) Info(msg string, fields ...interface{})
- func (NoopLogger) Output(calldepth int, level Level, msg string, fields ...interface{})
- func (NoopLogger) SetFormatter(Formatter)
- func (NoopLogger) SetLevel(Level) error
- func (NoopLogger) SetLevelString(string) error
- func (NoopLogger) SetOutput(io.Writer)
- func (NoopLogger) Warn(msg string, fields ...interface{})
- func (NoopLogger) WithField(key string, value interface{}) Logger
- func (NoopLogger) WithFields(fields ...interface{}) Logger
- type Option
Constants ¶
const ( FatalLevelString = "fatal" ErrorLevelString = "error" WarnLevelString = "warning" InfoLevelString = "info" DebugLevelString = "debug" )
const TimeFormatLayout = "2006-01-02 15:04:05.000"
Variables ¶
var ( ConcurrentStdout io.Writer = ConcurrentWriter(os.Stdout) ConcurrentStderr io.Writer = ConcurrentWriter(os.Stderr) )
Functions ¶
func ConcurrentWriter ¶
ConcurrentWriter wraps an io.Writer and returns a concurrent io.Writer.
func Debug ¶
func Debug(msg string, fields ...interface{})
Debug logs a message at DebugLevel on the standard logger. For more information see the Logger interface.
func DebugContext ¶
DebugContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(1, DebugLevel, msg, fields...) return } Output(1, DebugLevel, msg, fields...)
func Error ¶
func Error(msg string, fields ...interface{})
Error logs a message at ErrorLevel on the standard logger. For more information see the Logger interface.
func ErrorContext ¶
ErrorContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(1, ErrorLevel, msg, fields...) return } Output(1, ErrorLevel, msg, fields...)
func Fatal ¶
func Fatal(msg string, fields ...interface{})
Fatal logs a message at FatalLevel on the standard logger. For more information see the Logger interface.
func FatalContext ¶
FatalContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(1, FatalLevel, msg, fields...) return } Output(1, FatalLevel, msg, fields...)
func Info ¶
func Info(msg string, fields ...interface{})
Info logs a message at InfoLevel on the standard logger. For more information see the Logger interface.
func InfoContext ¶
InfoContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(1, InfoLevel, msg, fields...) return } Output(1, InfoLevel, msg, fields...)
func JSON ¶
func JSON(v interface{}) string
JSON is a helper function, following is its function code.
data, _ := json.Marshal(v) return string(data)
func Output ¶
Output logs a message at specified level on the standard logger. For more information see the Logger interface.
func OutputContext ¶
func OutputContext(ctx context.Context, calldepth int, level Level, msg string, fields ...interface{})
OutputContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(calldepth+1, level, msg, fields...) return } Output(calldepth+1, level, msg, fields...)
func SetBytesBufferPool ¶
func SetBytesBufferPool(pool BytesBufferPool)
func SetDefaultOptions ¶
func SetDefaultOptions(opts []Option)
func SetFormatter ¶
func SetFormatter(formatter Formatter)
SetFormatter sets the standard logger formatter.
func SetLevelString ¶
SetLevelString sets the standard logger level.
func SetOutput ¶
SetOutput sets the standard logger output.
NOTE: output must be thread-safe, see ConcurrentWriter.
func Warn ¶
func Warn(msg string, fields ...interface{})
Warn logs a message at WarnLevel on the standard logger. For more information see the Logger interface.
func WarnContext ¶
WarnContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { lg.Output(1, WarnLevel, msg, fields...) return } Output(1, WarnLevel, msg, fields...)
Types ¶
type Formatter ¶
var JsonFormatter Formatter = jsonFormatter{}
var TextFormatter Formatter = textFormatter{}
type Logger ¶
type Logger interface { // Fatal logs a message at FatalLevel. // // Unlike other golang log libraries (for example, the golang standard log library), // Fatal just logs a message and does not call os.Exit, so you need to explicitly call os.Exit if necessary. // // For fields, the following conditions must be satisfied // 1. the len(fields) must be an even number, that is to say len(fields)%2==0 // 2. the even index element of fields must be non-empty string Fatal(msg string, fields ...interface{}) // Error logs a message at ErrorLevel. // The requirements for fields can see the comments of Fatal. Error(msg string, fields ...interface{}) // Warn logs a message at WarnLevel. // The requirements for fields can see the comments of Fatal. Warn(msg string, fields ...interface{}) // Info logs a message at InfoLevel. // The requirements for fields can see the comments of Fatal. Info(msg string, fields ...interface{}) // Debug logs a message at DebugLevel. // The requirements for fields can see the comments of Fatal. Debug(msg string, fields ...interface{}) // Output logs a message at specified level. // // For level==FatalLevel, unlike other golang log libraries (for example, the golang standard log library), // Output just logs a message and does not call os.Exit, so you need to explicitly call os.Exit if necessary. // // The requirements for fields can see the comments of Fatal. Output(calldepth int, level Level, msg string, fields ...interface{}) // WithField creates a new Logger from the current Logger and adds a field to it. WithField(key string, value interface{}) Logger // WithFields creates a new Logger from the current Logger and adds multiple fields to it. // The requirements for fields can see the comments of Fatal. WithFields(fields ...interface{}) Logger // SetFormatter sets the logger formatter. SetFormatter(Formatter) // SetOutput sets the logger output. SetOutput(io.Writer) // SetLevel sets the logger level. SetLevel(Level) error // SetLevelString sets the logger level. SetLevelString(string) error }
func FromContextOrNew ¶
func FromRequestOrNew ¶
func MustFromContext ¶
func MustFromRequest ¶
func WithField ¶
WithField creates a new Logger from the standard Logger and adds a field to it. For more information see the Logger interface.
func WithFieldContext ¶
WithFieldContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { return lg.WithField(key, value) } return WithField(key, value)
func WithFields ¶
func WithFields(fields ...interface{}) Logger
WithFields creates a new Logger from the standard Logger and adds multiple fields to it. For more information see the Logger interface.
func WithFieldsContext ¶
WithFieldsContext is a shortcut to the following code:
lg, ok := FromContext(ctx) if ok { return lg.WithFields(fields...) } return WithFields(fields...)
type NoopLogger ¶
type NoopLogger struct{}
NoopLogger no operation Logger
func (NoopLogger) Debug ¶
func (NoopLogger) Debug(msg string, fields ...interface{})
Debug impl Logger Debug
func (NoopLogger) Error ¶
func (NoopLogger) Error(msg string, fields ...interface{})
Error impl Logger Error
func (NoopLogger) Fatal ¶
func (NoopLogger) Fatal(msg string, fields ...interface{})
Fatal impl Logger Fatal
func (NoopLogger) Info ¶
func (NoopLogger) Info(msg string, fields ...interface{})
Info impl Logger Info
func (NoopLogger) Output ¶
func (NoopLogger) Output(calldepth int, level Level, msg string, fields ...interface{})
Output impl Logger Output
func (NoopLogger) SetFormatter ¶
func (NoopLogger) SetFormatter(Formatter)
SetFormatter impl Logger SetFormatter
func (NoopLogger) SetLevelString ¶
func (NoopLogger) SetLevelString(string) error
SetLevelString impl Logger SetLevelString
func (NoopLogger) SetOutput ¶
func (NoopLogger) SetOutput(io.Writer)
SetOutput impl Logger SetOutput
func (NoopLogger) Warn ¶
func (NoopLogger) Warn(msg string, fields ...interface{})
Warn impl Logger Warn
func (NoopLogger) WithField ¶
func (NoopLogger) WithField(key string, value interface{}) Logger
WithField impl Logger WithField
func (NoopLogger) WithFields ¶
func (NoopLogger) WithFields(fields ...interface{}) Logger
WithFields impl Logger WithFields
type Option ¶
type Option func(*options)
func WithFormatter ¶
func WithLevelString ¶
func WithOutput ¶
WithOutput sets the logger output.
NOTE: output must be thread-safe, see ConcurrentWriter.