Documentation ¶
Overview ¶
Package log provides logging functionality.
Index ¶
- Constants
- func RecoverPanics()
- func ReplaceLevelName(_ []string, a slog.Attr) slog.Attr
- func SetDefaultLevel(level Level)
- func StdLogger(logger *Logger) *log.Logger
- type Config
- type ConsoleHandler
- type ConsoleHandlerOptions
- type ExtendedLogger
- type FanOutHandler
- type Field
- func Bool(key string, val bool) Field
- func Duration(key string, val time.Duration) Field
- func Err(err error) Field
- func Float32(key string, val float32) Field
- func Float64(key string, val float64) Field
- func Int(key string, val int) Field
- func Int16(key string, val int16) Field
- func Int32(key string, val int32) Field
- func Int64(key string, val int64) Field
- func Int8(key string, val int8) Field
- func Object(key string, val any) Field
- func String(key, val string) Field
- func Stringer(key string, val fmt.Stringer) Field
- func Strings(key string, val []string) Field
- func Time(key string, val time.Time) Field
- func Uint(key string, val uint) Field
- func Uint16(key string, val uint16) Field
- func Uint32(key string, val uint32) Field
- func Uint64(key string, val uint64) Field
- func Uint8(key string, val uint8) Field
- type Level
- type Logger
- func (l *Logger) Closer(closer io.Closer, msg string)
- func (l *Logger) CloserCtx(ctx context.Context, closer closerCtx, msg string)
- func (l *Logger) Debug(msg string, args ...any)
- func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Enabled(ctx context.Context, level Level) bool
- func (l *Logger) Error(msg string, args ...any)
- func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Fatal(msg string, args ...any)
- func (l *Logger) FatalContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Info(msg string, args ...any)
- func (l *Logger) InfoContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Level() Level
- func (l *Logger) Log(ctx context.Context, level Level, msg string, args ...any)
- func (l *Logger) Named(name string) *Logger
- func (l *Logger) SetLevel(level Level)
- func (l *Logger) Trace(msg string, args ...any)
- func (l *Logger) TraceContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) Warn(msg string, args ...any)
- func (l *Logger) WarnContext(ctx context.Context, msg string, args ...any)
- func (l *Logger) With(fields ...any) *Logger
- type TemporalLogger
- type TestingT
- type Writer
Constants ¶
const ( // TraceLevel logs are typically voluminous, and are usually disabled in // production. TraceLevel = slog.LevelDebug << 1 // DebugLevel logs are typically voluminous, and are usually disabled in // production. DebugLevel = slog.LevelDebug // InfoLevel is the default logging priority. InfoLevel = slog.LevelInfo // WarnLevel logs are more important than Info, but don't need individual // human review. WarnLevel = slog.LevelWarn // ErrorLevel logs are high-priority. If an application is running smoothly, // it shouldn't generate any error-level logs. ErrorLevel = slog.LevelError // FatalLevel logs a message, then calls os.Exit(1). FatalLevel = slog.LevelError << 1 )
Log levels.
const DefaultTimeFormat = "2006-01-02 15:04:05"
DefaultTimeFormat is a slimmer default time format used if no other time format is specified.
Variables ¶
This section is empty.
Functions ¶
func RecoverPanics ¶
func RecoverPanics()
RecoverPanics is a function that can be deferred to recover from panics. The panic will be logged as fatal message and the system exits with os.Exit(1), even if logging at FatalLevel is disabled.
func ReplaceLevelName ¶
ReplaceLevelName sets custom defined level names for outputting.
func SetDefaultLevel ¶
func SetDefaultLevel(level Level)
SetDefaultLevel sets the default level for all newly created loggers.
Types ¶
type Config ¶
type Config struct { JSONOutput bool // CallerInfo adds a ("source", "file:line") attribute to the output // indicating the source code position of the log statement. CallerInfo bool Level Level Output io.Writer // Handler handles log records produced by a Logger.. Handler slog.Handler // TimeFormat defines the time format to use, defaults to "2006-01-02 15:04:05" // Outputting of time can be disabled with - for the console handler. TimeFormat string }
Config represents configuration for a logger.
func ConfigForEnv ¶
func ConfigForEnv(environment env.Environment) (Config, error)
ConfigForEnv returns the default config for the given environment. The returned config can be adjusted and used to create a logger with custom config using the NewWithConfig() function.
type ConsoleHandler ¶
type ConsoleHandler struct {
// contains filtered or unexported fields
}
ConsoleHandler formats the logger output in a better human-readable way.
func NewConsoleHandler ¶
func NewConsoleHandler(w io.Writer, opts *ConsoleHandlerOptions) *ConsoleHandler
NewConsoleHandler returns a new console handler.
func (*ConsoleHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
type ConsoleHandlerOptions ¶
type ConsoleHandlerOptions struct { SlogOptions *slog.HandlerOptions TimeFormat string }
ConsoleHandlerOptions are options for a ConsoleHandler. A zero HandlerOptions consists entirely of default values.
type ExtendedLogger ¶
type ExtendedLogger struct {
// contains filtered or unexported fields
}
ExtendedLogger implements a logger compatible with multiple common packages.
func NewExtendedLogger ¶
func NewExtendedLogger(logger *Logger) *ExtendedLogger
NewExtendedLogger returns a new extended logger based on the given logger.
func (*ExtendedLogger) Debugf ¶
func (l *ExtendedLogger) Debugf(format string, v ...interface{})
Debugf logs a message at DebugLevel.
func (*ExtendedLogger) Errorf ¶
func (l *ExtendedLogger) Errorf(format string, v ...interface{})
Errorf logs a message at ErrorLevel.
func (*ExtendedLogger) Warnf ¶
func (l *ExtendedLogger) Warnf(format string, v ...interface{})
Warnf logs a message at WarnLevel.
type FanOutHandler ¶
type FanOutHandler struct {
// contains filtered or unexported fields
}
FanOutHandler implements a fan-out to multiple log handlers.
func NewFanOutHandler ¶
func NewFanOutHandler(handlers ...slog.Handler) *FanOutHandler
NewFanOutHandler creates a new fan-out log handler.
func (*FanOutHandler) Enabled ¶
Enabled reports whether the handler handles records at the given level. The handler ignores records whose level is lower.
type Field ¶
A Field is a marshaling operation used to add a key-value pair to a logger's context. Most fields are lazily marshaled, so it's inexpensive to add fields to disabled debug-level log statements.
func Object ¶
Object constructs a Field with the given key and value. It should be used for types that are not represented by a specialized Field function. If the passed value type does not implement a custom array or object marshaller, reflection will be used for the fields of the type. Using reflection for performance critical code paths should be avoided.
type Level ¶
Level is a logging priority. Higher levels are more important.
func DefaultLevel ¶
func DefaultLevel() Level
DefaultLevel returns the current default level for all loggers newly created with New().
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger provides fast, leveled, structured logging. All methods are safe for concurrent use.
func NewNop ¶
func NewNop() *Logger
NewNop creates a no-op logger which never writes logs to the output. Useful for tests.
func NewTestLogger ¶
NewTestLogger builds a new Logger that logs all messages to the given testing.TB. The logs get only printed if a test fails or if the test is run with -v verbose flag.
func NewWithConfig ¶
NewWithConfig creates a new logger for the given config. If no level is set in the config, it will use the default level of this package.
func (*Logger) Closer ¶
Closer calls the closer function and if an error gets returned it logs an error. This function is useful when using patterns like defer resp.Body.Close() which now become: defer logger.Closer(resp.Body, "closing body").
func (*Logger) CloserCtx ¶
CloserCtx calls the closer function and if an error gets returned it logs an error.
func (*Logger) DebugContext ¶
DebugContext logs at LevelDebug with the given context.
func (*Logger) Enabled ¶
Enabled reports whether l emits log records at the given context and level. nolint: contextcheck
func (*Logger) ErrorContext ¶
ErrorContext logs at LevelError with the given context.
func (*Logger) FatalContext ¶
FatalContext logs at FatalLevel with the given context.
func (*Logger) InfoContext ¶
InfoContext logs at LevelInfo with the given context.
func (*Logger) Log ¶
Log emits a log record with the current time and the given level and message. nolint: contextcheck
func (*Logger) Named ¶
Named adds a new path segment to the logger's name. Segments are joined by periods. By default, Loggers are unnamed.
func (*Logger) TraceContext ¶
TraceContext logs at TraceLevel with the given context.
func (*Logger) WarnContext ¶
WarnContext logs at LevelWarn with the given context.
type TemporalLogger ¶
type TemporalLogger struct {
// contains filtered or unexported fields
}
TemporalLogger implements a logger compatible with the temporal package.
func NewTemporalLogger ¶
func NewTemporalLogger(logger *Logger) *TemporalLogger
NewTemporalLogger returns a new temporal logger based on the given logger.
func (*TemporalLogger) Debug ¶
func (l *TemporalLogger) Debug(msg string, keyValues ...any)
Debug logs a message at DebugLevel.
func (*TemporalLogger) Error ¶
func (l *TemporalLogger) Error(msg string, keyValues ...any)
Error logs a message at ErrorLevel.
func (*TemporalLogger) Info ¶
func (l *TemporalLogger) Info(msg string, keyValues ...any)
Info logs a message at InfoLevel.
func (*TemporalLogger) Warn ¶
func (l *TemporalLogger) Warn(msg string, keyValues ...any)
Warn logs a message at WarnLevel.
type TestingT ¶
type TestingT interface { // Logf logs the given message without failing the test. Logf(string, ...interface{}) // Errorf logs the given message and marks the test as failed. Errorf(string, ...interface{}) // FailNow marks the test as failed and stops execution of that test. FailNow() // Helper marks the calling function as a test helper function. Helper() }
TestingT is a subset of the API provided by all *testing.T and *testing.B objects.