Documentation ¶
Index ¶
- Variables
- func Debug(args ...any)
- func Debugf(format string, args ...any)
- func Debugw(msg string, keysAndValues ...any)
- func Error(args ...any)
- func Errorf(format string, args ...any)
- func Errorw(msg string, keysAndValues ...any)
- func Fatal(args ...any)
- func Fatalf(format string, args ...any)
- func Fatalw(msg string, keysAndValues ...any)
- func FileWithLineNum() string
- func FormatWeek(t time.Time) string
- func GetAbsPath(file string, onlyFile bool) (path string, err error)
- func GetLevelEnabler(start, stop, min string) zapcore.LevelEnabler
- func GetLogPath(dir string, files []string) []string
- func GetZapLevel(lvl string) (string, zapcore.Level)
- func Info(args ...any)
- func Infof(format string, args ...any)
- func Infow(msg string, keysAndValues ...any)
- func NewEncoderConfig(timeFormat, levelFormat string) zapcore.EncoderConfig
- func NewLogger(dir string) *zap.SugaredLogger
- func NewLoggerCustom(cfg *LogConfig, dir string) *zap.SugaredLogger
- func NewLoggerURL(level, url string) *zap.SugaredLogger
- func Panic(args ...any)
- func Panicf(format string, args ...any)
- func Panicw(msg string, keysAndValues ...any)
- func ReplaceCores(cores []zapcore.Core) zap.Option
- func SetLogger(l *zap.SugaredLogger)
- func SetLoggerDir(dir string)
- func Trace(args ...any)
- func Tracef(format string, args ...any)
- func Tracew(msg string, keysAndValues ...any)
- func Warn(args ...any)
- func Warnf(format string, args ...any)
- func Warnw(msg string, keysAndValues ...any)
- func WithContext(_ context.Context) *zap.SugaredLogger
- type LogConfig
- type Output
- type RotateFile
Constants ¶
This section is empty.
Variables ¶
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 Debugw ¶
Debugw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func Errorw ¶
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 Fatalw ¶
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 ¶
FormatWeek format time as the week of the year
func GetAbsPath ¶
GetAbsPath 使用真实的绝对路径
func GetLevelEnabler ¶
func GetLevelEnabler(start, stop, min string) zapcore.LevelEnabler
GetLevelEnabler 级别过滤
func Infow ¶
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 NewLoggerCustom ¶
func NewLoggerCustom(cfg *LogConfig, dir string) *zap.SugaredLogger
NewLoggerCustom 根据配置产生记录器
func NewLoggerURL ¶
func NewLoggerURL(level, url string) *zap.SugaredLogger
NewLoggerURL 单路径日志,指定日志级别和路径
func Panicw ¶
Panicw logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
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 Tracew ¶
Tracew logs a message with some additional context. The variadic key-value pairs are treated as they are privateLog With.
func Warnw ¶
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 SingleFileConfig ¶
SingleFileConfig 使用单个文件的记录器
func (*LogConfig) BuildCores ¶
BuildCores 产生记录器内核
func (*LogConfig) BuildLevel ¶
func (c *LogConfig) BuildLevel() zap.AtomicLevel
BuildLevel 当前日志的最低级别
func (*LogConfig) BuildLogger ¶
BuildLogger 生成日志记录器
func (*LogConfig) GetEncoder ¶
GetEncoder 根据编码配置设置日志格式
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) 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.