logger

package
v0.0.0-...-df360ae Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 10 Imported by: 3

Documentation

Index

Constants

View Source
const (
	// TraceIDKey key
	TraceIDKey key = 0
)

Variables

View Source
var DefaultGenRequestID func() string = func() string {
	var b [12]byte
	binary.LittleEndian.PutUint32(b[:], pid)
	binary.LittleEndian.PutUint64(b[4:], uint64(time.Now().UnixNano()))
	return base64.URLEncoding.EncodeToString(b[:])
}

DefaultGenRequestID default generate request id

View Source
var (

	// DefaultLogTimeFormat default log time format
	DefaultLogTimeFormat = "2006-01-02 15:04:05.000000"
)
View Source
var New func(traceID ...string) Logger = newLogger

New return logger

View Source
var TraceID = "x-request-id"

TraceID is the key for the x-request-id header.

Functions

func Debug

func Debug(arguments ...interface{})

Debug debug level

func Debugf

func Debugf(format string, arguments ...interface{})

Debugf debug format

func Error

func Error(arguments ...interface{})

Error error level

func Errorf

func Errorf(format string, arguments ...interface{})

Errorf error format

func Fatal

func Fatal(arguments ...interface{})

Fatal fatal level

func Fatalf

func Fatalf(format string, arguments ...interface{})

Fatalf fatal format

func Info

func Info(arguments ...interface{})

Info info level

func Infof

func Infof(format string, arguments ...interface{})

Infof info format

func NewContext

func NewContext(ctx context.Context, reqID string) context.Context

NewContext return context with reqid

func Panic

func Panic(arguments ...interface{})

Panic panic level

func Panicf

func Panicf(format string, arguments ...interface{})

Panicf panic format

func SetConfig

func SetConfig(cfg *Config)

SetConfig set logger config

func Warn

func Warn(arguments ...interface{})

Warn warn level

func Warnf

func Warnf(format string, arguments ...interface{})

Warnf warn format

Types

type Config

type Config struct {
	// log level
	LogLevel Level `mapstructure:"log_level"`

	// Enable console logging
	ConsoleLoggingEnabled bool `mapstructure:"console_logging_enabled"`

	// EncodeLogsAsJSON makes the log framework log JSON
	EncodeLogsAsJSON bool `mapstructure:"encode_logs_as_json"`

	// FileLoggingEnabled makes the framework log to a file, the fields below can be skipped if this value is false!
	FileLoggingEnabled bool `mapstructure:"file_logging_enabled"`

	// Filename is the name of the logfile which will be placed inside the directory
	Filename string `mapstructure:"filename"`

	// MaxSize the max size in MB of the logfile before it's rolled
	MaxSize int `mapstructure:"max_size"`

	// MaxBackups the max number of rolled files to keep
	MaxBackups int `mapstructure:"max_backups"`

	// MaxAge the max age in days to keep a logfile
	MaxAge int `mapstructure:"max_age"`
	// contains filtered or unexported fields
}

Config for logging

type Level

type Level int8

Level type

const (
	// DebugLevel defines debug log level.
	DebugLevel Level = iota
	// InfoLevel defines info log level.
	InfoLevel
	// WarnLevel defines warn log level.
	WarnLevel
	// ErrorLevel defines error log level.
	ErrorLevel
	// FatalLevel defines fatal log level.
	FatalLevel
	// PanicLevel defines panic log level.
	PanicLevel
	// NoLevel defines an absent log level.
	NoLevel
	// Disabled disables the logger.
	Disabled
	// TraceLevel defines trace log level.
	TraceLevel Level = -1
)
var (
	// DefaultLogLevel log level
	DefaultLogLevel Level = TraceLevel
)

type Log

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

Log implement Logger

func (*Log) Caller

func (l *Log) Caller(frame int) Logger

Caller set caller frame

func (*Log) Debug

func (l *Log) Debug(arguments ...interface{})

Debug debug level

func (*Log) Debugf

func (l *Log) Debugf(format string, arguments ...interface{})

Debugf debug format

func (*Log) Error

func (l *Log) Error(arguments ...interface{})

Error error level

func (*Log) Errorf

func (l *Log) Errorf(format string, arguments ...interface{})

Errorf error format

func (*Log) Fatal

func (l *Log) Fatal(arguments ...interface{})

Fatal fatal level

func (*Log) Fatalf

func (l *Log) Fatalf(format string, arguments ...interface{})

Fatalf fatal format

func (*Log) Info

func (l *Log) Info(arguments ...interface{})

Info info level

func (*Log) Infof

func (l *Log) Infof(format string, arguments ...interface{})

Infof info format

func (*Log) Panic

func (l *Log) Panic(arguments ...interface{})

Panic panic level

func (*Log) Panicf

func (l *Log) Panicf(format string, arguments ...interface{})

Panicf panic format

func (*Log) SetLevel

func (l *Log) SetLevel(level Level) Logger

SetLevel set level

func (*Log) TraceID

func (l *Log) TraceID() string

TraceID trace id

func (*Log) Warn

func (l *Log) Warn(arguments ...interface{})

Warn warn level

func (*Log) Warnf

func (l *Log) Warnf(format string, arguments ...interface{})

Warnf warn format

func (*Log) WithContext

func (l *Log) WithContext(ctx ...context.Context) context.Context

WithContext return context with log

func (*Log) WithError

func (l *Log) WithError(err error) Logger

WithError adds the field "error" with serialized err to the logger context.

func (*Log) WithField

func (l *Log) WithField(key string, value interface{}) Logger

WithField add new field

func (*Log) WithFields

func (l *Log) WithFields(fields map[string]interface{}) Logger

WithFields add new fields

type Logger

type Logger interface {
	// STD log
	Debug(arguments ...interface{})
	Info(arguments ...interface{})
	Warn(arguments ...interface{})
	Error(arguments ...interface{})
	Fatal(arguments ...interface{})
	Panic(arguments ...interface{})
	Debugf(format string, arguments ...interface{})
	Infof(format string, arguments ...interface{})
	Warnf(format string, arguments ...interface{})
	Errorf(format string, arguments ...interface{})
	Fatalf(format string, arguments ...interface{})
	Panicf(format string, arguments ...interface{})

	// Field logger
	WithField(key string, value interface{}) Logger
	WithFields(fields map[string]interface{}) Logger
	WithError(err error) Logger

	// Set level
	SetLevel(level Level) Logger

	// Caller skip frame count
	Caller(frame int) Logger

	// Trace ID
	TraceID() string

	// context
	WithContext(ctx ...context.Context) context.Context
}

Logger logger methods

func NewWithContext

func NewWithContext(ctx context.Context) Logger

NewWithContext return logger with context

func NewWithoutCaller

func NewWithoutCaller(reqID ...string) Logger

NewWithoutCaller new log without caller field

func WithError

func WithError(err error) Logger

WithError adds the field "error" with serialized err to the logger context.

func WithField

func WithField(key string, value interface{}) Logger

WithField add new field

func WithFields

func WithFields(fields map[string]interface{}) Logger

WithFields add new fields

Jump to

Keyboard shortcuts

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