corelog

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: Apache-2.0 Imports: 11 Imported by: 8

README

Corelog

Logging library used across Source Network projects.

Usage

package main

import "github.com/sourcenetwork/corelog"

var log = corelog.NewLogger("main")

func main() {
    // with attributes
    log.Info("message", corelog.String("key", "val"))

    // with context
    log.InfoContext(ctx, "message", corelog.Int("key", 10))

    // with error stacktrace
    log.ErrorE("message", err, corelog.Bool("key", true))

    // with common attributes
    attrs := log.WithAttrs(corelog.Float64("key", float64(1.234)))
    attrs.Info("message")

    // with common group
    group := log.WithGroup("group")
    group.Info("message", corelog.Any("key", struct{}{}))
}

Configuration

Default config values can be set via environment variables.

Env Description Values
LOG_LEVEL sets logging level info error
LOG_FORMAT sets logging format json text
LOG_STACKTRACE enables stacktraces true false
LOG_SOURCE enables source location true false
LOG_OUTPUT sets the output path stderr stdout
LOG_OVERRIDES logger specific overrides net,level=info;core,output=stdout
LOG_NO_COLOR disable color text output true false

Documentation

Index

Constants

View Source
const (
	// LevelDebug specifies info log level.
	LevelInfo = "info"
	// LevelDebug specifies error log level.
	LevelError = "error"
	// FormatText specifies text output for a logger.
	FormatText = "text"
	// FormatJSON specifies json output for a logger.
	FormatJSON = "json"
	// OutputStdout specifies stdout output for a logger.
	OutputStdout = "stdout"
	// OutputStderr specifies stderr output for a logger.
	OutputStderr = "stderr"
)

Variables

This section is empty.

Functions

func Any added in v0.0.5

func Any(key string, value any) slog.Attr

Any returns an slog.Attr for the supplied value.

func Bool added in v0.0.5

func Bool(key string, value bool) slog.Attr

Bool returns an slog.Attr for a bool.

func Duration added in v0.0.5

func Duration(key string, value time.Duration) slog.Attr

Duration returns an slog.Attr for a time.Duration.

func Float64 added in v0.0.5

func Float64(key string, value float64) slog.Attr

Float64 returns an slog.Attr for a floating-point number.

func Group added in v0.0.5

func Group(key string, args ...any) slog.Attr

Group returns an slog.Attr for a Group Value.

func Int added in v0.0.5

func Int(key string, value int) slog.Attr

Int converts an int to an int64 and returns an slog.Attr with that value.

func Int64 added in v0.0.5

func Int64(key string, value int64) slog.Attr

Int64 returns an slog.Attr for an int64.

func SetConfig added in v0.0.3

func SetConfig(cfg Config)

SetConfig sets the config values for all loggers.

func SetConfigOverride added in v0.0.3

func SetConfigOverride(name string, cfg Config)

SetConfigOverride sets the config override for the given named logger.

func SetConfigOverrides added in v0.0.3

func SetConfigOverrides(text string)

SetConfigOverrides parses and sets config overrides from the given text.

Overrides are separated by ";", and override values are comma separated, where the first value is the name, and the remaining values are key value pairs separated by "=".

func String added in v0.0.5

func String(key string, value string) slog.Attr

String returns an slog.Attr for a string value.

func Time added in v0.0.5

func Time(key string, value time.Time) slog.Attr

Time returns an slog.Attr for a time.Time.

func Uint64 added in v0.0.5

func Uint64(key string, value uint64) slog.Attr

Uint64 returns an slog.Attr for a uint64.

Types

type Config

type Config struct {
	// Level specifies the logging level.
	Level string
	// Format specifies the output format of the logger.
	Format string
	// EnableStackTrace enables logging error stack traces.
	EnableStackTrace bool
	// EnableSource enables logging the source location.
	EnableSource bool
	// Output specifies the output path for the logger.
	Output string
	// DisableColor specifies if colored output is disabled.
	DisableColor bool
}

Config contains general settings for a logger.

func DefaultConfig added in v0.0.3

func DefaultConfig() Config

DefaultConfig returns a config with default values.

The default values are derived from environment variables.

func GetConfig added in v0.0.3

func GetConfig(name string) Config

GetConfig returns the config for a named logger.

type Logger

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

Logger is a logger that wraps the slog package.

func NewLogger

func NewLogger(name string) *Logger

NewLogger returns a new named logger.

func (*Logger) Error

func (l *Logger) Error(msg string, args ...slog.Attr)

Error logs a message at error log level.

func (*Logger) ErrorContext

func (l *Logger) ErrorContext(ctx context.Context, msg string, args ...slog.Attr)

ErrorContext logs a message at error log level.

func (*Logger) ErrorContextE

func (l *Logger) ErrorContextE(ctx context.Context, msg string, err error, args ...slog.Attr)

ErrorContextE logs a message at error log level with an error stacktrace.

func (*Logger) ErrorE

func (l *Logger) ErrorE(msg string, err error, args ...slog.Attr)

ErrorE logs a message at error log level with an error stacktrace.

func (*Logger) Info

func (l *Logger) Info(msg string, args ...slog.Attr)

Info logs a message at info log level.

func (*Logger) InfoContext

func (l *Logger) InfoContext(ctx context.Context, msg string, args ...slog.Attr)

InfoContext logs a message at info log level.

func (*Logger) WithAttrs

func (l *Logger) WithAttrs(attrs ...slog.Attr) *Logger

WithAttrs returns a new Logger whose attributes consist of both the receiver's attributes and the arguments.

func (*Logger) WithGroup

func (l *Logger) WithGroup(name string) *Logger

WithGroup returns a new Logger with the given group appended to the receiver's existing groups.

Jump to

Keyboard shortcuts

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