log

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: May 17, 2024 License: GPL-3.0 Imports: 8 Imported by: 61

Documentation

Index

Constants

View Source
const (
	OriginKey string = "origin"
	MethodKey string = "method"
	PathKey   string = "path"
	QueryKey  string = "query"
	CodeKey   string = "code"
	CauseKey  string = "cause"
	BodyKey   string = "body"
	ErrorKey  string = "err"
)

API keys

View Source
const (
	// The key used in contexts to retrieve the logger that should be used
	ContextLogKey NmcContextKey = "nmc_logger"

	// Lumberjack settings
	MaxLogSize    int = 20
	MaxLogBackups int = 3
	MaxLogAge     int = 28
)

Variables

This section is empty.

Functions

func Err

func Err(err error) slog.Attr

Prints an error to a log line

func ReplaceTime

func ReplaceTime(_ []string, a slog.Attr) slog.Attr

Replaces the default time formatting (RFC3339) in a logger with an easier to read format

func WithoutTimeAndLevel

func WithoutTimeAndLevel(groups []string, a slog.Attr) slog.Attr

Removes the time and level from the message

Types

type LogFormat

type LogFormat string

Format for log output

const (
	// Use logfmt
	LogFormat_Logfmt LogFormat = "logfmt"

	// Use JSON
	LogFormat_Json LogFormat = "json"
)

type Logger

type Logger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Logger is a simple wrapper for a slog Logger that writes to a file on disk.

func FromContext

func FromContext(ctx context.Context) (*Logger, bool)

Retrieves the logger from the context

func NewLogger

func NewLogger(logFilePath string, options LoggerOptions) (*Logger, error)

Creates a new logger

func (*Logger) Close

func (l *Logger) Close()

Closes the log file

func (*Logger) CreateContextWithLogger

func (l *Logger) CreateContextWithLogger(parent context.Context) context.Context

Creates a copy of the parent context with the logger put into the ContextLogKey value

func (*Logger) CreateSubLogger

func (l *Logger) CreateSubLogger(origin string) *Logger

Create a clone of the logger that prints each message with the "origin" attribute. The underlying file handle isn't copied, so calling Close() on the sublogger won't do anything.

func (*Logger) GetFilePath

func (l *Logger) GetFilePath() string

Get the path of the file this logger is writing to

func (*Logger) Rotate

func (l *Logger) Rotate() error

Rotate the log file, migrating the current file to an old backup and starting a new one

type LoggerOptions

type LoggerOptions struct {

	// The maximum size (in megabytes) of the log file before it gets rotated
	MaxSize int

	// The maximum number of old log files to retain.
	// Use 0 to retain all backups.
	MaxBackups int

	// The maximum number of days to retain old log files based on the timestamp encoded in their filename.
	// Use 0 to always preserve old logs.
	MaxAge int

	// True to format the timestamps in backup files in the computer's local time; false to format in UTC
	LocalTime bool

	// True to compress rotated log files using gzip
	Compress bool

	// The format to use when printing logs
	Format LogFormat

	// The minimum record level that will be logged
	Level slog.Level

	// True to include the source code position of the log statement in log messages
	AddSource bool
}

Options for logging

type NmcContextKey

type NmcContextKey string

type TerminalLogger

type TerminalLogger struct {
	*slog.Logger
	// contains filtered or unexported fields
}

Simple logger that logs colored messages directly to the terminal (stdout) without time or level. Useful for CLI applications that only log debug messages, where such things aren't relevant.

func NewTerminalLogger

func NewTerminalLogger(debugEnabled bool, logColor color.Attribute) *TerminalLogger

Creates a new TerminalLogger instance

Jump to

Keyboard shortcuts

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