logger

package
v0.0.0-...-a2093d6 Latest Latest
Warning

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

Go to latest
Published: Mar 11, 2022 License: LGPL-2.1 Imports: 11 Imported by: 0

README

logger

This package is based on reading the logrus source code, encapsulating it, and then adding some commonly used custom functions, and adapting to logrus

For example:

  • logger has built-in file segmentation hook;
  • Built in quick creation of lohger, which contains the common fields of Web services;
  • The default format is JSON;
  • By default, the simplified file name and function name are carried;

Encapsulating logrus makes me better understand the principle of logrus. Even most of the code is similar to logrus. I must thank logrus !

I may add some other hooks later, such as outputting logs to Redis, ElasticSearch, etc.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(args ...interface{})

Debug logs a message at level Debug on the standard logger.

func Debugf

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

Debugf logs a message at level Debug on the standard logger.

func Debugln

func Debugln(args ...interface{})

Debugln logs a message at level Debug on the standard logger.

func DefaultRotateFileOutput

func DefaultRotateFileOutput(filename string) io.Writer

func Error

func Error(args ...interface{})

Error logs a message at level Error on the standard logger.

func Errorf

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

Errorf logs a message at level Error on the standard logger.

func Errorln

func Errorln(args ...interface{})

Errorln logs a message at level Error on the standard logger.

func Fatal

func Fatal(args ...interface{})

Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func Fatalf

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

Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func Fatalln

func Fatalln(args ...interface{})

Fatalln logs a message at level Fatal on the standard logger then the process will exit with status set to 1.

func FileOutput

func FileOutput(filename interface{}) io.Writer

func GetLevel

func GetLevel() level.Level

GetLevel returns the standard logger level.

func Info

func Info(args ...interface{})

Info logs a message at level Info on the standard logger.

func Infof

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

Infof logs a message at level Info on the standard logger.

func Infoln

func Infoln(args ...interface{})

Infoln logs a message at level Info on the standard logger.

func IsLevelEnabled

func IsLevelEnabled(level level.Level) bool

IsLevelEnabled checks if the log level of the standard logger is greater than the level param

func Panic

func Panic(args ...interface{})

Panic logs a message at level Panic on the standard logger.

func Panicf

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

Panicf logs a message at level Panic on the standard logger.

func Panicln

func Panicln(args ...interface{})

Panicln logs a message at level Panic on the standard logger.

func Print

func Print(args ...interface{})

Print logs a message at level Info on the standard logger.

func Printf

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

Printf logs a message at level Info on the standard logger.

func Println

func Println(args ...interface{})

Println logs a message at level Info on the standard logger.

func ResetStandardLogger

func ResetStandardLogger(options ...Option)

func ResetStandardLoggerWithConfig

func ResetStandardLoggerWithConfig(config Config)

func RotateFileOutput

func RotateFileOutput(filename string, rotateTime time.Duration, expireTime time.Duration) io.Writer

func StandardErrOutput

func StandardErrOutput() io.Writer

func StandardOutput

func StandardOutput() io.Writer

func Trace

func Trace(args ...interface{})

Trace logs a message at level Trace on the standard logger.

func Tracef

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

Tracef logs a message at level Trace on the standard logger.

func Traceln

func Traceln(args ...interface{})

Traceln logs a message at level Trace on the standard logger.

func Warn

func Warn(args ...interface{})

Warn logs a message at level Warn on the standard logger.

func Warnf

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

Warnf logs a message at level Warn on the standard logger.

func Warning

func Warning(args ...interface{})

Warning logs a message at level Warn on the standard logger.

func Warningf

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

Warningf logs a message at level Warn on the standard logger.

func Warningln

func Warningln(args ...interface{})

Warningln logs a message at level Warn on the standard logger.

func Warnln

func Warnln(args ...interface{})

Warnln logs a message at level Warn on the standard logger.

Types

type Config

