qslogger

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	EnvLogLevel = "LOG_LEVEL"
)

Variables

View Source
var (
	ErrInvalidFormat = errors.New("invalid log format provided")
)

Functions

func BuildConfigs

func BuildConfigs(opts *LogConfigs) *zap.Config

Helper function for quick building a zap-compatible log format

func BuildConsoleCore

func BuildConsoleCore(configs *zap.Config) zapcore.Core

Builds a zapcore.Core that logs to the console

func BuildFileCore

func BuildFileCore(configs *zap.Config, opts *FileOption) zapcore.Core

Builds a zapcore.Core that logs to a daily rotated log folder.

Logs are splitted into 1MB log files and 30 files are kept at any instance and the maximum age of a log file is 90 days. Log files are not compressed

func NewLogger

func NewLogger(format LogFormat) (*zap.Logger, error)

Creates a new instance of logger based on the provided log format.

If no log format was provided, it uses the default one and retrieve the log level through LOG_LEVEL environment variable

Create a custom format by implementing this interface:

 type LogFormat interface {
		Format() *zap.Config
		Core() zapcore.Core
 }

Example of a custom format:

type DefaultFormat struct {
 	toConsole bool
 	fileOptions *FileOption
 }

func ProvideDefaultFormat(toConsole bool, opts *FileOption) fx.Option {
	return fx.Provide(func() *DefaultFormat {
		return &DefaultFormat{
			toConsole:   toConsole,
			fileOptions: opts,
		}
	})
}

func (f *DefaultFormat) Format() *zap.Config {
	rawLevel := os.Getenv(EnvLogLevel)
	lvl := ParseLevel(rawLevel)
	return BuildConfigs(&LogConfigs{
		Encoding:       ConsoleLog,
		Level:          lvl,
		IsShortCaller:  true,
		IsColoredLevel: true,
		IsCapitalLevel: true,
		TimeFormat:  	zapcore.RFC3339Encoder,
		DurationScale:  time.Second,
		OutputPaths:    []string{"stderr"}, // to standard error
	})
}

func (f *DefaultFormat) Core() zapcore.Core {
	var listCore []zapcore.Core
	if f.toConsole {
		listCore = append(listCore, BuildConsoleCore(f.Format()))
	}
	if f.fileOptions != nil {
		listCore = append(listCore, BuildFileCore(f.Format(), f.fileOptions))
	}
	core := zapcore.NewTee(listCore...)
	return core
}

To use it, add it to a dependency list:

 var Module = fx.Options(
		qs.ProvideDefaultFormat(true, nil)
 )

func NewSugaredLogger

func NewSugaredLogger(format LogFormat) (*zap.SugaredLogger, error)

Creates a new instance of a SUGARED logger, which is more flexible but slightly slower in performance, based on the provided log format.

If no log format was provided, it uses the default one and retrieve the log level through LOG_LEVEL environment variable

Read NewLogger documentation to learn more.

func ParseLevel

func ParseLevel(level string) zapcore.Level

Parse the log level given as an input into zap types. If is invalid, it defaults to returning INFO

func ProvideLoggerConfig

func ProvideLoggerConfig(config LogFormat) fx.Option

General way to provide a LoggerConfig

Types

type DefaultFormat

type DefaultFormat struct {
	ToConsole bool

	// If non-nil, will also logs to files
	FileOptions *FileOption
}

The default format will produces a log format based on common configs and logs at a level specified through LOG_LEVEL environment variable. In the case it is not specified then the default is INFO.

func (*DefaultFormat) Core

func (f *DefaultFormat) Core() zapcore.Core

func (*DefaultFormat) Format

func (f *DefaultFormat) Format() *zap.Config

The default format has:

  • Short caller path
  • Colored capital log level
  • Log time in RFC3339 format
  • Execution duration enabled
  • Time scale in seconds

type Encoding

type Encoding = string
var (
	JsonLog    Encoding = "json"
	ConsoleLog Encoding = "console"
)

type FileOption

type FileOption struct {
	// Which folder to keeps the files
	Folder string

	// Which filename to use
	//
	// Example: filename = "logs.log"
	// -> Folder/logs-[logTime].log
	Filename string

	// In days
	MaxAge uint32

	// In MB
	MaxSize uint32

	// The number of files to keep.
	// If a file expires, it will be removed even when the backup quota remains
	MaxBackups uint32

	// Whether to compres the file
	Compress bool
}

type JSONFormat

type JSONFormat struct {
	ToConsole bool

	// If non-nil, will also logs to files
	FileOptions *FileOption
}

This formats the log into JSON strings. Can opts into saving log files into daily rotated logs and/or to console

func (*JSONFormat) Core

func (f *JSONFormat) Core() zapcore.Core

func (*JSONFormat) Format

func (f *JSONFormat) Format() *zap.Config

The JSON format has:

  • Short caller path
  • Plain lowercase log level
  • Log time in RFC3339 format
  • Execution duration enabled
  • Time scale in second

type LogConfigs

type LogConfigs struct {
	Encoding       Encoding
	Level          zapcore.Level
	IsShortCaller  bool
	IsColoredLevel bool
	IsCapitalLevel bool
	HasMessage     bool
	TimeFormat     zapcore.TimeEncoder
	DurationScale  time.Duration
	OutputPaths    []string
}

type LogFormat

type LogFormat interface {
	Format() *zap.Config

	// If a customized core (for file logging, logging to Kafka, etc.) is not needed, simply returns nil
	Core() zapcore.Core
}

To provide custom log formats, create a dependency implementing this interface

In case a customized core is not needed, make Core() simply returns nil

type LongFormat

type LongFormat struct {
	ToConsole bool

	// If non-nil, will also logs to file
	FileOptions *FileOption
}

This formats the logs such that it is as specific as possible.

func (*LongFormat) Core

func (f *LongFormat) Core() zapcore.Core

func (*LongFormat) Format

func (f *LongFormat) Format() *zap.Config

The long format has:

  • Short caller path
  • Plain capital log level
  • Log time in RFC3339 format
  • Execution duration enabled
  • Time scale in miliseconds

Jump to

Keyboard shortcuts

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