zerolog

package
v0.0.0-...-67c52db Latest Latest
Warning

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

Go to latest
Published: May 20, 2022 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

package zerolog implements a Logger interface using zerolog.

Example

This example uses command-line flags to demonstrate various outputs depending on the chosen log level.

package main

import (
	"flag"
	"os"
	"time"

	"github.com/sraphs/go/log"
	"github.com/sraphs/go/log/logger"
	"github.com/sraphs/go/log/zerolog"
)

// setup would normally be an init() function, however, there seems
// to be something awry with the testing framework when we set the
// global Logger from an init()
func setup() {

	zerolog.TimeFieldFormat = ""

	zerolog.TimestampFunc = func() time.Time {
		return time.Date(2008, 1, 8, 17, 5, 05, 0, time.UTC)
	}

	log.Init(
		zerolog.New(os.Stdout,
			zerolog.WithTimestamp(),
		),
	)
}

func main() {
	setup()
	debug := flag.Bool("debug", false, "sets log level to debug")

	flag.Parse()

	// Default level for this example is info, unless debug flag is present
	log.SetLevel(logger.LevelInfo)

	if *debug {
		log.SetLevel(logger.LevelDebug)
	}

	log.Debug("This message appears only when log level set to Debug")
	log.Info("This message appears when log level set to Debug or Info")

	if log.IsLevelEnabled(logger.LevelDebug) {
		// Compute log output only if enabled.
		value := "bar"
		log.Debug("some debug message", "foo", value)
	}

}
Output:

{"level":"info","msg":"This message appears when log level set to Debug or Info","ts":1199811905}

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	SyncWriter       = zlog.SyncWriter
	MultiLevelWriter = zlog.MultiLevelWriter
)
View Source
var (
	// TimestampFieldName is the field name used for the timestamp field.
	TimestampFieldName = "ts"

	// LevelFieldName is the field name used for the level field.
	LevelFieldName = "level"

	// LevelTraceValue is the value used for the trace level field.
	LevelTraceValue = "trace"
	// LevelDebugValue is the value used for the debug level field.
	LevelDebugValue = "debug"
	// LevelInfoValue is the value used for the info level field.
	LevelInfoValue = "info"
	// LevelWarnValue is the value used for the warn level field.
	LevelWarnValue = "warn"
	// LevelErrorValue is the value used for the error level field.
	LevelErrorValue = "error"
	// LevelFatalValue is the value used for the fatal level field.
	LevelFatalValue = "fatal"
	// LevelPanicValue is the value used for the panic level field.
	LevelPanicValue = "panic"

	// LevelFieldMarshalFunc allows customization of global level field marshaling.
	LevelFieldMarshalFunc = func(l zlog.Level) string {
		return l.String()
	}

	// MessageFieldName is the field name used for the message field.
	MessageFieldName = "msg"

	// ErrorFieldName is the field name used for error fields.
	ErrorFieldName = "error"

	// CallerFieldName is the field name used for caller field.
	CallerFieldName = "caller"

	// CallerSkipFrameCount is the number of stack frames to skip to find the caller.
	CallerSkipFrameCount = 2 + 1

	// CallerMarshalFunc allows customization of global caller marshaling
	CallerMarshalFunc = func(file string, line int) string {
		short := file
		for i := len(file) - 1; i > 0; i-- {
			if file[i] == '/' {
				short = file[i+1:]
				break
			}
		}
		file = short
		return file + ":" + strconv.Itoa(line)
	}

	// ErrorStackFieldName is the field name used for error stacks.
	ErrorStackFieldName = "stack"

	// ErrorStackMarshaler extract the stack from err if any.
	ErrorStackMarshaler = pkgerrors.MarshalStack

	// ErrorMarshalFunc allows customization of global error marshaling
	ErrorMarshalFunc = func(err error) interface{} {
		return err
	}

	// InterfaceMarshalFunc allows customization of interface marshaling.
	// Default: "encoding/json.Marshal"
	InterfaceMarshalFunc = json.Marshal

	// TimeFieldFormat defines the time format of the Time field type. If set to
	// TimeFormatUnix, TimeFormatUnixMs or TimeFormatUnixMicro, the time is formatted as an UNIX
	// timestamp as integer.
	TimeFieldFormat = time.RFC3339

	// TimestampFunc defines the function called to generate a timestamp.
	TimestampFunc = time.Now

	// DurationFieldUnit defines the unit for time.Duration type fields added
	// using the Dur method.
	DurationFieldUnit = time.Millisecond

	// DurationFieldInteger renders Dur fields as integer instead of float if
	// set to true.
	DurationFieldInteger = false

	// ErrorHandler is called whenever zerolog fails to write an event on its
	// output. If not set, an error is printed on the stderr. This handler must
	// be thread safe and non-blocking.
	ErrorHandler func(err error)
)

Functions

func New

func New(w io.Writer, opt ...Option) *zerolog

Types

type ConsoleWriter

type ConsoleWriter = zlog.ConsoleWriter

type Option

type Option func(zlog.Context) zlog.Context

func WithCaller

func WithCaller() Option

func WithCallerWithSkipFrameCount

func WithCallerWithSkipFrameCount(skipFrameCount int) Option

func WithFields

func WithFields(fields interface{}) Option

func WithStack

func WithStack() Option

func WithTimestamp

func WithTimestamp() Option

Jump to

Keyboard shortcuts

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