zaplog

package module
v0.0.0-...-eb33cdb Latest Latest
Warning

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

Go to latest
Published: Feb 27, 2023 License: Apache-2.0 Imports: 9 Imported by: 0

README

zaplog-driver

github.com/hexastack-dev/devkit-go/log/drivers/zaplog implementation using zap and lumberjack as logging framework.

Documentation

Index

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 Encoder

type Encoder uint8
const (
	JSONEncoder Encoder = iota
	ConsoleEncoder
)

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

func New(zapLogger *zap.Logger) *Logger

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

func NewDefaultLogger(config Config) *Logger

NewDefaultLogger create new instance of Zap Logger using default configuration.

func (*Logger) Debug

func (l *Logger) Debug(msg string, optfields ...log.LogField)

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

func (l *Logger) Error(msg string, err error, optfields ...log.LogField)

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

func (l *Logger) Fatal(msg string, err error, optfields ...log.LogField)

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

func (l *Logger) Info(msg string, optfields ...log.LogField)

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

func (l *Logger) Sync() error

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

func (l *Logger) Warn(msg string, optfields ...log.LogField)

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

func (l *Logger) WithContext(ctx context.Context) log.Logger

WithContext return Logger instance that will use passed context to log additional info, such as opentelemetry's SpanID and TraceID if applicable.

func (*Logger) WithOptions

func (l *Logger) WithOptions(opts ...zap.Option) *Logger

Jump to

Keyboard shortcuts

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