Documentation ¶
Overview ¶
Package zerolog provides a lightweight logging library dedicated to JSON logging.
A global Logger can be use for simple logging:
import "github.com/bloom42/astro-go/log" log.Info().Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world"}
NOTE: To import the global logger, import the "log" subpackage "github.com/bloom42/astro-go/log".
Fields can be added to log messages:
log.Info().Str("foo", "bar").Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
Create logger instance to manage different outputs:
logger := zerolog.New(os.Stderr).With().Timestamp().Logger() logger.Info(). Str("foo", "bar"). Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","foo":"bar"}
Sub-loggers let you chain loggers with additional context:
sublogger := log.With().Str("component": "foo").Logger() sublogger.Info().Msg("hello world") // Output: {"time":1494567715,"level":"info","message":"hello world","component":"foo"}
Level logging
zerolog.SetGlobalLevel(zerolog.InfoLevel) log.Debug().Msg("filtered out message") log.Info().Msg("routed message") if e := log.Debug(); e.Enabled() { // Compute log output only if enabled. value := compute() e.Str("foo": value).Msg("some debug message") } // Output: {"level":"info","time":1494567715,"routed message"}
Customize automatic field names:
log.TimestampFieldName = "t" log.LevelFieldName = "p" log.MessageFieldName = "m" log.Info().Msg("hello world") // Output: {"t":1494567715,"p":"info","m":"hello world"}
Log with no level and message:
log.Log().Str("foo","bar").Msg("") // Output: {"time":1494567715,"foo":"bar"}
Add contextual fields to global Logger:
log.Logger = log.With().Str("foo", "bar").Logger()
Sample logs:
sampled := log.Sample(&zerolog.BasicSampler{N: 10}) sampled.Info().Msg("will be logged every 10 messages")
Log with contextual hooks:
// Create the hook: type SeverityHook struct{} func (h SeverityHook) Run(e *zerolog.Event, level zerolog.Level, msg string) { if level != zerolog.NoLevel { e.Str("severity", level.String()) } } // And use it: var h SeverityHook log := zerolog.New(os.Stdout).Hook(h) log.Warn().Msg("") // Output: {"level":"warn","severity":"warn"}
Caveats ¶
There is no fields deduplication out-of-the-box. Using the same key multiple times creates new key in final JSON each time.
logger := zerolog.New(os.Stderr).With().Timestamp().Logger() logger.Info(). Timestamp(). Msg("dup") // Output: {"level":"info","time":1494567715,"time":1494567715,"message":"dup"}
However, it’s not a big deal though as JSON accepts dup keys, the last one prevails.
Index ¶
- Constants
- Variables
- func DisableSampling(v bool)
- func SetGlobalLevel(l LogLevel)
- func SyncWriter(w io.Writer) io.Writer
- type Array
- func (a *Array) Bool(b bool) *Array
- func (a *Array) Bytes(val []byte) *Array
- func (a *Array) Dur(d time.Duration) *Array
- func (a *Array) Err(err error) *Array
- func (a *Array) Float32(f float32) *Array
- func (a *Array) Float64(f float64) *Array
- func (a *Array) Hex(val []byte) *Array
- func (a *Array) IPAddr(ip net.IP) *Array
- func (a *Array) IPPrefix(pfx net.IPNet) *Array
- func (a *Array) Int(i int) *Array
- func (a *Array) Int16(i int16) *Array
- func (a *Array) Int32(i int32) *Array
- func (a *Array) Int64(i int64) *Array
- func (a *Array) Int8(i int8) *Array
- func (a *Array) Interface(i interface{}) *Array
- func (a *Array) MACAddr(ha net.HardwareAddr) *Array
- func (*Array) MarshalZerologArray(*Array)
- func (a *Array) Object(obj LogObjectMarshaler) *Array
- func (a *Array) Str(val string) *Array
- func (a *Array) Time(t time.Time) *Array
- func (a *Array) Uint(i uint) *Array
- func (a *Array) Uint16(i uint16) *Array
- func (a *Array) Uint32(i uint32) *Array
- func (a *Array) Uint64(i uint64) *Array
- func (a *Array) Uint8(i uint8) *Array
- type BasicSampler
- type BurstSampler
- type ConsoleWriter
- type Event
- func (e *Event) Array(key string, arr LogArrayMarshaler) *Event
- func (e *Event) Bool(key string, b bool) *Event
- func (e *Event) Bools(key string, b []bool) *Event
- func (e *Event) Bytes(key string, val []byte) *Event
- func (e *Event) Caller() *Event
- func (e *Event) Dict(key string, dict *Event) *Event
- func (e *Event) Discard() *Event
- func (e *Event) Dur(key string, d time.Duration) *Event
- func (e *Event) Durs(key string, d []time.Duration) *Event
- func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event
- func (e *Event) Enabled() bool
- func (e *Event) Err(err error) *Event
- func (e *Event) Error(key string, err error) *Event
- func (e *Event) Errors(key string, errs []error) *Event
- func (e *Event) Fields(fields map[string]interface{}) *Event
- func (e *Event) Float32(key string, f float32) *Event
- func (e *Event) Float64(key string, f float64) *Event
- func (e *Event) Floats32(key string, f []float32) *Event
- func (e *Event) Floats64(key string, f []float64) *Event
- func (e *Event) Hex(key string, val []byte) *Event
- func (e *Event) IP(key string, ip net.IP) *Event
- func (e *Event) IPNet(key string, pfx net.IPNet) *Event
- func (e *Event) Int(key string, i int) *Event
- func (e *Event) Int16(key string, i int16) *Event
- func (e *Event) Int32(key string, i int32) *Event
- func (e *Event) Int64(key string, i int64) *Event
- func (e *Event) Int8(key string, i int8) *Event
- func (e *Event) Interface(key string, i interface{}) *Event
- func (e *Event) Ints(key string, i []int) *Event
- func (e *Event) Ints16(key string, i []int16) *Event
- func (e *Event) Ints32(key string, i []int32) *Event
- func (e *Event) Ints64(key string, i []int64) *Event
- func (e *Event) Ints8(key string, i []int8) *Event
- func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event
- func (e *Event) Object(key string, obj LogObjectMarshaler) *Event
- func (e *Event) RawJSON(key string, b []byte) *Event
- func (e *Event) Stack() *Event
- func (e *Event) String(key, val string) *Event
- func (e *Event) Strings(key string, vals []string) *Event
- func (e *Event) Time(key string, t time.Time) *Event
- func (e *Event) TimeDiff(key string, t time.Time, start time.Time) *Event
- func (e *Event) Times(key string, t []time.Time) *Event
- func (e *Event) Timestamp() *Event
- func (e *Event) Uint(key string, i uint) *Event
- func (e *Event) Uint16(key string, i uint16) *Event
- func (e *Event) Uint32(key string, i uint32) *Event
- func (e *Event) Uint64(key string, i uint64) *Event
- func (e *Event) Uint8(key string, i uint8) *Event
- func (e *Event) Uints(key string, i []uint) *Event
- func (e *Event) Uints16(key string, i []uint16) *Event
- func (e *Event) Uints32(key string, i []uint32) *Event
- func (e *Event) Uints64(key string, i []uint64) *Event
- func (e *Event) Uints8(key string, i []uint8) *Event
- type Formatter
- type HookFunc
- type LevelHook
- type LevelSampler
- type LevelWriter
- type LogArrayMarshaler
- type LogHook
- type LogLevel
- type LogObjectMarshaler
- type LogSampler
- type Logger
- func (l Logger) Config(options ...Option) Logger
- func (l *Logger) Debug(message string, fields func(*Event))
- func (l *Logger) Error(message string, fields func(*Event))
- func (l *Logger) Fatal(message string, fields func(*Event))
- func (l *Logger) Info(message string, fields func(*Event))
- func (l *Logger) Log(message string, fields func(*Event))
- func (l *Logger) Panic(message string, fields func(*Event))
- func (l *Logger) ToCtx(ctx context.Context) context.Context
- func (l *Logger) Warn(message string, fields func(*Event))
- func (l Logger) With(fields func(*Event)) Logger
- func (l Logger) Write(p []byte) (n int, err error)
- type Option
- type RandomSampler
- type SyslogWriter
Constants ¶
const (
// Version is the astro's library version
Version = "0.13.3"
)
Variables ¶
var ( // TimestampFieldName is the field name used for the timestamp field. TimestampFieldName = "timestamp" // LevelFieldName is the field name used for the level field. LevelFieldName = "level" // MessageFieldName is the field name used for the message field. MessageFieldName = "message" // 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 = 4 // ErrorStackFieldName is the field name used for error stacks. ErrorStackFieldName = "stack" // ErrorStackMarshaler extract the stack from err if any. ErrorStackMarshaler func(err error) interface{} // ErrorMarshalFunc allows customization of global error marshaling ErrorMarshalFunc = func(err error) interface{} { return err } // TimeFieldFormat defines the time format of the Time field type. // If set to an empty string, 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) )
var ( // Often samples log every ~ 10 events. Often = RandomSampler(10) // Sometimes samples log every ~ 100 events. Sometimes = RandomSampler(100) // Rarely samples log every ~ 1000 events. Rarely = RandomSampler(1000) )
Functions ¶
func DisableSampling ¶ added in v0.13.0
func DisableSampling(v bool)
DisableSampling will disable sampling in all Loggers if true.
func SetGlobalLevel ¶ added in v0.13.0
func SetGlobalLevel(l LogLevel)
SetGlobalLevel sets the global override for log level. If this values is raised, all Loggers will use at least this value.
To globally disable logs, set GlobalLevel to Disabled.
func SyncWriter ¶ added in v0.13.0
SyncWriter wraps w so that each call to Write is synchronized with a mutex. This syncer can be the call to writer's Write method is not thread safe. Note that os.File Write operation is using write() syscall which is supposed to be thread-safe on POSIX systems. So there is no need to use this with os.File on such systems as zerolog guaranties to issue a single Write call per log event.
Types ¶
type Array ¶ added in v0.13.0
type Array struct {
// contains filtered or unexported fields
}
Array is used to prepopulate an array of items which can be re-used to add to log messages.
func Arr ¶ added in v0.13.0
func Arr() *Array
Arr creates an array to be added to an Event or Context.
func (*Array) IPPrefix ¶ added in v0.13.0
IPPrefix adds IPv4 or IPv6 Prefix (IP + mask) to the array
func (*Array) MACAddr ¶ added in v0.13.0
func (a *Array) MACAddr(ha net.HardwareAddr) *Array
MACAddr adds a MAC (Ethernet) address to the array
func (*Array) MarshalZerologArray ¶ added in v0.13.0
MarshalZerologArray method here is no-op - since data is already in the needed format.
func (*Array) Object ¶ added in v0.13.0
func (a *Array) Object(obj LogObjectMarshaler) *Array
Object marshals an object that implement the LogObjectMarshaler interface and append append it to the array.
func (*Array) Time ¶ added in v0.13.0
Time append append t formated as string using zerolog.TimeFieldFormat.
type BasicSampler ¶ added in v0.13.0
type BasicSampler struct { N uint32 // contains filtered or unexported fields }
BasicSampler is a sampler that will send every Nth events, regardless of there level.
func (*BasicSampler) Sample ¶ added in v0.13.0
func (s *BasicSampler) Sample(lvl LogLevel) bool
Sample implements the Sampler interface.
type BurstSampler ¶ added in v0.13.0
type BurstSampler struct { // Burst is the maximum number of event per period allowed before calling // NextSampler. Burst uint32 // Period defines the burst period. If 0, NextSampler is always called. Period time.Duration // NextSampler is the sampler used after the burst is reached. If nil, // events are always rejected after the burst. NextSampler LogSampler // contains filtered or unexported fields }
BurstSampler lets Burst events pass per Period then pass the decision to NextSampler. If Sampler is not set, all subsequent events are rejected.
func (*BurstSampler) Sample ¶ added in v0.13.0
func (s *BurstSampler) Sample(lvl LogLevel) bool
Sample implements the Sampler interface.
type ConsoleWriter ¶ added in v0.13.0
type ConsoleWriter struct { // Out is the output destination. Out io.Writer // NoColor disables the colorized output. NoColor bool // TimeFormat specifies the format for timestamp in output. TimeFormat string // PartsOrder defines the order of parts in output. PartsOrder []string FormatTimestamp Formatter FormatLevel Formatter FormatCaller Formatter FormatMessage Formatter FormatFieldName Formatter FormatFieldValue Formatter FormatErrFieldName Formatter FormatErrFieldValue Formatter }
ConsoleWriter parses the JSON input and writes it in an (optionally) colorized, human-friendly format to Out.
func NewConsoleWriter ¶ added in v0.13.0
func NewConsoleWriter(options ...func(w *ConsoleWriter)) ConsoleWriter
NewConsoleWriter creates and initializes a new ConsoleWriter.
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a log event. It is instanced by one of the level method.
func Dict ¶ added in v0.13.0
func Dict() *Event
Dict creates an Event to be used with the *Event.Dict method. Call usual field methods like Str, Int etc to add fields to this event and give it as argument the *Event.Dict method.
func (*Event) Array ¶ added in v0.13.0
func (e *Event) Array(key string, arr LogArrayMarshaler) *Event
Array adds the field key with an array to the event context. Use zerolog.Arr() to create the array or pass a type that implement the LogArrayMarshaler interface.
func (*Event) Bool ¶ added in v0.13.0
Bool adds the field key with val as a bool to the *Event context.
func (*Event) Bools ¶ added in v0.13.0
Bools adds the field key with val as a []bool to the *Event context.
func (*Event) Bytes ¶ added in v0.13.0
Bytes adds the field key with val as a string to the *Event context.
Runes outside of normal ASCII ranges will be hex-encoded in the resulting JSON.
func (*Event) Caller ¶ added in v0.13.0
Caller adds the file:line of the caller with the zerolog.CallerFieldName key.
func (*Event) Dict ¶ added in v0.13.0
Dict adds the field key with a dict to the event context. Use zerolog.Dict() to create the dictionary.
func (*Event) Dur ¶ added in v0.13.0
Dur adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.
func (*Event) Durs ¶ added in v0.13.0
Durs adds the field key with duration d stored as zerolog.DurationFieldUnit. If zerolog.DurationFieldInteger is true, durations are rendered as integer instead of float.
func (*Event) EmbedObject ¶ added in v0.13.0
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event
EmbedObject marshals an object that implement the LogObjectMarshaler interface.
func (*Event) Enabled ¶ added in v0.13.0
Enabled return false if the *Event is going to be filtered out by log level or sampling.
func (*Event) Err ¶ added in v0.13.0
Err adds the field "error" with serialized err to the *Event context. If err is nil, no field is added. To customize the key name, change zerolog.ErrorFieldName.
To customize the key name, change zerolog.ErrorFieldName.
If Stack() has been called before and zerolog.ErrorStackMarshaler is defined, the err is passed to ErrorStackMarshaler and the result is appended to the zerolog.ErrorStackFieldName.
func (*Event) Error ¶ added in v0.13.1
Error adds the field key with serialized err to the *Event context. If err is nil, no field is added.
func (*Event) Errors ¶ added in v0.13.1
Errors adds the field key with errs as an array of serialized errors to the *Event context.
func (*Event) Fields ¶ added in v0.13.0
Fields is a helper function to use a map to set fields using type assertion.
func (*Event) Float32 ¶ added in v0.13.0
Float32 adds the field key with f as a float32 to the *Event context.
func (*Event) Float64 ¶ added in v0.13.0
Float64 adds the field key with f as a float64 to the *Event context.
func (*Event) Floats32 ¶ added in v0.13.0
Floats32 adds the field key with f as a []float32 to the *Event context.
func (*Event) Floats64 ¶ added in v0.13.0
Floats64 adds the field key with f as a []float64 to the *Event context.
func (*Event) Hex ¶ added in v0.13.0
Hex adds the field key with val as a hex string to the *Event context.
func (*Event) IPNet ¶ added in v0.13.2
IPNet adds IPv4 or IPv6 Prefix (address and mask) to the event
func (*Event) Int16 ¶ added in v0.13.0
Int16 adds the field key with i as a int16 to the *Event context.
func (*Event) Int32 ¶ added in v0.13.0
Int32 adds the field key with i as a int32 to the *Event context.
func (*Event) Int64 ¶ added in v0.13.0
Int64 adds the field key with i as a int64 to the *Event context.
func (*Event) Int8 ¶ added in v0.13.0
Int8 adds the field key with i as a int8 to the *Event context.
func (*Event) Interface ¶ added in v0.13.0
Interface adds the field key with i marshaled using reflection.
func (*Event) Ints ¶ added in v0.13.0
Ints adds the field key with i as a []int to the *Event context.
func (*Event) Ints16 ¶ added in v0.13.0
Ints16 adds the field key with i as a []int16 to the *Event context.
func (*Event) Ints32 ¶ added in v0.13.0
Ints32 adds the field key with i as a []int32 to the *Event context.
func (*Event) Ints64 ¶ added in v0.13.0
Ints64 adds the field key with i as a []int64 to the *Event context.
func (*Event) Ints8 ¶ added in v0.13.0
Ints8 adds the field key with i as a []int8 to the *Event context.
func (*Event) MACAddr ¶ added in v0.13.0
func (e *Event) MACAddr(key string, ha net.HardwareAddr) *Event
MACAddr adds MAC address to the event
func (*Event) Object ¶ added in v0.13.0
func (e *Event) Object(key string, obj LogObjectMarshaler) *Event
Object marshals an object that implement the LogObjectMarshaler interface.
func (*Event) RawJSON ¶ added in v0.13.0
RawJSON adds already encoded JSON to the log line under key.
No sanity check is performed on b; it must not contain carriage returns and be valid JSON.
func (*Event) Stack ¶ added in v0.13.0
Stack enables stack trace printing for the error passed to Err().
ErrorStackMarshaler must be set for this method to do something.
func (*Event) String ¶ added in v0.13.1
String adds the field key with val as a string to the *Event context.
func (*Event) Strings ¶ added in v0.13.1
Strings adds the field key with vals as a []string to the *Event context.
func (*Event) Time ¶ added in v0.13.0
Time adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (*Event) TimeDiff ¶ added in v0.13.0
TimeDiff adds the field key with positive duration between time t and start. If time t is not greater than start, duration will be 0. Duration format follows the same principle as Dur().
func (*Event) Times ¶ added in v0.13.0
Times adds the field key with t formated as string using zerolog.TimeFieldFormat.
func (*Event) Timestamp ¶ added in v0.13.0
Timestamp adds the current local time as UNIX timestamp to the *Event context with the "time" key. To customize the key name, change zerolog.TimestampFieldName.
NOTE: It won't dedupe the "time" key if the *Event (or *Context) has one already.
func (*Event) Uint ¶ added in v0.13.0
Uint adds the field key with i as a uint to the *Event context.
func (*Event) Uint16 ¶ added in v0.13.0
Uint16 adds the field key with i as a uint16 to the *Event context.
func (*Event) Uint32 ¶ added in v0.13.0
Uint32 adds the field key with i as a uint32 to the *Event context.
func (*Event) Uint64 ¶ added in v0.13.0
Uint64 adds the field key with i as a uint64 to the *Event context.
func (*Event) Uint8 ¶ added in v0.13.0
Uint8 adds the field key with i as a uint8 to the *Event context.
func (*Event) Uints ¶ added in v0.13.0
Uints adds the field key with i as a []int to the *Event context.
func (*Event) Uints16 ¶ added in v0.13.0
Uints16 adds the field key with i as a []int16 to the *Event context.
func (*Event) Uints32 ¶ added in v0.13.0
Uints32 adds the field key with i as a []int32 to the *Event context.
type Formatter ¶
type Formatter func(interface{}) string
Formatter transforms the input into a formatted string.
type HookFunc ¶ added in v0.13.0
HookFunc is an adaptor to allow the use of an ordinary function as a LogHook.
type LevelHook ¶ added in v0.13.0
type LevelHook struct {
NoLevelHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook LogHook
}
LevelHook applies a different hook for each level.
func NewLevelHook ¶ added in v0.13.0
func NewLevelHook() LevelHook
NewLevelHook returns a new LevelHook.
type LevelSampler ¶ added in v0.13.0
type LevelSampler struct {
DebugSampler, InfoSampler, WarnSampler, ErrorSampler LogSampler
}
LevelSampler applies a different sampler for each level.
func (LevelSampler) Sample ¶ added in v0.13.0
func (s LevelSampler) Sample(lvl LogLevel) bool
type LevelWriter ¶ added in v0.13.0
LevelWriter defines as interface a writer may implement in order to receive level information with payload.
func MultiLevelWriter ¶ added in v0.13.0
func MultiLevelWriter(writers ...io.Writer) LevelWriter
MultiLevelWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command. If some writers implement LevelWriter, their WriteLevel method will be used instead of Write.
func SyslogLevelWriter ¶ added in v0.13.0
func SyslogLevelWriter(w SyslogWriter) LevelWriter
SyslogLevelWriter wraps a SyslogWriter and call the right syslog level method matching the zerolog level.
type LogArrayMarshaler ¶ added in v0.13.0
type LogArrayMarshaler interface {
MarshalZerologArray(a *Array)
}
LogArrayMarshaler provides a strongly-typed and encoding-agnostic interface to be implemented by types used with Event/Context's Array methods.
type LogHook ¶ added in v0.13.1
type LogHook interface { // Run runs the hook with the event. Run(e *Event, level LogLevel, message string) }
LogHook defines an interface to a log hook.
type LogLevel ¶ added in v0.13.1
type LogLevel uint8
LogLevel defines log levels.
const ( // DebugLevel defines debug log level. DebugLevel LogLevel = iota // InfoLevel defines info log level. InfoLevel // WarnLevel defines warn log level. WarnLevel // ErrorLevel defines error log level. ErrorLevel // FatalLevel defines fatal log level. FatalLevel // PanicLevel defines panic log level. PanicLevel // NoLevel defines an absent log level. NoLevel // Disabled disables the logger. Disabled )
func GlobalLevel ¶ added in v0.13.0
func GlobalLevel() LogLevel
GlobalLevel returns the current global log level
func ParseLevel ¶ added in v0.13.0
ParseLevel converts a level string into a zerolog Level value. returns an error if the input string does not match known values.
type LogObjectMarshaler ¶ added in v0.13.0
type LogObjectMarshaler interface {
MarshalZerologObject(e *Event)
}
LogObjectMarshaler provides a strongly-typed and encoding-agnostic interface to be implemented by types used with Event/Context's Object methods.
type LogSampler ¶ added in v0.13.1
type LogSampler interface { // Sample returns true if the event should be part of the sample, false if // the event should be dropped. Sample(lvl LogLevel) bool }
LogSampler defines an interface to a log sampler.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
A Logger represents an active logging object that generates lines of JSON output to an io.Writer. Each logging operation makes a single call to the Writer's Write method. There is no guaranty on access serialization to the Writer. If your Writer is not thread safe, you may consider a sync wrapper.
func FromCtx ¶ added in v0.13.1
FromCtx returns the Logger associated with the ctx. If no logger is associated, a nilis returned.
For example, to add a field to an existing logger in the context, use this notation:
ctx := r.Context() l := astro.FromCtx(ctx) l.With(...)
func New ¶ added in v0.13.0
New creates a root logger with given options. If the output writer implements the LevelWriter interface, the WriteLevel method will be called instead of the Write one. Default writer is os.Stdout
Each logging operation makes a single call to the Writer's Write method. There is no guaranty on access serialization to the Writer. If your Writer is not thread safe, you may consider using sync wrapper.
func Nop ¶ added in v0.13.0
func Nop() Logger
Nop returns a disabled logger for which all operation are no-op.
func (*Logger) Fatal ¶
Fatal logs a new message with fatal level. The os.Exit(1) function is then called, which terminates the program immediately.
func (*Logger) Log ¶ added in v0.13.0
Log logs a new message with no level. Setting GlobalLevel to Disabled will still disable events produced by this method.
func (*Logger) Panic ¶ added in v0.13.0
Panic logs a new message with panic level. The panic() function is then called, which stops the ordinary flow of a goroutine.
func (*Logger) ToCtx ¶ added in v0.13.1
ToCtx returns a copy of ctx with l associated. If an instance of Logger is already in the context, the context is not updated.
type Option ¶ added in v0.13.1
type Option func(logger *Logger)
Option is used to configure a logger.
func Sampler ¶ added in v0.13.0
func Sampler(sampler LogSampler) Option
Sampler update logger's sampler.
type RandomSampler ¶ added in v0.13.0
type RandomSampler uint32
RandomSampler use a PRNG to randomly sample an event out of N events, regardless of their level.
func (RandomSampler) Sample ¶ added in v0.13.0
func (s RandomSampler) Sample(lvl LogLevel) bool
Sample implements the Sampler interface.
Source Files ¶
Directories ¶
Path | Synopsis |
---|---|
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper.
|
Package diode provides a thread-safe, lock-free, non-blocking io.Writer wrapper. |
examples
|
|
Package hlog provides a set of http.Handler helpers for zerolog.
|
Package hlog provides a set of http.Handler helpers for zerolog. |
internal
|
|
cbor
Package cbor provides primitives for storing different data in the CBOR (binary) format.
|
Package cbor provides primitives for storing different data in the CBOR (binary) format. |
Package log provides a global logger for astro.
|
Package log provides a global logger for astro. |