Documentation ¶
Overview ¶
Package log simply a wrapper of zerolog.Logger.
Example ¶
package main import ( "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{}) log.Info().Msg("hello world") }
Output: {"level":"info","time":"2020-07-18T21:01:05Z","message":"hello world"}
Example (StdLog) ¶
package main import ( stdLog "log" "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{}) stdLog.Printf("hello from standard log") }
Output: {"time":"2020-07-18T21:01:05Z","level":"warn","message":"hello from standard log"}
Example (WithCaller) ¶
package main import ( "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{Caller: true}) log.Error().Msg("something went wrong") }
Output: {"level":"error","time":"2020-07-18T21:01:05Z","caller":"/app/controller/root.go:113","message":"something went wrong"}
Example (WithLevel) ¶
package main import ( "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{Level: "panic"}) log.Fatal().Msg("debugging message") }
Output:
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Config ¶
type Config struct { // Level log level config. // // Supported values: `trace`, `debug`, `info`, `warn`, `fatal`, `panic`. // Default value "info". Level string // Output log output. // // Supported values: `console`, `stderr`, `stdout`. // For `console` print log with pretty output, // `stderr` and `stdout` print log with JSON format. Out string // StackTrace set this to true to enable stack stacktrace. StackTrace bool // Caller if enabled, print log caller filename and line number. Caller bool }
Config logging configuration.
type Event ¶
Event zerolog.Event alias.
func Debug ¶
func Debug() *Event
Debug starts logging with debug level.
Example ¶
package main import ( "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{Level: "debug"}) log.Debug().Msg("debugging message") }
Output: {"level":"debug","time":"2020-07-18T21:01:05Z","message":"debugging message"}
type Logger ¶
Logger zerolog.Logger alias.
var ( // Log instance of zerolog.Logger. Log Logger )
func WithHook ¶
WithHook returns a logger with the h Hook.
Example ¶
package main import ( "time" "github.com/foodarchive/truffls/pkg/log" "github.com/rs/zerolog" ) type copyHook struct { } func (h copyHook) Run(e *log.Event, level log.Level, m string) { e.Str("copy", m) } func setup() { zerolog.TimestampFunc = func() time.Time { return time.Date(2020, 7, 18, 21, 1, 05, 0, time.UTC) } zerolog.CallerMarshalFunc = func(_ string, _ int) string { return "/app/controller/root.go:113" } } func main() { setup() log.Init(log.Config{}) lw := log.WithHook(copyHook{}) lw.Log().Msg("message") }
Output: {"time":"2020-07-18T21:01:05Z","copy":"message","message":"message"}
type NoLevelDebugHook ¶
type NoLevelDebugHook struct{}
NoLevelDebugHook replace no level with warn log level.
func (NoLevelDebugHook) Run ¶
func (h NoLevelDebugHook) Run(e *Event, level Level, _ string)
Run add debug level to the log message when log level not provided.
Example ¶
setup() log.Init(log.Config{}) hook := log.Log.Hook(log.NoLevelDebugHook{}) stdLog.SetFlags(0) stdLog.SetOutput(hook) stdLog.Print("debugging logs...")
Output: {"time":"2020-07-18T21:01:05Z","level":"debug","message":"debugging logs..."}
type NoLevelErrorHook ¶
type NoLevelErrorHook struct{}
NoLevelErrorHook replace no level with warn log level.
func (NoLevelErrorHook) Run ¶
func (h NoLevelErrorHook) Run(e *Event, level Level, _ string)
Run add error level to the log message when log level not provided.
Example ¶
setup() log.Init(log.Config{}) hook := log.Log.Hook(log.NoLevelErrorHook{}) stdLog.SetFlags(0) stdLog.SetOutput(hook) stdLog.Print("error logs...")
Output: {"time":"2020-07-18T21:01:05Z","level":"error","message":"error logs..."}
type NoLevelWarnHook ¶
type NoLevelWarnHook struct{}
NoLevelWarnHook replace no level with warn log level.
Click to show internal directories.
Click to hide internal directories.