Documentation ¶
Index ¶
- Constants
- Variables
- func AddWriteSyncer(ws zapcore.WriteSyncer)
- func Debug(msg string, fields ...zap.Field)
- func Debugf(template string, args ...interface{})
- func DefaultRotateOption(name string, local bool) string
- func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
- func Error(msg string, fields ...zap.Field)
- func Errorf(template string, args ...interface{})
- func Fatal(msg string, fields ...zap.Field)
- func Fatalf(template string, args ...interface{})
- func Info(msg string, fields ...zap.Field)
- func Infof(template string, args ...interface{})
- func InitFileLogger(fileName, level, format string, maxSize, maxDays, maxBackups int, ...) (*Logger, *ZapProperties, error)
- func InitFileLoggerWithDefault(fileName string) (*Logger, *ZapProperties, error)
- func InitLoggerWithConfig(cfg *Config) (*Logger, *ZapProperties, error)
- func InitStdoutLogger(level, format string) (*Logger, *ZapProperties, error)
- func InitStdoutLoggerWithDefault() (*Logger, *ZapProperties, error)
- func NewLogger() (*Logger, *ZapProperties, error)
- func NewMultiWriteSyncer(ws ...zapcore.WriteSyncer) zapcore.WriteSyncer
- func NewStdoutLogger(level, format string) (*Logger, *ZapProperties, error)
- func NewStdoutWriteSyncer() zapcore.WriteSyncer
- func NewTextCore(enc *textEncoder, ws zapcore.WriteSyncer, enab zapcore.LevelEnabler) zapcore.Core
- func NewTextEncoder(cfg *Config) zapcore.Encoder
- func NewWriteSyncer(w io.Writer) zapcore.WriteSyncer
- func Panic(msg string, fields ...zap.Field)
- func Panicf(template string, args ...interface{})
- func ReplaceGlobals(logger *Logger, props *ZapProperties)
- func Rotate() error
- func S() *zap.SugaredLogger
- func SetDisableDoubleQuotes(disableDoubleQuotes bool)
- func SetDisableEscape(disableEscape bool)
- func SetLevel(l Level)
- func SetSeperator(seperator string)
- func SetTimeFormat(timeFormat string)
- func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
- func StringToLogFormatter(format string, disableTimestamp bool) logrus.Formatter
- func Warn(msg string, fields ...zap.Field)
- func Warnf(template string, args ...interface{})
- func With(fields ...zap.Field) *zap.Logger
- type Config
- type FileLogConfig
- func NewEmptyFileLogConfig() *FileLogConfig
- func NewFileLogConfig(fileName string, maxSize, maxDays, maxBackups int, options ...Option) (fileLogConfig *FileLogConfig, err error)
- func NewFileLogConfigWithDefaultFileName(fileName string, maxSize, maxDays, maxBackups int) (fileLogConfig *FileLogConfig, err error)
- type Level
- type Logger
- func (logger *Logger) AddWriteSyncer(ws zapcore.WriteSyncer)
- func (logger *Logger) Clone() *Logger
- func (logger *Logger) CloneAndAddWriteSyncer(ws zapcore.WriteSyncer) *Logger
- func (logger *Logger) Debug(msg string, fields ...zap.Field)
- func (logger *Logger) Debugf(template string, args ...interface{})
- func (logger *Logger) Error(msg string, fields ...zap.Field)
- func (logger *Logger) Errorf(template string, args ...interface{})
- func (logger *Logger) Fatal(msg string, fields ...zap.Field)
- func (logger *Logger) Fatalf(template string, args ...interface{})
- func (logger *Logger) Info(msg string, fields ...zap.Field)
- func (logger *Logger) Infof(template string, args ...interface{})
- func (logger *Logger) Panic(msg string, fields ...zap.Field)
- func (logger *Logger) Panicf(template string, args ...interface{})
- func (logger *Logger) Rotate() error
- func (logger *Logger) SetDisableDoubleQuotes(disableDoubleQuotes bool)
- func (logger *Logger) SetDisableEscape(disableEscape bool)
- func (logger *Logger) SetSeperator(seperator string)
- func (logger *Logger) SetTimeFormat(timeFormat string)
- func (logger *Logger) Sugar() *zap.SugaredLogger
- func (logger *Logger) Warn(msg string, fields ...zap.Field)
- func (logger *Logger) Warnf(template string, args ...interface{})
- func (logger *Logger) WithOptions(opts ...zap.Option) *Logger
- type MultiWriteSyncer
- type Option
- type WriteSyncer
- type Writer
- type ZapProperties
Constants ¶
const ( // default log file name DefaultLogFileName = "run.log" // default maximum log file size DefaultLogMaxSize = 100 // MB // default maximum log file backup days DefaultLogMaxDays = 7 // default maximum log file backup number DefaultLogMaxBackups = 5 // default log format in string DefaultLogFormat = "text" // default log level in string DefaultLogLevel = "info" DefaultDisableTimestamp = false DefaultDisableDoubleQuotes = false DefaultDisableEscape = false )
const ( DebugLevel = zap.DebugLevel InfoLevel = zap.InfoLevel WarnLevel = zap.WarnLevel ErrorLevel = zap.ErrorLevel DPanicLevel = zap.DPanicLevel PanicLevel = zap.PanicLevel FatalLevel = zap.FatalLevel )
const ( TimeFormatMicroSecond = "2006-01-02 15:04:05.000000" TimeFormatMilliSecond = "2006-01-02 15:04:05.000" DefaultLogSeparator = "->" )
const (
DefaultCallerSkip = 1
)
const DefaultOutput = "stdout"
Variables ¶
var ( ErrEmptyLogFileName = "Log file name could NOT be an empty string." ErrNotValidLogFileName = "Log file name must be either unix or windows path format, %s is not valid." )
var ( MyLogger *Logger MyProps *ZapProperties )
var (
// Get retrieves a buffer from the pool, creating one if necessary.
Get = _pool.Get
)
Functions ¶
func AddWriteSyncer ¶ added in v1.0.12
func AddWriteSyncer(ws zapcore.WriteSyncer)
AddWriteSyncer add write syncer to multi write syncer, which allows to add a new way to write log message
func Debug ¶
Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func Debugf ¶
func Debugf(template string, args ...interface{})
Debugf uses fmt.Sprintf to log a templated message.
func DefaultRotateOption ¶ added in v1.0.23
func DefaultTimeEncoder ¶
func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
DefaultTimeEncoder serializes time.Time to a human-readable formatted string
func Error ¶
Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func Errorf ¶
func Errorf(template string, args ...interface{})
Errorf uses fmt.Sprintf to log a templated message.
func Fatal ¶
Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
func Fatalf ¶
func Fatalf(template string, args ...interface{})
Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func Info ¶
Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func Infof ¶
func Infof(template string, args ...interface{})
Infof uses fmt.Sprintf to log a templated message.
func InitFileLogger ¶ added in v1.0.19
func InitFileLogger(fileName, level, format string, maxSize, maxDays, maxBackups int, options ...Option) (*Logger, *ZapProperties, error)
InitFileLogger initiates a file logger with given options
func InitFileLoggerWithDefault ¶ added in v1.0.20
func InitFileLoggerWithDefault(fileName string) (*Logger, *ZapProperties, error)
InitFileLoggerWithDefaultConfig initiates logger with default options
func InitLoggerWithConfig ¶ added in v1.0.2
func InitLoggerWithConfig(cfg *Config) (*Logger, *ZapProperties, error)
InitLoggerWithConfig initializes a zap logger with config.
func InitStdoutLogger ¶ added in v1.0.19
func InitStdoutLogger(level, format string) (*Logger, *ZapProperties, error)
InitStdoutLogger initiates a stdout logger with given level and format
func InitStdoutLoggerWithDefault ¶ added in v1.0.20
func InitStdoutLoggerWithDefault() (*Logger, *ZapProperties, error)
InitStdoutLogger initiates a stdout logger with given level and format
func NewLogger ¶ added in v1.0.12
func NewLogger() (*Logger, *ZapProperties, error)
NewLogger returns a logger which will write log message to stdout with default log level and format
func NewMultiWriteSyncer ¶ added in v1.0.12
func NewMultiWriteSyncer(ws ...zapcore.WriteSyncer) zapcore.WriteSyncer
NewMultiWriteSyncer creates a WriteSyncer that duplicates its writes and sync calls, much like io.MultiWriter.
func NewStdoutLogger ¶ added in v1.0.19
func NewStdoutLogger(level, format string) (*Logger, *ZapProperties, error)
NewStdoutLogger returns a logger which will write log message to stdout
func NewStdoutWriteSyncer ¶ added in v1.0.12
func NewStdoutWriteSyncer() zapcore.WriteSyncer
NewStdoutWriteSyncer returns a zapcore.WriteSyncer using os.Stdout
func NewTextCore ¶
func NewTextCore(enc *textEncoder, ws zapcore.WriteSyncer, enab zapcore.LevelEnabler) zapcore.Core
NewTextCore creates a Core that writes logs to a WriteSyncer.
func NewTextEncoder ¶
NewTextEncoder creates a fast, low-allocation Text encoder. The encoder appropriately escapes all field keys and values.
func NewWriteSyncer ¶ added in v1.0.12
func NewWriteSyncer(w io.Writer) zapcore.WriteSyncer
NewWriteSyncer returns a new zapcore.WriteSyncer
func Panic ¶
Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
The logger then panics, even if logging at PanicLevel is disabled.
func Panicf ¶
func Panicf(template string, args ...interface{})
Panicf uses fmt.Sprintf to log a templated message, then panics.
func ReplaceGlobals ¶
func ReplaceGlobals(logger *Logger, props *ZapProperties)
ReplaceGlobals replaces global logger with given logger and properties
func S ¶
func S() *zap.SugaredLogger
S returns the global SugaredLogger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.
func SetDisableDoubleQuotes ¶ added in v1.0.10
func SetDisableDoubleQuotes(disableDoubleQuotes bool)
SetDisableDoubleQuotes disables wrapping log content with double quotes of global logger
func SetDisableEscape ¶ added in v1.0.11
func SetDisableEscape(disableEscape bool)
SetDisableEscape disables wrapping log content with double quotes of global logger
func SetSeperator ¶ added in v1.0.30
func SetSeperator(seperator string)
SetSeperator sets the seperator of global logger
func SetTimeFormat ¶ added in v1.0.30
func SetTimeFormat(timeFormat string)
SetTimeFormat sets the time format of global logger
func ShortCallerEncoder ¶
func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
ShortCallerEncoder serializes a caller in file:line format.
func StringToLogFormatter ¶ added in v1.0.2
func Warn ¶
Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
Types ¶
type Config ¶
type Config struct { // Log level. Level string `yaml:"level" json:"level"` // Log format. one of json, text, or console. Format string `yaml:"format" json:"format"` // Disable automatic timestamps in output. DisableTimestamp bool `yaml:"disable-timestamp" json:"disable-timestamp"` // File log config. File FileLogConfig `yaml:"file" json:"file"` // Development puts the logger in development mode, which changes the // behavior of DPanicLevel and takes stacktraces more liberally. Development bool `yaml:"development" json:"development"` // DisableDoubleQuote disables adding double-quotes to log entry DisableDoubleQuotes bool // DisableEscape disables escaping special characters like \n,\r... DisableEscape bool // DisableCaller stops annotating logs with the calling function's file // name and line number. By default, all logs are annotated. DisableCaller bool `yaml:"disable-caller" json:"disable-caller"` // DisableStacktrace completely disables automatic stacktrace capturing. By // default, stacktraces are captured for WarnLevel and above logs in // development and ErrorLevel and above in production. DisableStacktrace bool `yaml:"disable-stacktrace" json:"disable-stacktrace"` // DisableErrorVerbose stops annotating logs with the full verbose error // message. DisableErrorVerbose bool `yaml:"disable-error-verbose" json:"disable-error-verbose"` // SamplingConfig sets a sampling strategy for the logger. Sampling caps the // global CPU and I/O load that logging puts on your process while attempting // to preserve a representative subset of your logs. // // Values configured here are per-second. See zapcore.NewSampler for details. Sampling *zap.SamplingConfig `yaml:"sampling" json:"sampling"` }
Config serializes log related config in yaml/json.
func NewConfig ¶
func NewConfig(level, format string, fileCfg FileLogConfig) *Config
NewConfig creates a Config.
func NewConfigWithFileLog ¶ added in v1.0.2
func NewConfigWithFileLog(fileName, level, format string, maxSize, maxDays, maxBackups int, options ...Option) (*Config, error)
NewConfigWithFileLog returns a *Config with file options
func NewConfigWithStdout ¶ added in v1.0.19
NewConfigWithStdout returns a *Config with given level and format
func (*Config) SetDisableDoubleQuotes ¶ added in v1.0.10
SetDisableDoubleQuotes disables wrapping log content with double quotes
func (*Config) SetDisableEscape ¶ added in v1.0.11
SetDisableEscape disables escaping special characters of log content like \n,\r...
type FileLogConfig ¶
type FileLogConfig struct { FileName string MaxSize int MaxDays int MaxBackups int Options []Option }
FileLogConfig serializes file log related config in yaml/json.
func NewEmptyFileLogConfig ¶ added in v1.0.19
func NewEmptyFileLogConfig() *FileLogConfig
NewEmptyFileLogConfig returns an empty *FileLogConfig
func NewFileLogConfig ¶
func NewFileLogConfig(fileName string, maxSize, maxDays, maxBackups int, options ...Option) (fileLogConfig *FileLogConfig, err error)
NewFileLogConfig creates a FileLogConfig.
func NewFileLogConfigWithDefaultFileName ¶ added in v1.0.3
func NewFileLogConfigWithDefaultFileName(fileName string, maxSize, maxDays, maxBackups int) (fileLogConfig *FileLogConfig, err error)
NewFileLogConfigWithDefaultFileName creates a FileLogConfig, if fileName is empty, it will use default file name.
type Level ¶ added in v1.0.1
func ConvertToLevel ¶ added in v1.0.28
type Logger ¶ added in v1.0.2
type Logger struct { SugaredLogger *zap.SugaredLogger // contains filtered or unexported fields }
Logger wraps zap logger and sugared logger
func CloneAndAddWriteSyncer ¶ added in v1.0.13
func CloneAndAddWriteSyncer(ws zapcore.WriteSyncer) *Logger
CloneAndAddWriteSyncer clones global logger and add specified write syncer to it
func CloneLogger ¶ added in v1.0.13
CloneLogger returns a fresh new logger with same options
func CloneStdoutLogger ¶ added in v1.0.16
func CloneStdoutLogger() *Logger
CloneStdoutLogger clones global logger and add stdout write syncer to it
func L ¶
func L() *Logger
L returns the global Logger, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.
func NewMyLogger ¶ added in v1.0.2
NewMyLogger returns *Logger
func (*Logger) AddWriteSyncer ¶ added in v1.0.12
func (logger *Logger) AddWriteSyncer(ws zapcore.WriteSyncer)
AddWriteSyncer adds write syncer to multi write syncer, which allows to add a new way to write log message
func (*Logger) CloneAndAddWriteSyncer ¶ added in v1.0.13
func (logger *Logger) CloneAndAddWriteSyncer(ws zapcore.WriteSyncer) *Logger
CloneAndAddWriteSyncer adds write syncer to multi write syncer, which allows to add a new way to write log message
func (*Logger) Debug ¶ added in v1.0.9
Debug logs a message at DebugLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func (*Logger) Error ¶ added in v1.0.9
Error logs a message at ErrorLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func (*Logger) Fatal ¶ added in v1.0.9
Fatal logs a message at FatalLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
The logger then calls os.Exit(1), even if logging at FatalLevel is disabled.
func (*Logger) Fatalf ¶ added in v1.0.2
Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.
func (*Logger) Info ¶ added in v1.0.9
Info logs a message at InfoLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
func (*Logger) Panic ¶ added in v1.0.9
Panic logs a message at PanicLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
The logger then panics, even if logging at PanicLevel is disabled.
func (*Logger) Panicf ¶ added in v1.0.2
Panicf uses fmt.Sprintf to log a templated message, then panics.
func (*Logger) SetDisableDoubleQuotes ¶ added in v1.0.10
SetDisableDoubleQuotes disables wrapping log content with double quotes
func (*Logger) SetDisableEscape ¶ added in v1.0.11
SetDisableEscape disables escaping special characters of log content like \n,\r...
func (*Logger) SetSeperator ¶ added in v1.0.30
SetSeperator sets the seperator to log message
func (*Logger) SetTimeFormat ¶ added in v1.0.30
SetTimeFormat sets the time format of log message
func (*Logger) Sugar ¶ added in v1.0.9
func (logger *Logger) Sugar() *zap.SugaredLogger
Sugar returns a new sugared logger
func (*Logger) Warn ¶ added in v1.0.9
Warn logs a message at WarnLevel. The message includes any fields passed at the log site, as well as any fields accumulated on the logger.
type MultiWriteSyncer ¶ added in v1.0.12
type MultiWriteSyncer []zapcore.WriteSyncer
func (MultiWriteSyncer) List ¶ added in v1.0.12
func (ws MultiWriteSyncer) List() []zapcore.WriteSyncer
List lists write syncer of MultiWriteSyncer.
func (MultiWriteSyncer) Sync ¶ added in v1.0.12
func (ws MultiWriteSyncer) Sync() error
func (MultiWriteSyncer) Write ¶ added in v1.0.12
func (ws MultiWriteSyncer) Write(p []byte) (int, error)
See https://golang.org/src/io/multi.go When not all underlying syncers write the same number of bytes, the smallest number is returned even though Write() is called on all of them.
type WriteSyncer ¶ added in v1.0.23
func (WriteSyncer) GetWriteSyncer ¶ added in v1.0.23
func (ws WriteSyncer) GetWriteSyncer() zapcore.WriteSyncer
GetWriteSyncer returns the underlying WriteSyncer of the WriteSyncer
func (WriteSyncer) GetWriter ¶ added in v1.0.23
func (ws WriteSyncer) GetWriter() io.Writer
GetWriter returns the underlying writer of the WriteSyncer
func (WriteSyncer) Sync ¶ added in v1.0.23
func (ws WriteSyncer) Sync() error
Sync implements zapcore.WriteSyncer
type Writer ¶ added in v1.0.23
type Writer 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"` // 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"` // 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"` // 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"` // 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"` // Options is an optional function slices that returns the backup file name, // note that, only the first option will be applied. Options []Option // contains filtered or unexported fields }
Writer is an io.WriteCloser that writes to the specified filename.
Writer 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 Writer is always the "current" log file.
Backups use the log file name given to Writer, 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 Writer.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 InitLumberjackLoggerWithFileLogConfig ¶ added in v1.0.20
func InitLumberjackLoggerWithFileLogConfig(cfg *FileLogConfig) (*Writer, error)
InitLumberjackLoggerWithFileLogConfig initializes file based logging options.
func (*Writer) Rotate ¶ added in v1.0.23
Rotate causes Writer 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 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 (*Writer) Write ¶ added in v1.0.23
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.
type ZapProperties ¶
type ZapProperties struct { Core zapcore.Core Syncer zapcore.WriteSyncer Level zap.AtomicLevel }
ZapProperties records some information about zap
func InitZapLoggerWithWriteSyncer ¶ added in v1.0.20
func InitZapLoggerWithWriteSyncer(cfg *Config, output zapcore.WriteSyncer, opts ...zap.Option) (*zap.Logger, *ZapProperties, error)
InitZapLoggerWithWriteSyncer initializes a zap logger with specified write syncer.
func P ¶ added in v1.0.25
func P() *ZapProperties
P returns the global properties, which can be reconfigured with ReplaceGlobals. It's safe for concurrent use.
func (*ZapProperties) Clone ¶ added in v1.0.20
func (props *ZapProperties) Clone() *ZapProperties
Clone returns a fresh new *ZapProperties with same options, note that it will use the same syncer
func (*ZapProperties) SetCore ¶ added in v1.0.20
func (props *ZapProperties) SetCore(core zapcore.Core)
SetCore sets the core
func (*ZapProperties) WithCore ¶ added in v1.0.20
func (props *ZapProperties) WithCore(core zapcore.Core) *ZapProperties
WithCore returns a fresh new *ZapProperties with given core