zap

package
v1.0.0-dev.146 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2024 License: MIT Imports: 9 Imported by: 0

README

zap.v1

Example

package main

import (
	"context"

	"github.com/xgodev/boost/wrapper/log"
	"github.com/xgodev/boost/wrapper/log/contrib/go.uber.org/zap.v1"
)

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

	//example use zap
	logger := zap.NewLogger()
	log.Set(logger)

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

	logger.Info("main method.")
	//output: 2021-05-16T14:30:31.788-0300	info	runtime/proc.go:225	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: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:24	foo method.	{"main_field": "example", "foo_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: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:37	bar method.	{"bar_field": "example", "main_field": "example", "foo_field": "example"}
}

func withoutContext() {
	log.Info("withoutContext method")
	//output: 2021-05-16T14:30:31.788-0300	info	contrib/main.go:50	withoutContext method
}

default options:

option value
ConsoleFormatter "TEXT"
ConsoleEnabled true
ConsoleLevel "INFO"
FileEnabled false
FileLevel "INFO"
FilePath "/tmp"
FileName "application.log"
FileMaxSize 100
FileCompress true
FileMaxAge 28
FileFormatter "TEXT"
ErrorFieldName "err"

The package accepts a default constructor:

logger := zap.NewLogger()

Or a constructor with Options:

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

Or a constructor with multiple parameters using optional pattern:

logger := zap.NewLogger(
	zap.WithConsoleFormatter("TEXT"),
	zap.WithConsoleEnabled(true),
	zap.WithFilePath("/tmp"),
	...
)

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

WithConsoleEnabled

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

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

// console enable false
logger := zap.NewLogger(zap.WithConsoleEnabled(false))
WithConsoleLevel

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

// log level DEBUG
logger := zap.NewLogger(zap.WithConsoleLevel("DEBUG"))

// log level WARN
logger := zap.NewLogger(zap.WithConsoleLevel("WARN"))

// log level FATAL
logger := zap.NewLogger(zap.WithConsoleLevel("FATAL"))

// log level ERROR
logger := zap.NewLogger(zap.WithConsoleLevel("ERROR"))

// log level TRACE
logger := zap.NewLogger(zap.WithConsoleLevel("TRACE"))

// log level INFO
logger := zap.NewLogger(zap.WithConsoleLevel("INFO"))
WithConsoleFormatter

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

// text formatter
logger := zap.NewLogger(zap.WithConsoleFormatter("TEXT"))

// json formatter
logger := zap.NewLogger(zap.WithConsoleFormatter("JSON"))
WithFileEnabled

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

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

// file enable false
logger := zap.NewLogger(zap.WithFileEnabled(false))
WithFileLevel

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

// log level DEBUG
logger := zap.NewLogger(zap.WithFileLevel("DEBUG"))

// log level WARN
logger := zap.NewLogger(zap.WithFileLevel("WARN"))

// log level FATAL
logger := zap.NewLogger(zap.WithFileLevel("FATAL"))

// log level ERROR
logger := zap.NewLogger(zap.WithFileLevel("ERROR"))

// log level TRACE
logger := zap.NewLogger(zap.WithFileLevel("TRACE"))

// log level INFO
logger := zap.NewLogger(zap.WithFileLevel("INFO"))
WithFilePath

sets the path where the file will be saved.

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

sets the name of the file.

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

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

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

sets whether the log files should be compressed.

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

// file compress false
logger := zap.NewLogger(zap.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 := zap.NewLogger(zap.WithFileMaxAge(10))
WithFileFormatter

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

// text formatter
logger := zap.NewLogger(zap.WithFileFormatter("TEXT"))

// json formatter
logger := zap.NewLogger(zap.WithFileFormatter("JSON"))
WithErrorFieldName

sets the field name used on WithError

logger := zap.NewLogger(zap.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 WithConsoleFormatter

func WithConsoleFormatter(value string) Option

func WithConsoleLevel

func WithConsoleLevel(value string) Option

func WithErrorFieldName

func WithErrorFieldName(value string) Option

func WithFileCompress

func WithFileCompress(value bool) Option

func WithFileEnabled

func WithFileEnabled(value bool) Option

func WithFileFormatter

func WithFileFormatter(value string) Option

func WithFileLevel

func WithFileLevel(value string) 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

type Options

type Options struct {
	Console struct {
		Enabled   bool   // enable/disable console logging
		Level     string // console log level
		Formatter string // console formatter TEXT/JSON
	}
	File struct {
		Enabled   bool   // enable/disable file logging
		Level     string // file log level
		Path      string // file log path
		Name      string // file log filename
		MaxSize   int    // log file max size (MB)
		Compress  bool   // enabled/disable file compress
		MaxAge    int    // file max age
		Formatter string // file formatter TEXT/JSON
	}

	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