zerolog

package
v1.0.0-dev.111 Latest Latest
Warning

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

Go to latest
Published: Sep 3, 2024 License: MIT Imports: 9 Imported by: 0

README

zerolog.v1

Example

package main

import (
	"context"

	"github.com/xgodev/boost/wrapper/log"
	"github.com/xgodev/boost/wrapper/log/contrib/rs/zerolog.v1"
)

func main() {
	ctx := context.Background()

	//example use zerolog
	logger := zerolog.NewLogger()

	logger = logger.WithField("main_field", "example")

	logger.Info("main method.")
	//output: 2:30PM INF main method. main_field=example

	ctx = logger.ToContext(ctx)

	foo(ctx)

	withoutContext()
}

func foo(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("foo_field", "example")
	logger.Infof("%s method.", "foo")
	//output: 2:30PM INF foo method. foo_field=example main_field=example

	ctx = logger.ToContext(ctx)
	bar(ctx)
}

func bar(ctx context.Context) {
	logger := log.FromContext(ctx)

	logger = logger.WithField("bar_field", "example")

	logger.Infof("%s method.", "bar")
	//output: 2:30PM INF bar method. bar_field=example foo_field=example main_field=example
}

func withoutContext() {
	log.Info("withoutContext method")
	//output: 2:30PM INF withoutContext method
}

default options:

option value
Formatter "TEXT"
Level "INFO"
ConsoleEnabled true
FileEnabled false
FilePath "/tmp"
FileName "application.log"
FileMaxSize 100
FileCompress true
FileMaxAge 28
ErrorFieldName "err"

The package accepts a default constructor:

logger := zerolog.NewLogger()

Or a constructor with Options:

logger := zerolog.NewLoggerWithOptions(&zerolog.Options{})

Or a constructor with multiple parameters using options functions pattern:

logger := zerolog.NewLogger(
	zerolog.WithFormatter("TEXT"),
	zerolog.WithConsoleEnabled(true),
	zerolog.WithFilePath("/tmp"),
	...
)

This is the list of all the configuration functions supported by package:

WithFormatter

sets output format of the logs. Using TEXT/JSON.

// text formatter
logger := zerolog.NewLogger(zerolog.WithFormatter("TEXT"))

// json formatter
logger := zerolog.NewLogger(zerolog.WithFormatter("JSON"))
WithLevel

sets logging level to any of these options below on the standard logger.

// log level DEBUG
logger := zerolog.NewLogger(zerolog.WithLevel("DEBUG"))

// log level WARN
logger := zerolog.NewLogger(zerolog.WithLevel("WARN"))

// log level FATAL
logger := zerolog.NewLogger(zerolog.WithLevel("FATAL"))

// log level ERROR
logger := zerolog.NewLogger(zerolog.WithLevel("ERROR"))

// log level TRACE
logger := zerolog.NewLogger(zerolog.WithLevel("TRACE"))

// log level INFO
logger := zerolog.NewLogger(zerolog.WithLevel("INFO"))
WithConsoleEnabled

sets whether the standard logger output will be in console. Accepts multi writing (console and file).

Enabled
// console enable true
logger := zerolog.NewLogger(zerolog.WithConsoleEnabled(true))

// console enable false
logger := zerolog.NewLogger(zerolog.WithConsoleEnabled(false))
WithFileEnabled

sets whether the standard logger output will be in file. Accepts multi writing (file and console).

Enabled
// file enable true
logger := zerolog.NewLogger(zerolog.WithFileEnabled(true))

// file enable false
logger := zerolog.NewLogger(zerolog.WithFileEnabled(false))
WithFilePath

sets the path where the file will be saved.

// file path
logger := zerolog.NewLogger(zerolog.WithFilePath("/tmp"))
WithFileName

sets the name of the file.

// file name
logger := zerolog.NewLogger(zerolog.WithFileName("application.log"))
WithFileMaxSize

sets the maximum size in megabytes of the log file. It defaults to 100 megabytes.

// file max size
logger := zerolog.NewLogger(zerolog.WithFileMaxSize(100))
WithFileCompress

sets whether the log files should be compressed.

// file compress true
logger := zerolog.NewLogger(zerolog.WithFileCompress(true))

// file compress false
logger := zerolog.NewLogger(zerolog.WithFileCompress(false))
WithFileMaxAge

sets the maximum number of days to retain old log files based on the timestamp encoded in their filename.

// file max age
logger := zerolog.NewLogger(zerolog.WithFileMaxAge(10))
WithErrorFieldName

sets the field name used on WithError

logger := zerolog.NewLogger(zerolog.WithErrorFieldName("error"))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewLogger

func NewLogger(option ...Option) log.Logger

NewLogger constructs a new Logger from provided variadic Option.

func NewLoggerWithOptions

func NewLoggerWithOptions(options *Options) log.Logger

NewLoggerWithOptions constructs a new Logger from provided Options.

Types

type Option

type Option func(options *Options)

func WithConsoleEnabled

func WithConsoleEnabled(value bool) Option

func WithErrorFieldName

func WithErrorFieldName(value string) Option

func WithFileCompress

func WithFileCompress(value bool) Option

func WithFileEnabled

func WithFileEnabled(value bool) Option

func WithFileMaxAge

func WithFileMaxAge(value int) Option

func WithFileMaxSize

func WithFileMaxSize(value int) Option

func WithFileName

func WithFileName(value string) Option

func WithFilePath

func WithFilePath(value string) Option

func WithFormatter

func WithFormatter(value string) Option

func WithLevel

func WithLevel(value string) Option

type Options

type Options struct {
	Formatter string // formatter TEXT/JSON
	Level     string // log level

	Console struct {
		Enabled bool // enable/disable console logging
	}
	File struct {
		Enabled  bool   // enable/disable file logging
		Path     string // file log path
		Name     string // file log filename
		MaxSize  int    // file log file max size (MB)
		Compress bool   // enabled/disable file compress
		MaxAge   int    // file max age
	}

	ErrorFieldName string // define field name for error logging
}

Jump to

Keyboard shortcuts

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