type Config struct {
	// Level default INFO
	Level string `mapstructure:"level" json:"level"`

	// AppName Application name or web service name. If it is not empty, a field named "app" will be added to the log
	// 应用名称或Web服务名称,非空时会在日志中添加名为"app"的Field
	AppName string `mapstructure:"app_name" json:"app_name"`

	// Format The default format is JSON. Formatters.go provides two additional formats: text and bracket
	// 默认格式化方式为JSON,formatters.go额外提供了"text"和"bracket"两种格式
	Format string `mapstructure:"format" json:"format"`

	// Timestamp Whether to display the timestamp, default true
	// 是否显示时间戳, 默认true
	Timestamp bool `mapstructure:"timestamp" json:"timestamp"`

	// File If this item is configured, it means that the output is a local file
	// 配置了File表示output为本地文件
	File string `mapstructure:"file" json:"file"`

	// ErrFile If this item is configured, the log at error level will be input to a separate local file
	// 配置了ErrFile表示error级别的日志将输入到单独的本地文件
	ErrFile string `mapstructure:"err_file" json:"err_file"`

	// RotateFile If this item is configured, which means that output is divided to the local file system by rotation
	// If File is configured at the same time, RotateFile will prevail
	// 配置了RotateFile表示output采用轮转分割到本地文件系统,若同时配置了File, 将以RotateFile为准
	RotateFile RotateFileConfig `mapstructure:"rotate_file" json:"rotate_file"`
}

type Entry

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

func NewEntry

func NewEntry(logger *Logger) *Entry

func WithContext

func WithContext(ctx context.Context) *Entry

WithContext creates an entry from the standard logger and adds a context to it.

func WithError

func WithError(err error) *Entry

WithError creates an entry from the standard logger and adds an error to it, using the value defined in ErrorKey as key.

func WithField

func WithField(key string, value interface{}) *Entry

WithField creates an entry from the standard logger and adds a field to it. If you want multiple fields, use `WithFields`.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func WithFields

func WithFields(fields field.Fields) *Entry

WithFields creates an entry from the standard logger and adds multiple fields to it. This is simply a helper for `WithField`, invoking it once for each field.

Note that it doesn't log until you call Debug, Print, Info, Warn, Fatal or Panic on the Entry it returns.

func (*Entry) Bytes

func (entry *Entry) Bytes() ([]byte, error)

Bytes Returns the bytes representation of this entry from the format.

func (*Entry) Debug

func (entry *Entry) Debug(args ...interface{})

func (*Entry) Debugf

func (entry *Entry) Debugf(format string, args ...interface{})

func (*Entry) Debugln

func (entry *Entry) Debugln(args ...interface{})

func (*Entry) Error

func (entry *Entry) Error(args ...interface{})

func (*Entry) Errorf

func (entry *Entry) Errorf(format string, args ...interface{})

func (*Entry) Errorln

func (entry *Entry) Errorln(args ...interface{})

func (*Entry) Fatal

func (entry *Entry) Fatal(args ...interface{})

func (*Entry) Fatalf

func (entry *Entry) Fatalf(format string, args ...interface{})

func (*Entry) Fatalln

func (entry *Entry) Fatalln(args ...interface{})

func (Entry) HasCaller

func (entry Entry) HasCaller() bool

func (*Entry) Info

func (entry *Entry) Info(args ...interface{})

func (*Entry) Infof

func (entry *Entry) Infof(format string, args ...interface{})

func (*Entry) Infoln

func (entry *Entry) Infoln(args ...interface{})

func (*Entry) Kernel

func (entry *Entry) Kernel() *logrus.Entry

func (*Entry) Log

func (entry *Entry) Log(level level.Level, args ...interface{})

func (*Entry) Logf

func (entry *Entry) Logf(level level.Level, format string, args ...interface{})

func (*Entry) Logln

func (entry *Entry) Logln(level level.Level, args ...interface{})

func (*Entry) Panic

func (entry *Entry) Panic(args ...interface{})

func (*Entry) Panicf

