Documentation
¶
Overview ¶
Package zlog provides a consistent, easy to use structure logging interface.
The interface is inspired by zerolog (https://github.com/rs/zerolog) and uses it for the default implementation that provides json formatted logs.
Every logger is named so that it can be registered with a logging service. To create a new logger:
log := zlog.New("test")
log.Info().Msg("test")
// Output: {level":"info","message":"test"}
Fields can be added to log messages:
log.Info().Str("foo", "bar").Msg("hello world")
// Output: {level":"info","message":"hello world","foo":"bar"}
Create logger instance to manage different outputs:
logger := zlog.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
log.Level(zlog.Info)
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"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Context ¶
type Context interface {
// Logger returns the logger with the context previously set.
Logger() Logger
// Str adds the field key with val as a string to the logger context.
Str(key string, val string) Context
// Int adds the field key with i as a int to the logger context.
Int(key string, val int) Context
// Uint adds the field key with i as a uint to the event.
Uint(key string, i uint) Context
// Float32 adds the field key with f as a float32 to the event.
Float32(key string, f float32) Context
// Bool adds the field key with val as a bool to event.
Bool(key string, val bool) Context
// Err adds the field "error" with err as a string to the event.
// If err is nil, no field is added.
Error(e error) Context
// Timestamp adds the current local time as UNIX timestamp to the event.
Timestamp() Context
// Object marshals a custom type that implements ObjectMarshaler interface.
Object(key string, obj ObjectMarshaler) Context
}
Context configures a new sub-logger with contextual fields.
Example (With) ¶
package main
import (
"fmt"
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
ctx := log.With()
// Add Int, Bool and Str context to every log
l := ctx.Int("n", 100).Bool("b", false).Str("t", "test").Uint("u", 1).Float32("f", 1.1).Logger()
// Add Error context in addition to above contexts.
l = l.With().Error(nil).Logger()
// Add one more Error Event in addition to above contexts.
l = l.With().Error(fmt.Errorf("test error")).Logger()
l.Info().Msg("context test")
}
Output: {"level":"info","name":"tst_logger","n":100,"b":false,"t":"test","u":1,"f":1.1,"error":"test error","message":"context test"}
Example (With_object) ¶
package main
import (
"github.com/anuvu/zlog"
)
type User struct {
Name string
ID string
}
func (u *User) Marshal(e zlog.Event) {
e.Str("name", u.Name).Str("id", u.ID)
}
func main() {
log := zlog.New("tst_logger")
l := log.With().Object("user", &User{"Jalaja", "Ganapathy"}).Logger()
l.Info().Msg("object test")
}
Output: {"level":"info","name":"tst_logger","user":{"name":"Jalaja","id":"Ganapathy"},"message":"object test"}
type Event ¶
type Event interface {
// Enabled return false if the *Event is going to be filtered out by
// log level or sampling.
Enabled() bool
// Msg sends the Event with msg added as the message field if not empty.
//
// NOTICE: once this method is called, the Event should be disposed.
// Calling Msg twice can have unexpected result.
Msg(msg string)
// Str adds the field key with val as a string to the event.
Str(key string, val string) Event
// Int adds the field key with i as a int to the event.
Int(key string, val int) Event
// Uint adds the field key with i as a uint to the event.
Uint(key string, i uint) Event
// Float32 adds the field key with f as a float32 to the event.
Float32(key string, f float32) Event
// Bool adds the field key with val as a bool to event.
Bool(key string, val bool) Event
// Err adds the field "error" with err as a string to the event.
// If err is nil, no field is added.
Error(e error) Event
// Timestamp adds the current local time as UNIX timestamp to the event.
Timestamp() Event
// Object marshals a custom type that implements ObjectMarshaler interface.
Object(key string, obj ObjectMarshaler) Event
}
Event provides a log event interface.
Example (Float32) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Info().Float32("float32", 5.5555).Msg("hello world")
}
Output: {"level":"info","name":"tst_logger","float32":5.5555,"message":"hello world"}
Example (Object) ¶
package main
import (
"github.com/anuvu/zlog"
)
type User struct {
Name string
ID string
}
func (u *User) Marshal(e zlog.Event) {
e.Str("name", u.Name).Str("id", u.ID)
}
func main() {
log := zlog.New("tst_logger")
log.Info().Object("user", &User{"Jalaja", "Ganapathy"}).Msg("object test")
}
Output: {"level":"info","name":"tst_logger","user":{"name":"Jalaja","id":"Ganapathy"},"message":"object test"}
Example (Timestamp) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log = log.Level(zlog.Warn)
// We cant test timestamp as its a moving target, lets filter it out
// and figure it out later
log.Info().Timestamp().Msg("hello world")
log.Warn().Msg("hello world")
}
Output: {"level":"warn","name":"tst_logger","message":"hello world"}
Example (Uint) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Info().Uint("uint", 1000).Msg("hello world")
}
Output: {"level":"info","name":"tst_logger","uint":1000,"message":"hello world"}
type Logger ¶
type Logger interface {
// Name returns the name of the logger.
Name() string
// LogLevel returns the current level of the logger.
LogLevel() Level
// Test returns True if the logger will emit a log event at the given level.
Test(lvl Level) bool
// Write provides the io.Writer interface for this logger to be chained the standard
// logging library.
Write([]byte) (int, error)
// Stream duplicates the current logger and sets the output to the given writer.
Stream(w io.Writer) Logger
// Level sets logger to the minimum accepted log level.
Level(lvl Level) Logger
// With creates a child logger with the field added to its context.
With() Context
// Debug starts a new message with debug level.
// You must call Msg on the returned event in order to send the event.
Debug() Event
// Info starts a new message with info level.
// You must call Msg on the returned event in order to send the event.
Info() Event
// Warn starts a new message with warn level.
// You must call Msg on the returned event in order to send the event.
Warn() Event
// Error starts a new message with error level.
// You must call Msg on the returned event in order to send the event.
Error() Event
// Fatal starts a new message with fatal level. The os.Exit(1) function
// is called by the Msg method.
//
// You must call Msg on the returned event in order to send the event.
Fatal() Event
// Panic starts a new message with panic level. The message is also sent
// to the panic function.
//
// You must call Msg on the returned event in order to send the event.
Panic() Event
}
Logger provides a leveled logging interface.
Example (Error) ¶
package main
import (
"errors"
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Error().Error(errors.New("some error")).Msg("error doing something")
}
Output: {"level":"error","name":"tst_logger","error":"some error","message":"error doing something"}
Example (Info) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Info().Str("foo", "bar").Int("n", 123).Bool("b", true).Msg("hello world")
}
Output: {"level":"info","name":"tst_logger","foo":"bar","n":123,"b":true,"message":"hello world"}
Example (Level) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger").Level(zlog.Warn)
log.Info().Msg("filtered out message")
log.Error().Msg("kept message")
}
Output: {"level":"error","name":"tst_logger","message":"kept message"}
Example (Msg) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Debug().Str("foo", "bar").Str("bar", "baz").Msg("")
}
Output: {"level":"debug","name":"tst_logger","foo":"bar","bar":"baz"}
Example (Warn) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger")
log.Warn().Str("foo", "bar").Msg("a warning message")
}
Output: {"level":"warn","name":"tst_logger","foo":"bar","message":"a warning message"}
Example (With) ¶
package main
import (
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger").With().Str("foo", "bar").Logger()
log.Info().Msg("hello world")
}
Output: {"level":"info","name":"tst_logger","foo":"bar","message":"hello world"}
Example (Write) ¶
package main
import (
stdlog "log"
"github.com/anuvu/zlog"
)
func main() {
log := zlog.New("tst_logger").With().Str("foo", "bar").Logger()
stdlog.SetFlags(0)
stdlog.SetOutput(log)
stdlog.Print("hello world")
}
Output: {"name":"tst_logger","foo":"bar","message":"hello world"}
type ObjectMarshaler ¶
type ObjectMarshaler interface {
Marshal(e Event)
}
ObjectMarshaler provides a strongly-typed encoding agnostic interface to be implemented by custom types for efficient encoding