logging

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2024 License: LGPL-3.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var LogLevels = map[string]zapcore.Level{
	"trace":     zapcore.DebugLevel,
	"debug":     zapcore.DebugLevel,
	"info":      zapcore.InfoLevel,
	"notice":    zapcore.InfoLevel,
	"warn":      zapcore.WarnLevel,
	"warning":   zapcore.WarnLevel,
	"err":       zapcore.ErrorLevel,
	"error":     zapcore.ErrorLevel,
	"dpanic":    zapcore.DPanicLevel,
	"panic":     zapcore.PanicLevel,
	"fatal":     zapcore.FatalLevel,
	"critical":  zapcore.FatalLevel,
	"emergency": zapcore.FatalLevel,
	"invalid":   zapcore.InvalidLevel,
}

LogLevels 日志等级

Functions

func Debug

func Debug(args ...any)

Debug calls the default logger's Debug method.

func Debugf

func Debugf(format string, args ...any)

Debugf calls the default logger's Debugf method.

func Debugw

func Debugw(msg string, keysAndValues ...any)

Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Error

func Error(args ...any)

Error calls the default logger's Error method.

func Errorf

func Errorf(format string, args ...any)

Errorf calls the default logger's Errorf method.

func Errorw

func Errorw(msg string, keysAndValues ...any)

Errorw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Fatal

func Fatal(args ...any)

Fatal calls the default logger's Fatal method and then os.Exit(1).

func Fatalf

func Fatalf(format string, args ...any)

Fatalf calls the default logger's Fatalf method and then os.Exit(1).

func Fatalw

func Fatalw(msg string, keysAndValues ...any)

Fatalw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func FileWithLineNum

func FileWithLineNum() string

FileWithLineNum return the file name and line number of the current file

func FormatWeek

func FormatWeek(t time.Time) string

FormatWeek format time as the week of the year

func GetAbsPath

func GetAbsPath(file string, onlyFile bool) (path string, err error)

GetAbsPath 使用真实的绝对路径

func GetLevelEnabler

func GetLevelEnabler(start, stop, min string) zapcore.LevelEnabler

GetLevelEnabler 级别过滤

func GetLogPath

func GetLogPath(dir string, files []string) []string

GetLogPath 使用绝对路径

func GetZapLevel

func GetZapLevel(lvl string) (string, zapcore.Level)

GetZapLevel 转为zap的Level

func Info

func Info(args ...any)

Info calls the default logger's Info method.

func Infof

func Infof(format string, args ...any)

Infof calls the default logger's Infof method.

func Infow

func Infow(msg string, keysAndValues ...any)

Infow logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func NewEncoderConfig

func NewEncoderConfig(timeFormat, levelFormat string) zapcore.EncoderConfig

func NewLogger

func NewLogger(dir string) *zap.SugaredLogger

NewLogger 指定日志目录,普通和错误日志分文件存放

func NewLoggerCustom

func NewLoggerCustom(cfg *LogConfig, dir string) *zap.SugaredLogger

NewLoggerCustom 根据配置产生记录器

func NewLoggerURL

func NewLoggerURL(level, url string) *zap.SugaredLogger

NewLoggerURL 单路径日志,指定日志级别和路径

func Panic

func Panic(args ...any)

Panic calls the default logger's Panic method.

func Panicf

func Panicf(format string, args ...any)

Panicf calls the default logger's Tracef method.

func Panicw

func Panicw(msg string, keysAndValues ...any)

Panicw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func ReplaceCores

func ReplaceCores(cores []zapcore.Core) zap.Option

ReplaceCores 替换为多种输出的Core

func SetLogger

func SetLogger(l *zap.SugaredLogger)

SetLogger sets the default logger and the system defaultLogger. Note that this method is not concurrent-safe and must not be called after the use of DefaultLogger and global functions privateLog this package.

func SetLoggerDir

func SetLoggerDir(dir string)

SetLoggerDir sets the default logger in the dir

func Trace

func Trace(args ...any)

Trace calls the default logger's Trace method.

func Tracef

func Tracef(format string, args ...any)

Tracef calls the default logger's Tracef method.

func Tracew

func Tracew(msg string, keysAndValues ...any)

Tracew logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func Warn

func Warn(args ...any)

Warn calls the default logger's Warn method.

func Warnf

func Warnf(format string, args ...any)

Warnf calls the default logger's Warnf method.

func Warnw

func Warnw(msg string, keysAndValues ...any)

Warnw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.

func WithContext

func WithContext(_ context.Context) *zap.SugaredLogger

WithContext return the defaultLogger

Types

type LogConfig

type LogConfig struct {
	zap.Config
	MinLevel   string
	LevelCase  string
	TimeFormat string
	Outputs    []Output
}

LogConfig 日志配置

func DefaultConfig

func DefaultConfig() *LogConfig

DefaultConfig 默认配置,使用两个文件分别记录警告和错误

func SingleFileConfig

func SingleFileConfig(level, file string) *LogConfig

