logger

package module
v1.11.0 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2025 License: MIT Imports: 10 Imported by: 117

README

Package logger v1.11.0

This package will provide you a generic way to handle logging.

Configuration

This plugin will configure himself automatically using the following environment variables:

  • LOGGER_TYPE: define the logger output type (values: json, text) (default: text)
  • LOGGER_LEVEL: define the minimum output level of the logger (values: panic, fatal, warn, info, debug) (default: info)

Usage

log := logger.Default() // Return a default logger

Context

The logger can be passed in a context so it can retain fields.

func main() {
  log := logger.Default().WithFields(logrus.Fields{"caller": "main"})
  add(logger.ToCtx(context.Background(), log), 1, 2)
}

def add(ctx context.Context, a, b int) int {
  log := logger.Get(ctx)
  log.Info("Starting add operation")

  log.WithField("operation", "add")
  do(logger.ToCtx(ctx, log), a,b, func(a,b int)int{return a+b})
}

def do(ctx context.Context, a,b int, op fun(int, int)int) {
  log := logger.Get(ctx)
  log.Info("Doing operation")
  op(a,b)
}

2017-08-27 11:10:10 [INFO] Starting add operation caller=main
2017-08-27 11:10:10 [INFO] Do operation caller=main operation=add

Plugins

This logger accept plugins which can register hooks on the logger.

Known plugins

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddLoggerToContext

func AddLoggerToContext(ctx context.Context) context.Context

AddLoggerToContext add the Default() logger on top of the current context

func Default

func Default(opts ...Opt) logrus.FieldLogger

Default generate a logrus logger with the configuration defined in the environment and the hooks used in the plugins

func FieldsFor added in v1.4.0

func FieldsFor(prefix string, value interface{}) logrus.Fields

FieldsFor extracts loggable fields from a struct based on the "log" tag. It returns a logrus.Fields map where the keys are the tag values prefixed with the provided prefix, and the values are the corresponding field values.

If the struct implements the Loggable interface. The `log` tags are ignored and the LogFields method is used to extract the fields.

If the struct has no fields with the "log" tag, it checks if the struct implements the fmt.Stringer interface. If it does, it adds a single field with the prefix as the key and the result of the String() method as the value. If the struct does not implement fmt.Stringer, it adds a single field with the prefix as the key and a default error message as the value.

The "omitempty" option specifies that the field should be omitted if the field has an empty value, defined as false, 0, a nil pointer, a nil interface value, and any array, slice, map, or string of length zero.

The "omitzero" option specifies that the field should be omitted if the field has a zero value, according to rules:

1) If the field type has an "IsZero() bool" method, that will be used to determine whether the value is zero.

2) Otherwise, the value is zero if it is the zero value for its type.

If both "omitempty" and "omitzero" are specified, the field will be omitted if the value is either empty or zero (or both).

Parameters: - value: The struct to extract fields from. - prefix: The prefix to add to each field key.

Returns: - logrus.Fields: A map of loggable fields.

func Get

Get return the logger stored in the context or create a new one if the logger is not set

func NewContextWithLogger

func NewContextWithLogger() context.Context

NewContextWithLogger generate a new context (based on context.Background()) and add a Default() logger on top of it

func ToCtx

func ToCtx(ctx context.Context, logger logrus.FieldLogger) context.Context

ToCtx add a logger to a context

func WithFieldToCtx added in v1.2.0

func WithFieldToCtx(ctx context.Context, key string, value interface{}) (context.Context, logrus.FieldLogger)

WithFieldToCtx adds the field to the logger and adds the logger to the context

func WithFieldsToCtx added in v1.2.0

func WithFieldsToCtx(ctx context.Context, fields logrus.Fields) (context.Context, logrus.FieldLogger)

WithFieldsToCtx adds fields to the logger and adds the logger back to the context

func WithStructToCtx added in v1.4.0

func WithStructToCtx(ctx context.Context, prefix string, value interface{}) (context.Context, logrus.FieldLogger)

Types

type Loggable added in v1.4.0

type Loggable interface {
	LogFields() logrus.Fields
}

type Opt

type Opt func(*logrus.Logger)

Opt is a function-option type for the Default() method.

func WithHooks

func WithHooks(hooks []logrus.Hook) Opt

func WithLogFormatter

func WithLogFormatter(f logrus.Formatter) Opt

func WithLogLevel

func WithLogLevel(lvl logrus.Level) Opt

WithLogLevel let us define the level of verbosity of the logger

func WithOutput added in v1.11.0

func WithOutput(w io.Writer) Opt

func WithSetRedactedFields added in v1.3.0

func WithSetRedactedFields(fields []*RedactionOption) Opt

WithSetRedactedFields redacts fields or parts of fields according to the regular expressions provided. If the regular expression is nil, the field is replaced by "REDACTED".

type Plugin

type Plugin interface {
	Name() string              // Return the name of the plugin. This name must be unique for each plugin
	Hook() (bool, logrus.Hook) // Return a boolean and a hook. If the boolean is set to true, the hook will be added to the logger, otherwise the hook will be ignored
}

Plugin is the interface that a Plugin need to implement to be compatible with the logger library

type PluginManager

type PluginManager struct {
	// contains filtered or unexported fields
}

func Plugins

func Plugins() *PluginManager

Plugins is the entrypoint to the global PluginManager

func (*PluginManager) Hooks

func (m *PluginManager) Hooks() []logrus.Hook

Hooks return all the hooks generated by the plugins

func (*PluginManager) RegisterPlugin

func (m *PluginManager) RegisterPlugin(plugin Plugin)

RegisterPlugin add a plugin to the current plugin list, if the plugin is already in memory, he will skip the add part

type RedactingFormatter added in v1.3.0

type RedactingFormatter struct {
	logrus.Formatter
	// contains filtered or unexported fields
}

func (*RedactingFormatter) Format added in v1.3.0

func (f *RedactingFormatter) Format(entry *logrus.Entry) ([]byte, error)

type RedactionOption added in v1.3.0

type RedactionOption struct {
	Field       string
	Regexp      *regexp.Regexp
	ReplaceWith string
}

Directories

Path Synopsis
plugins

Jump to

Keyboard shortcuts

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