func (entry *Entry) Panicf(format string, args ...interface{})

func (*Entry) Panicln

func (entry *Entry) Panicln(args ...interface{})

func (*Entry) Print

func (entry *Entry) Print(args ...interface{})

func (*Entry) Printf

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

func (*Entry) Println

func (entry *Entry) Println(args ...interface{})

func (*Entry) String

func (entry *Entry) String() (string, error)

String Returns the string representation from the reader and ultimately the format.

func (*Entry) Trace

func (entry *Entry) Trace(args ...interface{})

func (*Entry) Tracef

func (entry *Entry) Tracef(format string, args ...interface{})

func (*Entry) Traceln

func (entry *Entry) Traceln(args ...interface{})

func (*Entry) Warn

func (entry *Entry) Warn(args ...interface{})

func (*Entry) Warnf

func (entry *Entry) Warnf(format string, args ...interface{})

func (*Entry) Warning

func (entry *Entry) Warning(args ...interface{})

func (*Entry) Warningf

func (entry *Entry) Warningf(format string, args ...interface{})

func (*Entry) Warningln

func (entry *Entry) Warningln(args ...interface{})

func (*Entry) Warnln

func (entry *Entry) Warnln(args ...interface{})

func (*Entry) WithContext

func (entry *Entry) WithContext(ctx context.Context) *Entry

WithContext Add a context to the Entry.

func (*Entry) WithError

func (entry *Entry) WithError(err error) *Entry

WithError Add an error as single field (using the key defined in ErrorKey) to the Entry.

func (*Entry) WithField

func (entry *Entry) WithField(key string, value interface{}) *Entry

WithField Add a single field to the Entry.

func (*Entry) WithFields

func (entry *Entry) WithFields(fields field.Fields) *Entry

WithFields Add a map of fields to the Entry.

type Logger

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

func AddHook

func AddHook(hook hook.Hook) *Logger

AddHook adds a hook to the standard logger hook.

func DisableCaller

func DisableCaller() *Logger

DisableCaller sets whether the standard logger will include the calling method as a field.

func New

func New(options ...Option) *Logger

func NewLoggerWithConfig

func NewLoggerWithConfig(config Config) *Logger

func RotateFileLogger

func RotateFileLogger(filename string, rotateTime time.Duration, expireTime time.Duration) *Logger

func SetFormatter

func SetFormatter(formatter format.Formatter) *Logger

SetFormatter sets the standard logger format.

func SetLevel

func SetLevel(level level.Level) *Logger

SetLevel sets the standard logger level.

func SetOutput

func SetOutput(out io.Writer) *Logger

SetOutput sets the standard logger output.

func StandardLogger

func StandardLogger() *Logger

func (*Logger) AddHook

func (logger *Logger) AddHook(hook hook.Hook) *Logger

AddHook adds a hook to the logger hook.

func (*Logger) Debug

func (logger *Logger) Debug(args ...interface{})

func (*Logger) Debugf

func (logger *Logger) Debugf(format string, args ...interface{})

func (*Logger) Debugln

func (logger *Logger) Debugln(args ...interface{})

func (*Logger) DisableCaller

func (logger *Logger) DisableCaller() *Logger

DisableCaller set ReportCaller false

func (*Logger) Error

func (logger *Logger) Error(args ...interface{})

func (*Logger) Errorf

func (logger *Logger) Errorf(format string, args ...interface{})

func (*Logger) Errorln

func (logger *Logger) Errorln(args ...interface{})

func (*Logger) Fatal

func (logger *Logger) Fatal(args ...interface{})

func (*Logger) Fatalf

func (logger *Logger) Fatalf(format string, args ...interface{})

func (*Logger) Fatalln

func (logger *Logger) Fatalln(args ...interface{})

func (*Logger) GetLevel

func (logger *Logger) GetLevel() level.Level

GetLevel returns the logger level.

func (*Logger) Info

func (logger *Logger) Info(args ...interface{})