SingleFileConfig 使用单个文件的记录器

func (*LogConfig) BuildCores

func (c *LogConfig) BuildCores(dir string) []zapcore.Core

BuildCores 产生记录器内核

func (*LogConfig) BuildLevel

func (c *LogConfig) BuildLevel() zap.AtomicLevel

BuildLevel 当前日志的最低级别

func (*LogConfig) BuildLogger

func (c *LogConfig) BuildLogger(dir string, opts ...zap.Option) (*zap.Logger, error)

BuildLogger 生成日志记录器

func (*LogConfig) GetEncoder

func (c *LogConfig) GetEncoder() zapcore.Encoder

GetEncoder 根据编码配置设置日志格式

func (*LogConfig) IsNop

func (c *LogConfig) IsNop() bool

IsNop 是否空日志

type Output

type Output struct {
	Start, Stop string
	OutPaths    []string
}

Output 输出配置

type RotateFile

type RotateFile struct {
	// Filename is the file to write logs to.  Backup log files will be retained
	// in the same directory.  It uses <processname>-lumberjack.log in
	// os.TempDir() if empty.
	Filename string `json:"filename" yaml:"filename"`

	// MaxSize is the maximum size in megabytes of the log file before it gets
	// rotated. It defaults to 100 megabytes.
	MaxSize int `json:"maxsize" yaml:"maxsize" form:"size"`

	// Cycle time cycle, one of daily/weekly/monthly
	Cycle string `json:"cycle" yaml:"cycle" form:"cycle"`

	// Minutely minute cycle, from 1 to 720 (half day)
	Minutely int `json:"minutely" yaml:"minutely" form:"min"`

	// MaxAge is the maximum number of days to retain old log files based on the
	// timestamp encoded in their filename.  Note that a day is defined as 24
	// hours and may not exactly correspond to calendar days due to daylight
	// savings, leap seconds, etc. The default is not to remove old log files
	// based on age.
	MaxAge int `json:"maxage" yaml:"maxage" form:"age"`

	// MaxBackups is the maximum number of old log files to retain.  The default
	// is to retain all old log files (though MaxAge may still cause them to get
	// deleted.)
	MaxBackups int `json:"maxbackups" yaml:"maxbackups" form:"bak"`

	// LocalTime determines if the time used for formatting the timestamps in
	// backup files is the computer's local time.  The default is to use UTC
	// time.
	LocalTime bool `json:"localtime" yaml:"localtime" form:"local"`

	// Compress determines if the rotated log files should be compressed
	// using gzip. The default is not to perform compression.
	Compress bool `json:"compress" yaml:"compress" form:"comp"`
	// contains filtered or unexported fields
}

RotateFile is an io.WriteCloser that writes to the specified filename. RotateFile opens or creates the logfile on first Write. If the file exists and is less than MaxSize megabytes, lumberjack will open and append to that file. If the file exists and its size is >= MaxSize megabytes, the file is renamed by putting the current time in a timestamp in the name immediately before the file's extension (or the end of the filename if there's no extension). A new log file is then created using original filename.

Whenever a write would cause the current log file exceed MaxSize megabytes, the current file is closed, renamed, and a new log file created with the original name. Thus, the filename you give RotateFile is always the "current" log file.

Backups use the log file name given to RotateFile, in the form `name-timestamp.ext` where name is the filename without the extension, timestamp is the time at which the log was rotated formatted with the time.Time format of `2006-01-02T15-04-05.000` and the extension is the original extension. For example, if your RotateFile.Filename is `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log`

Cleaning Up Old Log Files

Whenever a new logfile gets created, old log files may be deleted. The most recent files according to the encoded timestamp will be retained, up to a number equal to MaxBackups (or all of them if MaxBackups is 0). Any files with an encoded timestamp older than MaxAge days are deleted, regardless of MaxBackups. Note that the time encoded in the timestamp is the rotation time, which may differ from the last time that file was written to.

If MaxBackups and MaxAge are both 0, no old log files will be deleted.

func (*RotateFile) Close

func (l *RotateFile) Close() error

Close implements io.Closer, and closes the current logfile.

func (*RotateFile) Rotate

func (l *RotateFile) Rotate() error

Rotate causes RotateFile to close the existing log file and immediately create a new one. This is a helper function for applications that want to initiate rotations outside of the normal rotation rules, such as in response to SIGHUP. After rotating, this initiates compression and removal of old log files according to the configuration.

func (*RotateFile) Sync

func (l *RotateFile) Sync() error

Sync implements zap.Sink.SyncWriter

func (*RotateFile) Write

func (l *RotateFile) Write(p []byte) (n int, err error)

Write implements io.Writer. If a write would cause the log file to be larger than MaxSize, the file is closed, renamed to include a timestamp of the current time, and a new log file is created using the original log file name. If the length of the write is greater than MaxSize, an error is returned.

Directories

Path Synopsis
adapters

Jump to

Keyboard shortcuts

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