log

package
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2026 License: Apache-2.0 Imports: 17 Imported by: 14

Documentation

Overview

Package log provides leveled, contextual loggers built on go-kit/log.

Index

Constants

View Source
const (
	// TraceIdName is the log and context key used to store a trace identifier.
	TraceIdName = "traceId"
)

Variables

View Source
var CallerName = callerName{}

CallerName is the log key used for the call-site file and line number.

View Source
var DefaultCaller = Caller(5)

DefaultCaller is a Valuer that records the call site five frames above the logger.

View Source
var TimestampFormat = log.TimestampFormat(
	func() time.Time { return time.Now().UTC() },
	"2006-01-02T15:04:05.000Z07:00",
)

TimestampFormat This timestamp format differs from RFC3339Nano by using .000 instead of .999999999 which changes the timestamp from 9 variable to 3 fixed decimals (.130 instead of .130987456).

Functions

func Caller

func Caller(depth int) log.Valuer

Caller returns a Valuer that records the file and line at the given call depth.

func GetContextLogger

func GetContextLogger(ctx context.Context, options ...Option) log.Logger

GetContextLogger returns the logger stored in ctx, or a new trace logger if none is present.

func GetDefaultLogger

func GetDefaultLogger() log.Logger

GetDefaultLogger returns the process-wide default logger, creating one on first use.

func GetSourceCodeDir

func GetSourceCodeDir(codeRelativePath string, depth ...int) string

GetSourceCodeDir returns the directory prefix of the current source file after trimming codeRelativePath. Optional depth selects a different runtime.Caller frame.

func GetTraceId

func GetTraceId(ctx context.Context) string

GetTraceId returns the trace identifier stored in ctx, or a new one if none is present.

func New

func New(opts ...NewLoggerOption) log.Logger

New returns a leveled go-kit logger annotated with a timestamp and caller. Output goes to Config.FilePath (or stdout when unset), unless WithWriter is given.

func NewContextLogger

func NewContextLogger(parent context.Context, opts ...NewLoggerOption) (context.Context, log.Logger)

NewContextLogger stores a trace identifier and logger in a child of parent and returns both.

func NewNopLogger

func NewNopLogger() log.Logger

NewNopLogger returns a logger that discards all log records.

func NewTeeLogger

func NewTeeLogger(lgs ...log.Logger) log.Logger

NewTeeLogger returns a logger that writes each record to every logger in lgs.

func NewTraceId

func NewTraceId() string

NewTraceId returns a new unique trace identifier.

func NewTraceLogger

func NewTraceLogger(options ...NewLoggerOption) log.Logger

NewTraceLogger returns a logger annotated with a trace identifier.

func NewWriterAdapter

func NewWriterAdapter(logger log.Logger, options ...WriterAdapterOption) io.Writer

NewWriterAdapter returns an io.Writer that logs each write through logger.

func RegisterLogFormat

func RegisterLogFormat(logFmt AllowedFormat, f LoggerCreateFunc)

RegisterLogFormat registers f as the constructor for logFmt.

func SetCallerFormatter added in v1.1.1

func SetCallerFormatter(f func(file string, line int) string)

SetCallerFormatter replaces the function that formats caller file and line values.

func SetDefaultLogger

func SetDefaultLogger(logger log.Logger)

SetDefaultLogger replaces the process-wide default logger.

func SetSourceCodeDir

func SetSourceCodeDir(d string)

SetSourceCodeDir overrides the prefix stripped from caller file paths.

func WrapKeyName

func WrapKeyName(name string) fmt.Stringer

WrapKeyName returns name as a KeyName so it can be used as a log key.

Types

type AllowedFormat

type AllowedFormat string

AllowedFormat is a settable identifier for the output format that the logger can have.

const (
	// FormatJSON encodes each log record as JSON.
	FormatJSON AllowedFormat = "json"
	// FormatLogfmt encodes each log record in logfmt.
	FormatLogfmt AllowedFormat = "logfmt"
)

func GetRegisteredLogFormats

func GetRegisteredLogFormats() []AllowedFormat

GetRegisteredLogFormats returns the names of all registered log formats.

func (*AllowedFormat) Set

func (f *AllowedFormat) Set(s string) error

Set updates the value of the allowed format.

func (AllowedFormat) String

func (f AllowedFormat) String() string

String returns the format name.

func (AllowedFormat) Valid

func (f AllowedFormat) Valid() error

Valid reports whether f is a registered log format.

type AllowedLevel

type AllowedLevel string

