log

package module
v0.2.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 10, 2024 License: MIT Imports: 13 Imported by: 2

README ¶

log

A structured logger for Go, based on zap. Migrated from golib.

test codecov Go Report Card release license

Quick Start

opts := log.Newoptions()
errs := opts.Validate()
if len(errs) > 0 {
	// handle errors
	return
}

log.Configure(opts)
defer func() {
    _ = log.Close()
}()

log.Debug("debug message")
log.Info("info message")
log.Warn("warn message")
log.Error("error message")

log.Debugf("%s message", "debug")
log.Infof("%s message", "info")
log.Warnf("%s message", "warn")
log.Errorf("%s message", "error")

log.Debugt("debug message", log.String("key1", "value1"))
log.Infot("info message", log.Int32("key2", 10))
log.Warnt("warn message", log.Bool("key3", false))
log.Errort("error message", log.Any("key4", "any"))

Documentation

You can find the docs at go docs.

🔋 JetBrains OS licenses

log had been being developed with GoLand under the free JetBrains Open Source license(s) granted by JetBrains s.r.o., hence I would like to express my thanks here.

JetBrains Logo (Main) logo.

Documentation ¶

Overview ¶

Package log is a structured logger for Go, based on https://github.com/uber-go/zap.

Index ¶

Constants ¶

View Source
const (
	DefaultCallerSkip = 1
)

Variables ¶

View Source
var (
	// DebugLevel logs are typically voluminous, and are usually disabled in
	// production.
	DebugLevel = zapcore.DebugLevel
	// InfoLevel is the default logging priority.
	InfoLevel = zapcore.InfoLevel
	// WarnLevel logs are more important than Info, but don't need individual
	// human review.
	WarnLevel = zapcore.WarnLevel
	// ErrorLevel logs are high-priority. If an application is running smoothly,
	// it shouldn't generate any error-level logs.
	ErrorLevel = zapcore.ErrorLevel
	// DPanicLevel logs are particularly important errors. In development the
	// logger panics after writing the message.
	DPanicLevel = zapcore.DPanicLevel
	// PanicLevel logs a message, then panics.
	PanicLevel = zapcore.PanicLevel
	// FatalLevel logs a message, then calls os.Exit(1).
	FatalLevel = zapcore.FatalLevel
)

Alias for zap log level.

View Source
var (
	Any         = zap.Any
	Array       = zap.Array
	Binary      = zap.Binary
	Bool        = zap.Bool
	Bools       = zap.Bools
	ByteString  = zap.ByteString
	ByteStrings = zap.ByteStrings
	Complex64   = zap.Complex64
	Complex64s  = zap.Complex64s
	Complex128  = zap.Complex128
	Complex128s = zap.Complex128s
	Duration    = zap.Duration
	Durations   = zap.Durations
	Err         = zap.Error
	Errors      = zap.Errors
	Float32     = zap.Float32
	Float32s    = zap.Float32s
	Float64     = zap.Float64
	Float64s    = zap.Float64s
	Int         = zap.Int
	Ints        = zap.Ints
	Int8        = zap.Int8
	Int8s       = zap.Int8s
	Int16       = zap.Int16
	Int16s      = zap.Int16s
	Int32       = zap.Int32
	Int32s      = zap.Int32s
	Int64       = zap.Int64
	Int64s      = zap.Int64s
	Namespace   = zap.Namespace
	Object      = zap.Object
	Reflect     = zap.Reflect
	Stack       = zap.Stack
	String      = zap.String
	Stringer    = zap.Stringer
	Strings     = zap.Strings
	Time        = zap.Time
	Times       = zap.Times
	Uint        = zap.Uint
	Uints       = zap.Uints
	Uint8       = zap.Uint8
	Uint8s      = zap.Uint8s
	Uint16      = zap.Uint16
	Uint16s     = zap.Uint16s
	Uint32      = zap.Uint32
	Uint32s     = zap.Uint32s
	Uint64      = zap.Uint64
	Uint64s     = zap.Uint64s
	Uintptr     = zap.Uintptr
	Uintptrs    = zap.Uintptrs
)

Alias for zap type functions.

Functions ¶

func AtLevel ¶

func AtLevel(level Level, msg string, keysAndValues ...interface{})

AtLevel logs a message at Level.

func AtLevelf ¶

func AtLevelf(level Level, msg string, args ...interface{})

AtLevelf logs a message at Level.

func AtLevelt ¶

func AtLevelt(level Level, msg string, fields ...Field)

AtLevelt logs a message at Level.

func Close ¶

func Close() error

