Back to

Package logging

Latest Go to latest

The highest tagged major version is .

Published: Oct 23, 2020 | License: Apache-2.0 | Module:



var AutoStartServer = true

func BasicLoggingConfig

func BasicLoggingConfig(serviceName string, atomicLevel zap.AtomicLevel, opts ...zap.Option) *zap.Config

func CreateLogger

func CreateLogger(serviceName string, opts ...zap.Option) (*zap.Logger, error)

CreateLogger can be used to create the correct zap logger based on the environment.

First, if an environment variable `ZAP_PRETTY` pretty is present, a `zapdriver.NewProduction` is used but logging all levels (`Debug` level and more). Furthermore, this logger will print everything into the standard output of the process (opposed to standard error by default). If the env is set, it overrides everything. If the value of the `ZAP_PRETTY` environment variable is a valid Zap level (`debug`, `info`, `warn`, `error`), the logger level will be configured using the value. In all other cases, `debug` level is used as the default.

Then, if in production, automatically a `zap.NewProduction()` is returned. The production environment is determined based on the presence of the `/.dockerenv` file.

In all other cases, return a `zap.NewDevelopment()` logger.

func CreateLoggerWithLevel

func CreateLoggerWithLevel(serviceName string, atomicLevel zap.AtomicLevel, opts ...zap.Option) (*zap.Logger, error)

CreateLoggerWithLevel behaves exactly like `CreateLogger`, but you can pass the atomic level that should be used for the logger that will use this atomic level. By keeping a reference to it, later on, you will be able to change the level at runtime by calling `atomicLevel.SetLevel` on your reference and logger level will be changed.

func Extend

func Extend(extender LoggerExtender, regexps ...string)

Extend is different than `Set` by being able to re-configure the existing logger set for all registered logger in the registry. This is useful for example to add a field to the currently set logger:

``` logger.Extend(func (current *zap.Logger) { return current.With("name", "value") }, "*") ```

func FlagFields

func FlagFields(extraFields ...zap.Field) []zap.Field

FlagFields returns all falg as `zap.Field` element for easy logging

func IsProductionEnvironment

func IsProductionEnvironment() bool

func LevelFromEnvironment

func LevelFromEnvironment() zap.AtomicLevel

func Logger

func Logger(ctx context.Context, fallbackLogger *zap.Logger) *zap.Logger

Logger is used to retrieved the logger from the context. If no logger is present in the context, the `fallbackLogger` received in parameter is returned instead.

func MustCreateLogger

func MustCreateLogger(opts ...zap.Option) *zap.Logger

Deprecated: You should use MustCreateLoggerWithServiceName

func MustCreateLoggerWithLevel

func MustCreateLoggerWithLevel(serviceName string, atomicLevel zap.AtomicLevel, opts ...zap.Option) *zap.Logger

MustCreateLoggerWithLevel behaves exactly like `MustCreateLogger`, but you can pass the atomic level that should be used for the logger that will use this atomic level. By keeping a reference to it, later on, you will be able to change the level at runtime by calling `atomicLevel.SetLevel` on your reference and logger level will be changed.

func MustCreateLoggerWithServiceName

func MustCreateLoggerWithServiceName(serviceName string, opts ...zap.Option) *zap.Logger

MustCreateLogger has the same behavior as `CreateLogger` function. However, it automatically panic if the logger was not created successfully.

func Override

func Override(logger *zap.Logger)

Override sets the given logger on previously registered and next registrations. Useful in tests.

func Register

func Register(name string, zlogPtr **zap.Logger, options ...RegisterOption)

func Set

func Set(logger *zap.Logger, regexps ...string)

func TestingOverride

func TestingOverride()

TestingOverride calls `Override` (or `Set`, see below) with a development logger setup correctly with the right level based on some environment variables.

By default, override using a `zap.NewDevelopment` logger (`info`), if environment variable `DEBUG` is set to anything or environment variable `TRACE` is set to `true`, logger is set in `debug` level.

If `DEBUG` is set to something else than `true` and/or if `TRACE` is set to something else than

func WithLogger

func WithLogger(ctx context.Context, logger *zap.Logger) context.Context

WithLogger is used to create a new context with a logger added to it so it can be later retrieved using `Logger`.

type Handler

type Handler struct {
	// Handler is the handler used to handle the incoming request.
	Next http.Handler

	// Propagation defines how traces are propagated. If unspecified,
	// Stackdriver propagation will be used.
	Propagation propagation.HTTPFormat

	// Actual root logger to instrument with request information
	RootLogger *zap.Logger

Handler is an http.Handler wrapper to instrument your HTTP server with an automatic `zap.Logger` per request (i.e. context).


This handler is aware of the incoming request's trace id, reading it from request headers as configured using the Propagation field. The extracted trace id if present is used to configure the actual logger with the field `trace_id`.

If the trace id cannot be extracted from the request, an random request id is generated and used under the field `req_id`.

func (*Handler) ServeHTTP

func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request)

type LoggerExtender

type LoggerExtender func(*zap.Logger) *zap.Logger

type RegisterOption

type RegisterOption interface {
	// contains filtered or unexported methods

RegisterOption are option parameters that you can set when registering a new logger in the system using `Register` function.

func RegisterOnUpdate

func RegisterOnUpdate(onUpdate func(newLogger *zap.Logger)) RegisterOption

RegisterOnUpdate enable you to have a hook function that will receive the new logger that is going to be assigned to your logger instance. This is useful in some situation where you need to update other instances or re-configuring a bit the logger when a new one is attached.

This is called **after** the instance has been re-assigned.

type TestLogger

type TestLogger struct {
	// contains filtered or unexported fields

func NewTestLogger

func NewTestLogger(t testing.T) *TestLogger

func (*TestLogger) Instance

func (l *TestLogger) Instance() *zap.Logger

Instance returns the actual *zap.Logger you should pass to your dependency to accumulate log lines and inspect them later on.

func (*TestLogger) RecordedLines

func (w *TestLogger) RecordedLines(t testing.T) (out []string)

RecordedLines returns all the logged line seen, each line being a full fledged JSON value

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier