corelog

package module
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: Apache-2.0 Imports: 10 Imported by: 8

README

Corelog

Logging library used across Source Network projects.

Usage

package main

import (
    "log/slog"

    "github.com/sourcenetwork/corelog"
)

var log = corelog.NewLogger("main")

func main() {
    log.Debug("doing stuff", "key", "val")

    attrs := log.WithAttrs(slog.Any("key", "val"))
    attrs.Info("doing stuff with attrs")

    group := log.WithGroup("group")
    group.Info("doing stuff with group", "key", "val")
}
Usage with Cobra

Add config flags to Cobra.

package main

import (
    "github.com/sourcenetwork/corelog"
    "github.com/spf13/cobra"
)

func main() {
    cmd := &cobra.Command{...}
    cmd.PersistentFlags().AddGoFlagSet(corelog.FlagSet)
}

Configuration

Loggers are configured via environment variables and/or command line flags.

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

Documentation

Index

Constants

View Source
const (
	// LevelDebug specifies debug log level.
	LevelDebug = "debug"
	// LevelDebug specifies info log level.
	LevelInfo = "info"
	// LevelDebug specifies error log level.
	LevelError = "error"
	// LevelDebug specifies fatal log level.
	LevelFatal = "fatal"
	// FormatText specifies text output for a logger.
	FormatText = "text"
	// FormatText 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

View Source
var FlagSet = flag.NewFlagSet("corelog", flag.ContinueOnError)

FlagSet is the set of flags used to configure the logging package.

Functions

This section is empty.

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
	// Overrides is a mapping of logger names to override configs.
	Overrides map[string]Config
}

Config contains general settings for a logger.

func LoadConfig

func LoadConfig() Config

LoadConfig returns a config with values set from environment variables and cli flags.

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 logger configured with the default config.

func NewLoggerWithConfig

func NewLoggerWithConfig(name string, config Config) *Logger

NewLoggerWithConfig returns a new logger configured with the given config.

func (*Logger) Debug

func (l *Logger) Debug(msg string, args ...any)

Debug logs a message at debug log level.

func (*Logger) DebugContext

func (l *Logger) DebugContext(ctx context.Context, msg string, args ...any)

DebugContext logs a message at debug log level.

func (*Logger) Error

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

Error logs a message at error log level.

func (*Logger) ErrorContext

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

ErrorContext logs a message at error log level.

func (*Logger) ErrorContextE

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

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

func (*Logger) ErrorE

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

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

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, args ...any)

Fatal logs a message at fatal log level.

func (*Logger) FatalContext

func (l *Logger) FatalContext(ctx context.Context, msg string, args ...any)

FatalContext logs a message at fatal log level and calls os.Exit(1).

func (*Logger) FatalContextE

func (l *Logger) FatalContextE(ctx context.Context, msg string, err error, args ...any)

FatalContextE logs a message at fatal log level with an error stacktrace and calls os.Exit(1).

func (*Logger) FatalE

func (l *Logger) FatalE(msg string, err error, args ...any)

FatalE logs a message at fatal log level with an error stacktrace.

func (*Logger) Info

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

Info logs a message at info log level.

func (*Logger) InfoContext

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

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
JackTT - Gopher 🇻🇳