Close implements io.Closer, and closes the current logfile of default logger.

func Configure ¶

func Configure(opts *Options)

Configure sets up the global logger.

func Debug ¶

func Debug(msg string, keysAndValues ...interface{})

Debug logs a message at DebugLevel.

func Debugf ¶

func Debugf(template string, args ...interface{})

Debugf logs a message at DebugLevel.

func Debugt ¶

func Debugt(msg string, fields ...Field)

Debugt logs a message at DebugLevel.

func DefaultFilenameEncoder ¶

func DefaultFilenameEncoder() string

DefaultFilenameEncoder return <process name>-<date>.log.

func DefaultTimeEncoder ¶

func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

func EncodedFilename ¶

func EncodedFilename() string

EncodedFilename returns the filename for logging when DisableFile is false.

func Error ¶

func Error(msg string, keysAndValues ...interface{})

Error logs a message at ErrorLevel.

func Errorf ¶

func Errorf(template string, args ...interface{})

Errorf logs a message at ErrorLevel.

func Errort ¶

func Errort(msg string, fields ...Field)

Errort logs a message at ErrorLevel.

func Fatal ¶

func Fatal(msg string, keysAndValues ...interface{})

Fatal logs a message with some additional context, then calls os.Exit.

func Fatalf ¶

func Fatalf(template string, args ...interface{})

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Fatalt ¶

func Fatalt(msg string, fields ...Field)

Fatalt logs a message at FatalLevel, then calls os.Exit(1).

func Flush ¶

func Flush() error

Flush calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func Info ¶

func Info(msg string, keysAndValues ...interface{})

Info logs a message at InfoLevel.

func Infof ¶

func Infof(template string, args ...interface{})

Infof logs a message at InfoLevel.

func Infot ¶

func Infot(msg string, fields ...Field)

Infot logs a message at InfoLevel.

func Panic ¶

func Panic(msg string, keysAndValues ...interface{})

Panic logs a message with some additional context, then panics.

func Panicf ¶

func Panicf(template string, args ...interface{})

Panicf uses fmt.Sprintf to log a templated message, then panics.

func Panict ¶

func Panict(msg string, fields ...Field)

Panict logs a message at PanicLevel, then panics.

func Print ¶ added in v0.1.10

func Print(args ...interface{})

Print logs a message at level Print.

func Printf ¶ added in v0.1.10

func Printf(format string, args ...interface{})

Printf logs a message at level Print.

func Println ¶ added in v0.1.10

func Println(args ...interface{})

Println logs a message at level Print.

func StdLogger ¶ added in v0.1.6

func StdLogger(level Level) *log.Logger

StdLogger returns logger of standard library which writes to supplied zap logger at the given level.

func SugaredL ¶ added in v0.1.10

func SugaredL() *zap.SugaredLogger

SugaredL returns global sugared logger.

func Warn ¶

func Warn(msg string, keysAndValues ...interface{})

Warn logs a message at WarnLevel.

func Warnf ¶

func Warnf(template string, args ...interface{})

Warnf logs a message at WarnLevel.

func Warnt ¶

func Warnt(msg string, fields ...Field)

Warnt logs a message at WarnLevel.

Types ¶

type CallerEncoder ¶ added in v0.1.3

type CallerEncoder = zapcore.CallerEncoder

CallerEncoder is an alias for the zapcore.CallerEncoder.

type CheckedEntry ¶ added in v0.2.2

type CheckedEntry = zapcore.CheckedEntry

CheckedEntry is an alias for the zapcore.CheckedEntry.

func Check ¶ added in v0.2.2

func Check(lvl Level, msg string) *CheckedEntry

Check returns a CheckedEntry if logging a message at the specified level is enabled. It's a completely optional optimization; in high-performance applications, Check can help avoid allocating a slice to hold fields.

type DebugLogger ¶

type DebugLogger interface {
	Debugt(msg string, fields ...Field)
	Debugf(template string, args ...interface{})
	Debug(msg string, keysAndValues ...interface{})
}

type Field ¶

type Field = zapcore.Field

Field is an alias for the zapcore.Field.

type FilenameEncoder ¶

type FilenameEncoder func() string

FilenameEncoder log filename encoder, return the full name of the log file.

type InfoLogger ¶

type InfoLogger interface {
	DebugLogger

	Infot(msg string, fields ...Field)
	Infof(template string, args ...interface{})
	Info(msg string, keysAndValues ...interface{})
}

type Interface ¶

