Documentation ¶
Overview ¶
Package logging provides a kitlog compatible logger.
This package is mostly a thin wrapper around kitlog (http://github.com/go-kit/kit/log). kitlog provides a minimalist, contextual, fully composable logger. However it is too unopinionated, hence requiring some efforts and coordination to set up a good practise.
Integration ¶
Package logging is bundled in core. Enable logging as dependency by calling:
var c *core.C = core.New() c.ProvideEssentials()
See example for usage.
Example (Level) ¶
package main import ( "github.com/DoNewsCode/core/logging" "github.com/go-kit/kit/log/level" ) func main() { logger := logging.NewLogger("json") level.Info(logger).Log("foo", "bar") }
Output: {"foo":"bar","level":"info"}
Example (Minimal) ¶
package main import ( "github.com/DoNewsCode/core/logging" ) func main() { logger := logging.NewLogger("json") logger.Log("foo", "bar") }
Output: {"foo":"bar"}
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func LevelFilter ¶
LevelFilter filters the log output based on its level. Allowed levels are "debug", "info", "warn", "error", or "none"
func NewLogger ¶
NewLogger constructs a log.Logger based on the given format. The support formats are "json" and "logfmt".
func WithContext ¶
WithContext decorates the log.Logger with information form context. If there is a opentracing span in the context, the span will receive the logger output as well.
Example ¶
package main import ( "context" "github.com/DoNewsCode/core/contract" "github.com/DoNewsCode/core/logging" ) func main() { ctx := context.WithValue(context.Background(), contract.IpKey, "127.0.0.1") ctx = context.WithValue(ctx, contract.TransportKey, "http") ctx = context.WithValue(ctx, contract.RequestUrlKey, "/example") logger := logging.NewLogger("json") ctxLogger := logging.WithContext(logger, ctx) ctxLogger.Log("foo", "bar") }
Output: {"clientIp":"127.0.0.1","foo":"bar","requestUrl":"/example","transport":"http"}
func WithLevel ¶
WithLevel decorates the logger and returns a contract.LevelLogger.
Note: Don't inject contract.LevelLogger to dependency consumers directly as this will weakens the powerful abstraction of log.Logger. Only inject log.Logger, and converts log.Logger to contract.LevelLogger within the boundary of dependency consumer if desired.
Example ¶
package main import ( "github.com/DoNewsCode/core/logging" ) func main() { logger := logging.NewLogger("json") levelLogger := logging.WithLevel(logger) levelLogger.Info("hello") }
Output: {"caller":"example_test.go:28","level":"info","msg":"hello"}