func (*Logger) Infof

func (logger *Logger) Infof(format string, args ...interface{})

func (*Logger) Infoln

func (logger *Logger) Infoln(args ...interface{})

func (*Logger) IsLevelEnabled

func (logger *Logger) IsLevelEnabled(level level.Level) bool

IsLevelEnabled checks if the log level of the logger is greater than the level param

func (*Logger) Kernel

func (logger *Logger) Kernel() *logrus.Logger

func (*Logger) Log

func (logger *Logger) Log(level level.Level, args ...interface{})

func (*Logger) Logf

func (logger *Logger) Logf(level level.Level, format string, args ...interface{})

func (*Logger) Logln

func (logger *Logger) Logln(level level.Level, args ...interface{})

func (*Logger) Panic

func (logger *Logger) Panic(args ...interface{})

func (*Logger) Panicf

func (logger *Logger) Panicf(format string, args ...interface{})

func (*Logger) Panicln

func (logger *Logger) Panicln(args ...interface{})

func (*Logger) Print

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

func (*Logger) Printf

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

func (*Logger) Println

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

func (*Logger) ReplaceHooks

func (logger *Logger) ReplaceHooks(hooks hook.LevelHooks) hook.LevelHooks

ReplaceHooks replaces the logger hook and returns the old ones

func (*Logger) SetFormatter

func (logger *Logger) SetFormatter(formatter format.Formatter) *Logger

SetFormatter sets the logger format.

func (*Logger) SetLevel

func (logger *Logger) SetLevel(level level.Level) *Logger

SetLevel sets the logger level.

func (*Logger) SetNoLock

func (logger *Logger) SetNoLock() *Logger

SetNoLock When file is opened with appending mode, it's safe to write concurrently to a file (within 4k message on Linux). In these cases user can choose to disable the lock.

func (*Logger) SetOutput

func (logger *Logger) SetOutput(output io.Writer) *Logger

SetOutput sets the logger output.

func (*Logger) Trace

func (logger *Logger) Trace(args ...interface{})

func (*Logger) Tracef

func (logger *Logger) Tracef(format string, args ...interface{})

func (*Logger) Traceln

func (logger *Logger) Traceln(args ...interface{})

func (*Logger) Warn

func (logger *Logger) Warn(args ...interface{})

func (*Logger) Warnf

func (logger *Logger) Warnf(format string, args ...interface{})

func (*Logger) Warning

func (logger *Logger) Warning(args ...interface{})

func (*Logger) Warningf

func (logger *Logger) Warningf(format string, args ...interface{})

func (*Logger) Warningln

func (logger *Logger) Warningln(args ...interface{})

func (*Logger) Warnln

func (logger *Logger) Warnln(args ...interface{})

func (*Logger) WithContext

func (logger *Logger) WithContext(ctx context.Context) *Entry

func (*Logger) WithError

func (logger *Logger) WithError(err error) *Entry

func (*Logger) WithField

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

func (*Logger) WithFields

func (logger *Logger) WithFields(fields field.Fields) *Entry

type Option

type Option func(config *Config)

func WithAppName

func WithAppName(appName string) Option

func WithErrFile

func WithErrFile(errFile string) Option

func WithFile

func WithFile(file string) Option

func WithFormat

func WithFormat(format string) Option

func WithLevel

func WithLevel(level string) Option

func WithRotateFile

func WithRotateFile(rotateFile RotateFileConfig) Option

func WithTimestamp

func WithTimestamp(timestamp bool) Option

type RotateFileConfig

type RotateFileConfig struct {
	File string `mapstructure:"file" json:"file"`
	// RotateTime Log cutting interval
	// 日志切割时间间隔
	RotateTime time.Duration `mapstructure:"rotate_time" json:"rotate_time"`
	// ExpireTime Maximum db time of log files
	// 日志文件最大保存时间
	ExpireTime time.Duration `mapstructure:"expire_time" json:"expire_time"`
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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