log

package
v0.1.17 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2022 License: MIT Imports: 10 Imported by: 0

README

log

A structured logger for Go, based on zap.

Quick Start

opts := &log.Newoptions()
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

Overview

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

Index

Constants

This section is empty.

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.

View Source
var (

	// EncodedFilename filename for logging when DisableFile is false.
	EncodedFilename string
)

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 added in v0.1.11

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 logging framework.

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 <processname>-<date>.log.

func DefaultTimeEncoder

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

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 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 DebugLogger added in v0.1.4

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 fullname of the log file.

type InfoLogger added in v0.1.4

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 Logger

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

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) Close added in v0.1.11

func (l *Logger) Close() error

func (*Logger) Debug

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

func (*Logger) DebugLogger added in v0.1.4

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) 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

func (*Logger) Info

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

func (*Logger) InfoLogger added in v0.1.4

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) 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

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
	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"`

	// 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 log filename encoder
	FilenameEncoder FilenameEncoder
	// TimeEncoder time encoder
	TimeEncoder TimeEncoder
}

Options Configuration for logging.

func NewOptions

func NewOptions() *Options

NewOptions creates an Options with default parameters.

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