logging

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 17, 2021 License: MIT Imports: 9 Imported by: 2

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

func LevelFilter(levelCfg string) level.Option

LevelFilter filters the log output based on its level. Allowed levels are "debug", "info", "warn", "error", or "none"

func NewLogger

func NewLogger(format string) (logger log.Logger)

NewLogger constructs a log.Logger based on the given format. The support formats are "json" and "logfmt".

func WithContext

func WithContext(logger log.Logger, ctx context.Context) log.Logger

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

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 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"}

Types

type LevelLogger

type LevelLogger interface {
	log.Logger
	Debug(string)
	Info(string)
	Warn(string)
	Err(string)
	Debugf(string, ...interface{})
	Infof(string, ...interface{})
	Warnf(string, ...interface{})
	Errf(string, ...interface{})
}

LevelLogger is plaintext logger with level.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL