log

package
v0.0.0-...-f36794c Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package log provides functions that are mainly wrappers for github.com/go-kit/log, but help provide consistent log behavior within the project.

In particular, this package focuses on providing level-based structured logging with the following standardized fields:

  • "msg": The main log message
  • "ts": Timestamp formatted with time.RFC3339Nano
  • "caller": The file and line number (as "file.go:N") that emitted the log
  • "error": (ERROR-level logs only) The error that serves as the reason for the log

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConfigureLogger

func ConfigureLogger(l *Logger, lvl string)

ConfigureLogger configures the Logger pointer with a level-based filter that emits JSON structured logs. lvl may be one of: DEBUG, INFO, WARN, ERROR and is not case-sensitive. The configured logger wil emit logs that always include a "ts"-keyed timestamp value and "caller"-keyed string value in the form "filename.go:lineno" that references where the log occurred.

func Debug

func Debug(l Logger, msg interface{}, kv ...interface{})

Debug logs a message and any keyvals with DEBUG level

func Error

func Error(l Logger, msg interface{}, err error, kv ...interface{})

Error logs a message, error and any keyvals with ERROR level

func Errorf

func Errorf(l Logger, msg interface{}, err error, kv ...interface{}) error

Errorf is like Error() but returns a new error that wraps err with msg. It exists to provide brevity/syntactic sugar by allowing callers to handle and log errors in a DRY manner, as demonstrated in the following example:

// myFuncA is functionally equivalent myFuncB
func myFuncA(thing string) error {
	err := doSomething(thing)
	if err != nil {
		log.Error(logger, "Error doing something", err, "thing", thing)
		return fmt.Errorf("Error doing something: %w", err)
	}
	return nil
}

// myFuncB is functionally equivalent to myFuncA
func myFuncB(thing string) error {
	err := doSomething(thing)
	if err != nil {
		return log.Errorf(logger, "Error doing something", err, "thing", thing)
	}
	return nil
}

Note that kvs are included in the log output, but not in the returned error.

func Info

func Info(l Logger, msg interface{}, kv ...interface{})

Info logs a message and any keyvals with INFO level

func Warn

func Warn(l Logger, msg interface{}, kv ...interface{})

Warn logs a message and any keyvals with WARN level

func With

func With(logger Logger, keyvals ...interface{}) log.Logger

With is a wrapper for log.With() and exists to provide brevity/syntactic sugar.

func WithPrefix

func WithPrefix(logger Logger, keyvals ...interface{}) log.Logger

WithPrefix is a wrapper for log.WithPrefix() and exists to provide brevity/syntactic sugar. This function has no effect on JSON log output.

func WithSuffix

func WithSuffix(logger Logger, keyvals ...interface{}) log.Logger

WithSuffix is a wrapper for log.WithSuffix() and exists to provide brevity/syntactic sugar. This function has no effect on JSON log output.

Types

type Logger

type Logger log.Logger

Jump to

Keyboard shortcuts

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