Documentation
¶
Overview ¶
Package logging provides a kitlog compatible logger.
This package is mostly a thin wrapper around kitlog (http://github.com/go-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/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 an opentracing span in the context, the span will receive the logger output as well.
Example ¶
package main
import (
"context"
"github.com/DoNewsCode/core/ctxmeta"
"github.com/DoNewsCode/core/logging"
)
func main() {
bag, ctx := ctxmeta.Inject(context.Background())
bag.Set("clientIp", "127.0.0.1")
bag.Set("requestUrl", "/example")
bag.Set("transport", "http")
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"}
Types ¶
type LevelLogger ¶
type LevelLogger = contract.LevelLogger
LevelLogger is an alias of contract.LevelLogger
func WithLevel ¶
func WithLevel(logger log.Logger) LevelLogger
WithLevel decorates the logger and returns a contract.LevelLogger.
Note: Don't inject contract.LevelLogger to dependency consumers directly as this will weaken 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"}