AllowedLevel is a settable identifier for the minimum level a log entry must have.

const (
	// LevelDebug allows debug, info, warn, and error log entries.
	LevelDebug AllowedLevel = "debug"
	// LevelInfo allows info, warn, and error log entries.
	LevelInfo AllowedLevel = "info"
	// LevelWarn allows warn and error log entries.
	LevelWarn AllowedLevel = "warn"
	// LevelError allows only error log entries.
	LevelError AllowedLevel = "error"
)

func (*AllowedLevel) Set

func (l *AllowedLevel) Set(s string) error

Set updates the value of the allowed level.

func (AllowedLevel) String

func (l AllowedLevel) String() string

String returns the log level name.

func (*AllowedLevel) UnmarshalYAML

func (l *AllowedLevel) UnmarshalYAML(unmarshal func(interface{}) error) error

UnmarshalYAML implements yaml.Unmarshaler by parsing a log level string.

func (AllowedLevel) Valid

func (l AllowedLevel) Valid() error

Valid reports whether l is a recognized log level.

type Config

type Config struct {
	// Level is the minimum severity that will be logged.
	Level *AllowedLevel
	// Format selects the encoder used for log records.
	Format *AllowedFormat
	// FilePath is the log file path, or a special device such as /dev/stdout.
	FilePath string
	// FileMaxAge is how long rotated log files are retained.
	FileMaxAge time.Duration
	// FileRotationSize is the maximum size of a log file before rotation, as a capacity string.
	FileRotationSize string
	// FileRotationTime is how often log files are rotated by time.
	FileRotationTime time.Duration
}

Config is a struct containing configurable settings for the logger

var DefaultLoggerConfig *Config

DefaultLoggerConfig is the process-wide default Config used when none is provided.

func MustNewConfig

func MustNewConfig(level string, format string) *Config

MustNewConfig returns a Config with the given level and format, panicking on invalid values.

type KeyName

type KeyName string

KeyName is a typed log key that implements fmt.Stringer.

func (KeyName) String

func (n KeyName) String() string

String returns the key name as a string.

type LoggerCreateFunc

type LoggerCreateFunc func(w io.Writer) log.Logger

LoggerCreateFunc builds a logger that writes to w.

type NewLoggerOption

type NewLoggerOption func(*newLoggerOptions)

NewLoggerOption configures a logger created by New, NewTraceLogger, or NewContextLogger.

func WithConfig

func WithConfig(c *Config) NewLoggerOption

WithConfig applies c when creating a logger.

func WithKeyValues

func WithKeyValues(keyvals ...interface{}) NewLoggerOption

WithKeyValues attaches additional key/value pairs to the logger.

func WithLogger

func WithLogger(l log.Logger) NewLoggerOption

WithLogger uses l as the base logger instead of the default logger.

func WithTraceId

func WithTraceId(traceId string) NewLoggerOption

WithTraceId sets the trace identifier attached to a trace logger.

func WithWriter

func WithWriter(w io.Writer) NewLoggerOption

WithWriter writes log output to w instead of the writer derived from Config.

type Option

type Option func(l log.Logger) log.Logger

Option wraps a logger to attach extra fields when retrieving a context logger.

func WithCaller

func WithCaller(layer int) Option

WithCaller returns an Option that records the call site at the given stack layer.

func WithMethod

func WithMethod(skip ...int) Option

WithMethod returns an Option that records the calling function name as a "method" field.

func WithPrint

func WithPrint(raw interface{}) Option

WithPrint returns an Option that writes raw to the logger's writer after each log record.

type WriterAdapter

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

WriterAdapter is an io.Writer that forwards each write as a log record.

func (WriterAdapter) Write

func (a WriterAdapter) Write(p []byte) (n int, err error)

Write logs p as a single message using the configured key and prefix rules.

type WriterAdapterOption

type WriterAdapterOption func(*WriterAdapter)

WriterAdapterOption configures a WriterAdapter created by NewWriterAdapter.

func MessageKey

func MessageKey(key string) WriterAdapterOption

MessageKey returns a WriterAdapterOption that sets the key used for the logged message.

func Prefix

func Prefix(prefix string, joinPrefixToMsg bool) WriterAdapterOption

Prefix returns a WriterAdapterOption that strips prefix from each write. When joinPrefixToMsg is true, the prefix is put back on the logged message.

Directories

Path Synopsis
Package flag registers command-line flags for configuring the log package.
Package flag registers command-line flags for configuring the log package.

Jump to

Keyboard shortcuts

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