logging

package
v1.27.1 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2026 License: MIT Imports: 10 Imported by: 0

README

logging

The logging submodule is used to easily configure zerolog for a more flexible and easy use.

Why

Zerolog is already a great logging framework. However, it does lack in especially one area. Area-specific levels. It requires a lot of effort to set up a system where you can configure per-area log levels. This module solves that problem.

Usage

package logging

import (
  "context"
  "github.com/rs/zerolog"
)

func main() {
  // To inject the required flags into the `flag` module
  // Allows setting the log levels via either `GO_LOG` or the -loglevel flag.
	SetupFlags()

  // If you want the logging output to include the area the message is from
	SetIncludeArea(true)

  // Optionally configure how the content of the flag and environment variable
  // get parsed. See the function documentation for details.
  SetAreaSeparator(",")
  SetAreaToLevelSeparator("=")
  SetSubAreaSeparator("::")

  // Configure the context for use with the framework.
  // IMPORTANT: SetupFlags and the various SetXXX functions must be called before
  // ConfigureLogging, otherwise they won't have any effect.

  // Use nil as 2nd argument to write only to stderr.
  ctx := ConfigureLogging(context.Background(), nil)
  // Or, to write to stderr and, in this case, stdout
  // ctx := ConfigureLogging(context.Background(), os.Stdout)

  var log zerolog.Logger

  // Then acquire your logger for the area you want

  // For the base area ("")
  log = FromContext(ctx) // or FromContext(ctx, ""), "" is cut from the area list

  // Log will now have the logging level configured for
  // the `example` area, if set. Otherwise the base area level
  log = FromContext(ctx, "example")

  // Log will now have the logging level configured for
  // the `example::sub` area, if set. Otherwise the level of `example` or base
  log = FromContext(ctx, "example", "sub")

  // You can also attach an area to the context to prepend to the requested area

  // Attach a default area to the context.
  ctxWithSubarea := WithSubarea(ctx, "example")

  // Log will now have the logging level configured for
  // the `example` area, if set. Otherwise the base area level
  log = FromContext(ctxWithSubarea)

  // Log will now have the logging level configured for
  // the `example::sub` area, if set. Otherwise the level of `example` or base
  log = FromContext(ctxWithSubarea, "sub")

  // You can also explicitly ignore the area already attached to the context

  // Log will now have the logging level configured for
  // the `example2` area, if set. Otherwise the base area level
  log = FromContextBlank(ctxWithSubarea, "example2")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureLogging

func ConfigureLogging(ctx context.Context, logWriter io.Writer) context.Context

Configure internal base logger (with logWriter (if not nil) and stderr) at trace level Also parses the "GO_LOG" environment variable into log levels

func CopyIntoCtx added in v1.21.1

func CopyIntoCtx(source, target context.Context) context.Context

Copy the logger configuration of the source context into the target context. Useful for cases where you don't control the base context

func FromContext

func FromContext(ctx context.Context, area ...string) zerolog.Logger

Get a logger from the given context, with the given area. If context has a subarea set, prepend that to the area

func FromContextBlank added in v1.23.0

func FromContextBlank(ctx context.Context, area ...string) zerolog.Logger

Get a logger from the given context, with the given area. Ignores the context's subarea, if set

func GetCurrentSubareaPrefix added in v1.25.2

func GetCurrentSubareaPrefix(ctx context.Context) string

func GetLevels added in v1.25.1

func GetLevels() map[string]zerolog.Level

func NewLogger

func NewLogger(extraLogWriter io.Writer) zerolog.Logger

func OverwriteLevelIfNotSet added in v1.23.1

func OverwriteLevelIfNotSet(level zerolog.Level, area ...string)

func SetAreaSeparator added in v1.26.0

func SetAreaSeparator(n string)

SetAreaSeparator configures the separator for area to level pairs. Example: the `,` in `info,example::test=info`. Defaults to "," to match Rust's `RUST_LOG` behaviour.

func SetAreaToLevelSeparator added in v1.26.0

func SetAreaToLevelSeparator(n string)

SetAreaSeparator configures the separator for each area to level. Example: the `=` in `info,example::test=info`. Defaults to "=" to match Rust's `RUST_LOG` behaviour.

func SetBadFormatShouldPanic added in v1.26.0

func SetBadFormatShouldPanic(n bool)

SetBadFormatShouldPanic configures whether a badly formatted loglevel should raise a panic. Defaults to false, where a bad value is set to info.

func SetIncludeArea added in v1.25.3

func SetIncludeArea(n bool)

SetIncludeArea configures whether the loggers returned by FromContext and FromContextBlank will append the specific area of the logger to the logger's output. Defaults to false.

func SetLogDebugAreaSearch added in v1.26.0

func SetLogDebugAreaSearch(n bool)

SetLogDebugAreaSearch configures whether the level search for the specified area (including subarea) should be logged. If true, the search will be logged at trace level, always. Defaults to false.

func SetSubAreaSeparator added in v1.26.0

func SetSubAreaSeparator(n string)

SetAreaSeparator configures the separator for each area to level. Example: the `::` in `info,example::test=info`. Defaults to "::" to match Rust's `RUST_LOG` behaviour.

func SetupFlags

func SetupFlags()

func WithSubarea added in v1.21.0

func WithSubarea(ctx context.Context, subarea ...string) context.Context

Configure a new context with a given subarea for logging. The subarea will be prepended to the area given in FromContext. If a subarea is already set, the existing one is prepended to the new one

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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