type Interface interface {
	InfoLogger

	Warnt(msg string, fields ...Field)
	Warnf(template string, args ...interface{})
	Warn(msg string, keysAndValues ...interface{})

	Errort(msg string, fields ...Field)
	Errorf(template string, args ...interface{})
	Error(msg string, keysAndValues ...interface{})

	Panict(msg string, fields ...Field)
	Panicf(template string, args ...interface{})
	Panic(msg string, keysAndValues ...interface{})

	Fatalt(msg string, fields ...Field)
	Fatalf(template string, args ...interface{})
	Fatal(msg string, keysAndValues ...interface{})

	AtLevelt(level Level, msg string, fields ...Field)
	AtLevelf(level Level, template string, args ...interface{})
	AtLevel(level Level, msg string, keysAndValues ...interface{})

	// WithValues creates a child logger and adds some Field of
	// context to this logger.
	WithValues(fields ...Field) *Logger

	// Flush calls the underlying Core's Sync method, flushing any buffered
	// log entries. Applications should take care to call Sync before exiting.
	Flush() error

	// Close implements io.Closer, and closes the current logfile.
	Close() error
}

type Level ¶

type Level = zapcore.Level

Level is an alias for the zapcore.Level.

type LevelEncoder ¶ added in v0.1.3

type LevelEncoder = zapcore.LevelEncoder

LevelEncoder is an alias for the zapcore.LevelEncoder.

type Logger ¶

type Logger struct {
	// contains filtered or unexported fields
}

func L ¶ added in v0.1.2

func L() *Logger

L returns the global logger.

func New ¶

func New(opts *Options) *Logger

New creates a new Logger.

func WithValues ¶

func WithValues(fields ...Field) *Logger

WithValues creates a child logger and adds some Field of context to this logger.

func (*Logger) AtLevel ¶

func (l *Logger) AtLevel(level Level, msg string, keysAndValues ...interface{})

func (*Logger) AtLevelf ¶

func (l *Logger) AtLevelf(level Level, msg string, args ...interface{})

func (*Logger) AtLevelt ¶

func (l *Logger) AtLevelt(level Level, msg string, fields ...Field)

func (*Logger) Check ¶ added in v0.2.2

func (l *Logger) Check(lvl Level, msg string) *CheckedEntry

Check returns a CheckedEntry if logging a message at the specified level is enabled. It's a completely optional optimization; in high-performance applications, Check can help avoid allocating a slice to hold fields.

func (*Logger) Close ¶

func (l *Logger) Close() error

Close implements io.Closer, and closes the current logfile of default logger.

func (*Logger) Debug ¶

func (l *Logger) Debug(msg string, keysAndValues ...interface{})

func (*Logger) DebugLogger ¶

func (l *Logger) DebugLogger() DebugLogger

func (*Logger) Debugf ¶

func (l *Logger) Debugf(template string, args ...interface{})

func (*Logger) Debugt ¶

func (l *Logger) Debugt(msg string, fields ...Field)

func (*Logger) EncodedFilename ¶ added in v0.1.9

func (l *Logger) EncodedFilename() string

func (*Logger) Error ¶

func (l *Logger) Error(msg string, keysAndValues ...interface{})

func (*Logger) Errorf ¶

func (l *Logger) Errorf(template string, args ...interface{})

func (*Logger) Errort ¶

func (l *Logger) Errort(msg string, fields ...Field)

func (*Logger) Fatal ¶

func (l *Logger) Fatal(msg string, keysAndValues ...interface{})

func (*Logger) Fatalf ¶

func (l *Logger) Fatalf(template string, args ...interface{})

func (*Logger) Fatalt ¶

func (l *Logger) Fatalt(msg string, fields ...Field)

func (*Logger) Flush ¶

func (l *Logger) Flush() error

Flush calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func (*Logger) Info ¶

func (l *Logger) Info(msg string, keysAndValues ...interface{})

func (*Logger) InfoLogger ¶

func (l *Logger) InfoLogger() InfoLogger

func (*Logger) Infof ¶

func (l *Logger) Infof(template string, args ...interface{})

func (*Logger) Infot ¶

func (l *Logger) Infot(msg string, fields ...Field)

func (*Logger) Panic ¶

func (l *Logger) Panic(msg string, keysAndValues ...interface{})

func (*Logger) Panicf ¶

func (l *Logger) Panicf(template string, args ...interface{})

func (*Logger) Panict ¶

func (l *Logger) Panict(msg string, fields ...Field)

func (*Logger) Print ¶ added in v0.1.10

func (l *Logger) Print(args ...interface{})

Print logs a message at level Print.

func (*Logger) Printf ¶ added in v0.1.10

func (l *Logger) Printf(format string, args ...interface{})

