Documentation
¶
Overview ¶
Package log provides leveled, contextual loggers built on go-kit/log.
Index ¶
- Constants
- Variables
- func Caller(depth int) log.Valuer
- func GetContextLogger(ctx context.Context, options ...Option) log.Logger
- func GetDefaultLogger() log.Logger
- func GetSourceCodeDir(codeRelativePath string, depth ...int) string
- func GetTraceId(ctx context.Context) string
- func New(opts ...NewLoggerOption) log.Logger
- func NewContextLogger(parent context.Context, opts ...NewLoggerOption) (context.Context, log.Logger)
- func NewNopLogger() log.Logger
- func NewTeeLogger(lgs ...log.Logger) log.Logger
- func NewTraceId() string
- func NewTraceLogger(options ...NewLoggerOption) log.Logger
- func NewWriterAdapter(logger log.Logger, options ...WriterAdapterOption) io.Writer
- func RegisterLogFormat(logFmt AllowedFormat, f LoggerCreateFunc)
- func SetCallerFormatter(f func(file string, line int) string)
- func SetDefaultLogger(logger log.Logger)
- func SetSourceCodeDir(d string)
- func WrapKeyName(name string) fmt.Stringer
- type AllowedFormat
- type AllowedLevel
- type Config
- type KeyName
- type LoggerCreateFunc
- type NewLoggerOption
- type Option
- type WriterAdapter
- type WriterAdapterOption
Constants ¶
const (
// TraceIdName is the log and context key used to store a trace identifier.
TraceIdName = "traceId"
)
Variables ¶
var CallerName = callerName{}
CallerName is the log key used for the call-site file and line number.
var DefaultCaller = Caller(5)
DefaultCaller is a Valuer that records the call site five frames above the logger.
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 GetContextLogger ¶
GetContextLogger returns the logger stored in ctx, or a new trace logger if none is present.
func GetDefaultLogger ¶
GetDefaultLogger returns the process-wide default logger, creating one on first use.
func GetSourceCodeDir ¶
GetSourceCodeDir returns the directory prefix of the current source file after trimming codeRelativePath. Optional depth selects a different runtime.Caller frame.
func GetTraceId ¶
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 ¶
NewNopLogger returns a logger that discards all log records.
func NewTeeLogger ¶
NewTeeLogger returns a logger that writes each record to every logger in lgs.
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
SetCallerFormatter replaces the function that formats caller file and line values.
func SetDefaultLogger ¶
SetDefaultLogger replaces the process-wide default logger.
func SetSourceCodeDir ¶
func SetSourceCodeDir(d string)
SetSourceCodeDir overrides the prefix stripped from caller file paths.
func WrapKeyName ¶
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 ¶
MustNewConfig returns a Config with the given level and format, panicking on invalid values.
type LoggerCreateFunc ¶
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 ¶
Option wraps a logger to attach extra fields when retrieving a context logger.
func WithCaller ¶
WithCaller returns an Option that records the call site at the given stack layer.
func WithMethod ¶
WithMethod returns an Option that records the calling function name as a "method" field.
type WriterAdapter ¶
type WriterAdapter struct {
// contains filtered or unexported fields
}
WriterAdapter is an io.Writer that forwards each write as a log record.
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.