Documentation ¶
Index ¶
- type Config
- type Encoder
- type FileLogConfig
- type Logger
- func (l *Logger) Debug(msg string, optfields ...log.LogField)
- func (l *Logger) Error(msg string, err error, optfields ...log.LogField)
- func (l *Logger) Fatal(msg string, err error, optfields ...log.LogField)
- func (l *Logger) Info(msg string, optfields ...log.LogField)
- func (l *Logger) Sync() error
- func (l *Logger) Warn(msg string, optfields ...log.LogField)
- func (l *Logger) WithContext(ctx context.Context) log.Logger
- func (l *Logger) WithOptions(opts ...zap.Option) *Logger
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Config ¶
type Config struct { // RootLogLevel define root log level to use, any log lower than this will not be logged. // Default to InfoLogLevel. RootLogLevel log.LogLevel // Encoder to use to log default to ConsoleEncoder. Encoder Encoder // FileLogConfig configuration for rolling file log. FileLogConfig FileLogConfig // Output set log output for console. Defaults to os.Stdout. Output io.Writer }
Config is configuration for log. Any underlying log framework should comply with this spec. By defaults this configuration specifies console logger. See FileLogConfig for file based log configurations. Console log cannot be disabled.
type FileLogConfig ¶
type FileLogConfig struct { // Enabled wether to log to a file or not. Enabled bool // LogLevel to use when logging to a file, any log lower than this will not be logged. // Default to use InfoLogLevel LogLevel log.LogLevel // Encoder to use when loging to file. Default to JSONEncoder. Encoder Encoder // Filename is the file to write logs to. Backup log files will be retained in the same directory. Filename string // MaxSize is the maximum size in megabytes of the log file before it gets rotated. Default to 100 megabytes. MaxSize int // MaxBackups is the maximum number of old log files to retain. The default is to retain all old log files. MaxBackups int }
FileLogConfig define configurations for rolling file log.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
func New ¶
New create new instance of Logger.
Example ¶
package main import ( "github.com/hexastack-dev/devkit-go/log" "github.com/hexastack-dev/devkit-go/log/drivers/zaplog" ) func main() { config := zaplog.Config{ RootLogLevel: log.DebugLogLevel, FileLogConfig: zaplog.FileLogConfig{ Enabled: true, Filename: "./log/app.log", }, } zlog := zaplog.NewDefaultLogger(config) defer zlog.Sync() zlog.Info("Hello") v1 := log.Field("v1", "V1") zlog.Info("World", v1) }
Output:
func NewDefaultLogger ¶
NewDefaultLogger create new instance of Zap Logger using default configuration.
func (*Logger) Debug ¶
Debug logs a message at DebugLevel. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func (*Logger) Error ¶
Error logs a message at ErrorLevel, put the passed error in "error" field. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func (*Logger) Fatal ¶
Fatal logs a message at FatalLevel, then calls os.Exit(1). This should only be use with extra care, ideally Fatal should only be used in main where the apps encountered an error and have noway to continue. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func (*Logger) Info ¶
Info logs a message at InfoLevel. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func (*Logger) Sync ¶
Sync will calls zap logger Sync(), this method should be called before the program exit.
ie. func main() { ... zlog := New(conf) defer zlog.Sync() ... }
func (*Logger) Warn ¶
Warn logs a message at WarnLevel. optfields is optional, when supplied it will be added as new field using Key as field name, and Value as it's value.
func (*Logger) WithContext ¶
WithContext return Logger instance that will use passed context to log additional info, such as opentelemetry's SpanID and TraceID if applicable.