Documentation
¶
Overview ¶
Package logger package provides a constructor and wrapper methods for an underlying logger (currently - slog.Logger).
Index ¶
- Constants
- Variables
- type Config
- type Logger
- func (logg Logger) Debug(ctx context.Context, msg string, args ...any)
- func (logg Logger) Error(ctx context.Context, msg string, args ...any)
- func (logg Logger) Fatal(ctx context.Context, msg string, args ...any)
- func (logg Logger) Info(ctx context.Context, msg string, args ...any)
- func (logg Logger) Warn(ctx context.Context, msg string, args ...any)
- func (logg Logger) With(args ...any) *Logger
- type Option
Constants ¶
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 ¶
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 ¶
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) Fatal ¶
Fatal logs a message with level Error on the standard logger and then calls os.Exit(1).
type Option ¶
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 ¶
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 ¶
WithWriter allows to apply custom configuration.