logger

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Aug 19, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package logger package provides a constructor and wrapper methods for an underlying logger (currently - slog.Logger).

Index

Constants

View Source
const (
	// DefaultTimeTemplate is a default time format string.
	DefaultTimeTemplate = "02.01.2006 15:04:05.000"
	// DefaultLogType is a default log type.
	DefaultLogType = "json"
	// DefaultLevel is a default log level.
	DefaultLevel = "error"
	// DefaultLevelValue is a default log level value.
	DefaultLevelValue = slog.LevelError
	// DefaultWriter is a default writer to use for logging.
	DefaultWriter = "stdout"
)

Variables

View Source
var DefaultWriterValue = os.Stdout

DefaultWriterValue is a default writer value.

Functions

This section is empty.

Types

type Config

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

Config defines an inner logger configuration.

type Logger

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

Logger is a wrapper structure for an underlying logger.

func NewLogger

func NewLogger(opts ...Option) (*Logger, error)

NewLogger returns a new Logger with the given log type and level. If no opts are provided, it returns a default logger.

The log type can be "text" or "json". The log level can be "debug", "info", "warn" or "error".

timeTemplate is a time format string. Any format which is valid for time.Time format is acceptable.

Empty log level corresponds to "error", as well as empty log type corresponds to "json". Empty time format is equal to the default value which is "02.01.2006 15:04:05.000". Empty writer option equals to using os.Stdout. Custom writer might be set using WithWriter option.

If the log type or level is unknown, it returns an error.

func (Logger) Debug

func (logg Logger) Debug(ctx context.Context, msg string, args ...any)

Debug logs a message with level Debug on the standard logger.

func (Logger) Error

func (logg Logger) Error(ctx context.Context, msg string, args ...any)

Error logs a message with level Error on the standard logger.

func (Logger) Fatal

func (logg Logger) Fatal(ctx context.Context, msg string, args ...any)

Fatal logs a message with level Error on the standard logger and then calls os.Exit(1).

func (Logger) Info

func (logg Logger) Info(ctx context.Context, msg string, args ...any)

Info logs a message with level Info on the standard logger.

func (Logger) Warn

func (logg Logger) Warn(ctx context.Context, msg string, args ...any)

Warn logs a message with level Warn on the standard logger.

func (Logger) With

func (logg Logger) With(args ...any) *Logger

With returns a new Logger that adds the given key-value pairs to the logger's context.

type Option

type Option func(c *Config) error

Option defines a function that allows to configure underlying logger on construction.

func SetDefaults

func SetDefaults() Option

SetDefaults is a wrapper over WithConfig, passing empty config.

func WithConfig

func WithConfig(cfg map[string]any) Option

WithConfig allows to apply custom configuration. Expected following config structure:

{
		format        string, // "text" or "json"
		level         string, // "debug", "info", "warn", "error"
		time_template string, // any valid time format
		log_stream:   string, // "stdout", "stderr"
}

func WithWriter

func WithWriter(w io.Writer) Option

WithWriter allows to apply custom configuration.

Jump to

Keyboard shortcuts

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