logging

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const RequestIDHeader = "opc-request-id"

RequestIDHeader is the header to look for the request ID.

View Source
const RequestIDKey = "request-id"

RequestIDKey is the key for the request ID in the context.

View Source
const RequestLoggerKey = "request-logger"

RequestLoggerKey is the key for the logger in the context.

Variables

View Source
var ConfigKey = "logging"

ConfigKey is the root configuration key (in Viper) for this module.

View Source
var Module fx.Option = fx.Provide(
	provideZapLogger(ConfigKey),
	provideInterface,
)

Module will load the configuration from Viper and creates a new logger using "logging" viper configuration key.

View Source
var TimeFormat = time.RFC3339

TimeFormat is the time format to be used when writing logs.

View Source
var TimeNowFunc = time.Now

TimeNowFunc lets you specify the function for obtaining the current time. This is mainly to aid in testing.

View Source
var UseLoggingInterface fx.Option = fx.WithLogger(
	func(logger Interface) fxevent.Logger {
		return &fxLoggerAdapter{Interface: logger}
	},
)

UseLoggingInterface makes fx itself log its events to the instance of logging.Interface inside the container being built. Requires logging.Interface to be provided within the resulting fx.App.

Functions

func ModuleNamed

func ModuleNamed(configKey string) fx.Option

ModuleNamed represents a module for *zap.Logger & logging.Interface that's loaded and annotated using a given configKey.

See https://uber-go.github.io/fx/get-started/another-handler.html and https://uber-go.github.io/fx/annotate.html#annotating-a-function.

func NewLogger

func NewLogger(config *Config) (*zap.Logger, error)

NewLogger takes a logging config and returns a new Zap logger that writes to the log file pointed to by the config with the options applied and stdout.

Types

type Config

type Config struct {
	// Debug sets the logging level to debug.
	//
	// If debug is true, any value in `level` is ignored and
	// it forces the logger to use Console encoder (instead of JSON).
	//
	// If you want to use JSON decoder while producing debug logs, use "debug=false, level=debug" combination.
	//
	// This field is here for legacy reasons only, maybe at some point we'll be able to get rid of it.
	Debug bool `mapstructure:"debug"`

	// Level controls the logging level.
	//
	// Defaults to INFO if not set.
	Level Level `mapstructure:"level"`

	// If set, timestamps will be serialized as RFC3339Nano time format.
	// Otherwise, default EncodeTime formatter will be used (ISO8601 if debug is set, Epoch otherwise).
	//
	// See getZapEncoderConfig() for details.
	EncodeTimeAsRFC3339Nano bool `mapstructure:"encodeTimeAsRFC3339Nano"`

	// DisableConsoleOutput disables logs to be written to the console.
	// This will prevent ODO from copying these logs into journalctl -> syslog -> /var/log/user.log
	// which can cause disk space usage issues.
	DisableConsoleOutput bool `mapstructure:"disableConsoleOutput"`

	// Logger contains various knobs of lumberjack logging functionality.
	lumberjack.Logger `mapstructure:",squash"`
}

Config holds the configuration for logging.

func NewConfig

func NewConfig(opts ...Option) (*Config, error)

NewConfig creates a new logging config with the given options.

func (*Config) Apply

func (c *Config) Apply(opts ...Option) error

Apply takes the supplied options and applies them to the configuration.

func (*Config) Validate

func (c *Config) Validate() error

Validate ensures the logging Config is valid.

type Interface

type Interface interface {
	WithField(key string, value interface{}) Interface
	WithError(err error) Interface

	Debug(msg string)
	Info(msg string)
	Warn(msg string)
	Error(msg string)
	Fatal(msg string)

	// Avoid using Printf-like methods
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})
	Fatalf(format string, args ...interface{})
}

Interface exists solely in order to decouple clients from various implementations of logging libraries (while we transition from logrus to zap).

NOTE: This is temporary and not intended to be fast (printf-like methods are really bad). Use zap logger directly for new code (see benchmarks at https://github.com/uber-go/zap).

func Discard

func Discard() Interface

Discard constructs a logger that discards any logging message.

Used as a null-object pattern.

func ForLogrus

func ForLogrus(logger *logrus.Entry) Interface

func ForZap

func ForZap(logger *zap.Logger) Interface

Create Zap logger with caller information enabled

func NewTestLogger

func NewTestLogger() Interface

type Level

type Level string

Level is an enumeration encapsulating the logging level.

const (
	LevelDebug Level = "DEBUG"
	LevelInfo  Level = "INFO"
	LevelWarn  Level = "WARN"
	LevelError Level = "ERROR"
)

func ParseLevel

func ParseLevel(level string) (Level, error)

ParseLevel parses the logging level.

func (Level) String

func (l Level) String() string

String implements fmt.Stringer.

func (Level) Validate

func (l Level) Validate() error

Validate validates whether this Level is valid.

type Option

type Option func(*Config) error

Option is a configuration option for logging.

func WithViper

func WithViper(v *viper.Viper) Option

WithViper applies the configuration using Viper root configuration key "logging". It assumes that Viper has already been configured to read from a config file, the environment, or flags.

By its nature, calling WithViper ensures the resulting config will never fail Validate.

func WithViperKey

func WithViperKey(v *viper.Viper, configKey string) Option

WithViperKey applies the configuration using Viper using a specified configuration key. It assumes that Viper has already been configured to read from a config file, the environment, or flags.

By its nature, calling WithViperKey ensures the resulting config will never fail Validate.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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