Printf logs a message at level Print.

func (*Logger) Println ¶ added in v0.1.10

func (l *Logger) Println(args ...interface{})

Println logs a message at level Print.

func (*Logger) Sugared ¶ added in v0.1.10

func (l *Logger) Sugared() *zap.SugaredLogger

Sugared returns sugared logger. SugaredLogger wraps the Logger to provide a more ergonomic, but slightly slower, API. Sugaring a Logger is quite inexpensive, so it's reasonable for a single application to use both Loggers and SugaredLoggers, converting between them on the boundaries of performance-sensitive code.

func (*Logger) Warn ¶

func (l *Logger) Warn(msg string, keysAndValues ...interface{})

func (*Logger) Warnf ¶

func (l *Logger) Warnf(template string, args ...interface{})

func (*Logger) Warnt ¶

func (l *Logger) Warnt(msg string, fields ...Field)

func (*Logger) WithValues ¶

func (l *Logger) WithValues(fields ...Field) *Logger

WithValues creates a child logger and adds some Field of context to this logger.

type Options ¶

type Options struct {
	// DisableConsole whether to log to console
	DisableConsole bool `json:"disable-console" mapstructure:"disable-console"`
	// DisableConsoleColor force disabling colors.
	DisableConsoleColor bool `json:"disable-console-color" mapstructure:"disable-console-color"`
	// DisableConsoleTime whether to add a time
	DisableConsoleTime bool `json:"disable-console-time" mapstructure:"disable-console-time"`
	// DisableConsoleLevel whether to add a level
	DisableConsoleLevel bool `json:"disable-console-level" mapstructure:"disable-console-level"`
	// DisableConsoleCaller whether to log caller info
	DisableConsoleCaller bool `json:"disable-console-caller" mapstructure:"disable-console-caller"`

	// DisableFile whether to log to file
	DisableFile bool `json:"disable-file" mapstructure:"disable-file"`
	// DisableFileJson whether to enable json format for log file
	//nolint:revive
	DisableFileJson bool `json:"disable-file-json" mapstructure:"disable-file-json"`
	// DisableFileTime whether to add a time
	DisableFileTime bool `json:"disable-file-time" mapstructure:"disable-file-time"`
	// DisableFileCaller whether to log caller info
	DisableFileCaller bool `json:"disable-file-caller" mapstructure:"disable-file-caller"`

	// DisableRotate whether to enable log file rotate
	DisableRotate bool `json:"disable-rotate" mapstructure:"disable-rotate"`
	// MaxSize the max size in MB of the logfile before it's rolled
	MaxSize int `json:"max-size" mapstructure:"max-size"`
	// MaxBackups the max number of rolled files to keep
	MaxBackups int `json:"max-backups" mapstructure:"max-backups"`
	// MaxAge the max age in days to keep a logfile
	MaxAge int `json:"max-age" mapstructure:"max-age"`

	// CallerSkip increases the number of callers skipped by caller annotation
	CallerSkip int `json:"caller-skip" mapstructure:"caller-skip"`

	// ConsoleLevel sets the standard logger level
	ConsoleLevel string `json:"console-level" mapstructure:"console-level"`
	// FileLevel sets the file logger level.
	FileLevel string `json:"file-level" mapstructure:"file-level"`

	// Output directory for logging when DisableFile is false
	Output string `json:"output" mapstructure:"output"`

	// FilenameEncoder is used to set the log filename encoder.
	FilenameEncoder FilenameEncoder `json:"-" mapstructure:"-"`
	// TimeEncoder is used to set the log time encoder.
	TimeEncoder TimeEncoder `json:"-" mapstructure:"-"`
	// LevelEncoder is used to set the log level encoder.
	LevelEncoder LevelEncoder `json:"-" mapstructure:"-"`
	// CallerEncoder is used to set the log caller encoder.
	CallerEncoder CallerEncoder `json:"-" mapstructure:"-"`
}

Options Configuration for logging.

func NewOptions ¶

func NewOptions() *Options

NewOptions creates an Options with default parameters.

func (*Options) AddFlags ¶ added in v0.1.12

func (o *Options) AddFlags(fs *pflag.FlagSet)

AddFlags adds flags related to logger to the specified FlagSet.

func (*Options) String ¶ added in v0.1.12

func (o *Options) String() string

func (*Options) Validate ¶

func (o *Options) Validate() []error

Validate validates the options fields.

type TimeEncoder ¶

type TimeEncoder = zapcore.TimeEncoder

TimeEncoder is an alias for the zapcore.TimeEncoder.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL