Documentation ¶
Overview ¶
Package log provides function for managing structured logs. You can create a new logger by calling the New function
// Create a production logger with Info level. logger, _ := log.New() defer logger.Close() // Create a production logger with Debug level logger, _ := log.New(log.WithLevel(log.DebugLevel)) defer logger.Close()
Index ¶
- Constants
- func ReplaceGlobal(logger *Logger) func()
- func WithLevel(level Level) func(*Option)
- type Field
- func Any(key string, val interface{}) Field
- func Bool(key string, val bool) Field
- func ByteString(key string, val []byte) Field
- func Duration(key string, val time.Duration) Field
- func Error(err error) Field
- func Float32(key string, val float32) Field
- func Float64(key string, val float64) Field
- func Int(key string, val int) Field
- func Int32(key string, val int32) Field
- func Int64(key string, val int64) Field
- func String(key string, val string) Field
- func Stringer(key string, val fmt.Stringer) Field
- func Strings(key string, val []string) Field
- func Time(key string, val time.Time) Field
- func Uint(key string, val uint) Field
- func Uint32(key string, val uint32) Field
- func Uint64(key string, val uint64) Field
- type FieldType
- type Level
- type Logger
- func (l *Logger) Close()
- func (l *Logger) Debug(ctx context.Context, message string, fields ...Field)
- func (l *Logger) Error(ctx context.Context, message string, fields ...Field)
- func (l *Logger) Fatal(ctx context.Context, message string, fields ...Field)
- func (l *Logger) Info(ctx context.Context, message string, fields ...Field)
- func (l *Logger) Warn(ctx context.Context, message string, fields ...Field)
- type Option
Constants ¶
const ( UnknownType = FieldType(zapcore.UnknownType) ArrayMarshalerType = FieldType(zapcore.ArrayMarshalerType) ObjectMarshalerType = FieldType(zapcore.ObjectMarshalerType) BinaryType = FieldType(zapcore.BinaryType) BoolType = FieldType(zapcore.BoolType) ByteStringType = FieldType(zapcore.ByteStringType) Complex128Type = FieldType(zapcore.Complex128Type) Complex64Type = FieldType(zapcore.Complex64Type) DurationType = FieldType(zapcore.DurationType) Float64Type = FieldType(zapcore.Float64Type) Float32Type = FieldType(zapcore.Float32Type) Int64Type = FieldType(zapcore.Int64Type) Int32Type = FieldType(zapcore.Int32Type) Int16Type = FieldType(zapcore.Int16Type) Int8Type = FieldType(zapcore.Int8Type) StringType = FieldType(zapcore.StringType) TimeType = FieldType(zapcore.TimeType) Uint64Type = FieldType(zapcore.Uint64Type) Uint32Type = FieldType(zapcore.Uint32Type) Uint16Type = FieldType(zapcore.Uint16Type) Uint8Type = FieldType(zapcore.Uint8Type) UintptrType = FieldType(zapcore.UintptrType) ReflectType = FieldType(zapcore.ReflectType) NamespaceType = FieldType(zapcore.NamespaceType) StringerType = FieldType(zapcore.StringerType) ErrorType = FieldType(zapcore.ErrorType) SkipType = FieldType(zapcore.SkipType) )
Field types
const ( // DebugLevel logs are typically voluminous, and are usually disabled in // production. DebugLevel = Level(zapcore.DebugLevel) // InfoLevel is the default logging priority. InfoLevel = Level(zapcore.InfoLevel) // WarnLevel logs are more important than Info, but don't need individual // human review. WarnLevel = Level(zapcore.WarnLevel) // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel = Level(zapcore.ErrorLevel) // FatalLevel logs a message, then calls os.Exit(1). FatalLevel = Level(zapcore.FatalLevel) )
Variables ¶
This section is empty.
Functions ¶
func ReplaceGlobal ¶
func ReplaceGlobal(logger *Logger) func()
ReplaceGlobal replaces the global Logger and returns a function to restore the original values. It's safe for concurrent use.
Types ¶
type FieldType ¶
type FieldType uint8
A FieldType indicates which member of the Field union struct should be used and how it should be serialized.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger provides fast, leveled, structured logging. All methods are safe for concurrent use.
func L ¶
func L() *Logger
L returns the global Logger, which can be reconfigured with ReplaceGlobal. By default, global logger is set to no-op. It's safe for concurrent use.
func New ¶
New is a reasonable production logging configuration. Logging is enabled at InfoLevel and above by default.
It uses a JSON encoder, writes to standard error, and enables sampling. Stacktraces are automatically included on logs of ErrorLevel and above.
func NewNop ¶
func NewNop() *Logger
NewNop returns a no-op Logger. It never writes out logs or internal errors, and it never runs user-defined hooks.
func (*Logger) Close ¶
func (l *Logger) Close()
Close is flushing any buffered log entries. Applications should take care to call Close before exiting.
func (*Logger) Debug ¶
Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func (*Logger) Error ¶
Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func (*Logger) Fatal ¶
Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.