Documentation
¶
Index ¶
- Constants
- Variables
- type ColorTheme
- type Config
- type ConsoleConfig
- type Field
- type FileConfig
- type Formatter
- type Level
- type Logger
- type Logging
- type PrefixLogger
- type Record
- func (r *Record) WithField(key string, value any) *Record
- func (r *Record) WithFieldf(key string, format string, a ...any) *Record
- func (r *Record) WithFields(keysValues ...any) *Record
- func (r *Record) WithLevel(lv Level) *Record
- func (r *Record) WithLogger(logger string) *Record
- func (r *Record) WithMessage(msg string) *Record
- func (r *Record) WithMessagef(msg string, args ...any) *Record
- func (r *Record) WithStack(stack []byte) *Record
- func (r *Record) WithStatus(st status.Status) *Record
- func (r *Record) WithTime(t time.Time) *Record
- type RecordBuilder
- type Writer
Constants ¶
const TEST_LOG = "TEST_LOG"
TEST_LOG specifies an env variable that can be used to override the log level.
Variables ¶
var TestLevel = LevelError
TestLevel is the default log level for tests, you can change it in your modules for tests.
Functions ¶
This section is empty.
Types ¶
type ColorTheme ¶
type ColorTheme struct { Time terminal.Color // Logger terminal.Color FieldKey terminal.Color FieldEqualSign terminal.Color FieldValue terminal.Color Levels map[Level]terminal.Color }
ColorTheme specifies the terminal colors.
func DefaultColorTheme ¶
func DefaultColorTheme() ColorTheme
DefaultColorTheme returns the default terminal colors.
type Config ¶
type Config struct { Console *ConsoleConfig `json:"console"` File *FileConfig `json:"file"` }
Config specifies the logging configuration.
func ReadConfig ¶
ReadConfig reads a configuration from a JSON or YAML file.
type ConsoleConfig ¶
type ConsoleConfig struct { Enabled bool `json:"enabled"` Level Level `json:"level"` Color bool `json:"color"` Theme ColorTheme `json:"theme"` }
type FileConfig ¶
type FileConfig struct { Enabled bool `yaml:"enabled"` Path string `yaml:"path"` Level Level `yaml:"level"` MaxSize int `yaml:"max_size"` // Maximum log file size in megabytes MaxAge int `yaml:"max_age"` // Maximum days to retain old log files MaxBackups int `yaml:"max_backups"` // Maximum number of old log files to retain }
type Level ¶
type Level int
func LevelFromString ¶
func TestLevelEnv ¶
func TestLevelEnv() Level
TestLevelEnv returns a log level from the env variable TEST_LOG or the default test level.
func (Level) MarshalJSON ¶
func (Level) MarshalText ¶
func (*Level) UnmarshalJSON ¶
func (*Level) UnmarshalText ¶
type Logger ¶
type Logger interface { // Name returns the logger name. Name() string // Logger returns a child logger. Logger(name string) Logger // WithFields returns a chained logger with the default fields. WithFields(keyValuePairs ...any) Logger // Enabled returns true if a level is enabled. Enabled(level Level) bool // Begin returns a record builder with the info level. Begin() RecordBuilder // Write sets the logger if abset, adds the default fields and writes the record. Write(rec *Record) error // Trace logs a trace message. Trace(msg string, keyValues ...any) // TraceStatus logs a trace message with a status and a stack trace. TraceStatus(msg string, st status.Status, keyValues ...any) // TraceOn returns true if trace level is enabled. TraceOn() bool // Debug logs a debug message. Debug(msg string, keyValues ...any) // DebugStatus logs a debug message with a status and a stack trace. DebugStatus(msg string, st status.Status, keyValues ...any) // DebugOn returns true if debug level is enabled. DebugOn() bool // Info logs an info message. Info(msg string, keyValues ...any) // InfoStatus logs an info message with a status and a stack trace. InfoStatus(msg string, st status.Status, keyValues ...any) // InfoOn returns true if info level is enabled. InfoOn() bool // Notice logs a notice message. Notice(msg string, keyValues ...any) // NoticeStatus logs a notice message with a status and a stack trace. NoticeStatus(msg string, st status.Status, keyValues ...any) // NoticeOn return true if notice level is enabled. NoticeOn() bool // Warn logs a warning message. Warn(msg string, keyValues ...any) // WarnStatus logs a warning message with a status and a stack trace. WarnStatus(msg string, st status.Status, keyValues ...any) // WarnOn returns true if warn level is enabled. WarnOn() bool // Error logs an error message. Error(msg string, keyValues ...any) // ErrorStatus logs an error message with a status and a stack trace. ErrorStatus(msg string, st status.Status, keyValues ...any) // ErrorOn returns true if error level is enabled. ErrorOn() bool // Fatal logs a fatal mesage. Fatal(msg string, keyValues ...any) // FatalStatus logs a fatal message with a status and a stack trace. FatalStatus(msg string, st status.Status, keyValues ...any) // FatalOn returns true if fatal level is enabled. FatalOn() bool }
func TestLoggerDebug ¶
TestLoggerDebug returns a new test logger with the debug level.
func TestLoggerInfo ¶
TestLoggerInfo returns a new test logger with the info level.
type Logging ¶
type Logging interface { // Main returns the main logger. Main() Logger // Logger returns a logger with the given name or creates a new one. Logger(name string) Logger // Enabled returns true if logging is enabled for the given level. Enabled(level Level) bool // Write writes a record. Write(rec *Record) error }
Logging is a logging service.
func Default ¶
func Default() Logging
Default returns a new logging service with the default config.
func Init ¶
Init initializes and returns a new logging service, uses the default config when config is nil.
type PrefixLogger ¶
type PrefixLogger interface { Logger // SetPrefix sets the logger prefix. SetPrefix(s string) // ClearPrefix clears the logger prefix. ClearPrefix() }
PrefixLogger is a logger that adds a prefix to messages, including messages from child loggers.
func NewPrefixLogger ¶
func NewPrefixLogger(logger Logger) PrefixLogger
NewPrefixLogger returns a new prefix logger.
type Record ¶
type Record struct { Time time.Time `json:"time"` Level Level `json:"level"` Logger string `json:"logger"` Message string `json:"message"` Fields []Field `json:"fields"` Stack []byte `json:"stack"` }
func (*Record) WithFieldf ¶
func (*Record) WithFields ¶
func (*Record) WithLogger ¶
func (*Record) WithMessage ¶
type RecordBuilder ¶
type RecordBuilder interface { // Build builds and returns the record, but does not send it. Build() *Record // Send sends the record. Send() // Level sets the record level. Level(lv Level) RecordBuilder // Message sets the record message. Message(msg string) RecordBuilder // Messagef formats and sets the record message. Messagef(msg string, args ...any) RecordBuilder // Stack adds a stack trace. Stack(stack []byte) RecordBuilder // Status adds a status with an optional stack trace. Status(st status.Status) RecordBuilder // Field adds a field to the record. Field(key string, value any) RecordBuilder // Fieldf formats a field value and adds a field to the record. Fieldf(key string, format string, a ...any) RecordBuilder // Fields adds fields to the record. Fields(keyValuePairs ...any) RecordBuilder // Trace sets the level to trace and adds the message. Trace(msg string) RecordBuilder // Tracef sets the level to trace and formats the message. Tracef(format string, args ...any) RecordBuilder // Debug sets the level to debug and adds the message. Debug(msg string) RecordBuilder // Debugf sets the level to debug and formats the message. Debugf(format string, args ...any) RecordBuilder // Info sets the level to info and adds the message. Info(msg string) RecordBuilder // Infof sets the level to info and formats the message. Infof(format string, args ...any) RecordBuilder // Notice sets the level to notice and adds the message. Notice(msg string) RecordBuilder // Noticef sets the level to notice and formats the message. Noticef(format string, args ...any) RecordBuilder // Warn sets the level to warn and adds the message. Warn(msg string) RecordBuilder // Warnf sets the level to warn and formats the message. Warnf(format string, args ...any) RecordBuilder // Error sets the level to error and adds the message. Error(msg string) RecordBuilder // Errorf sets the level to error and formats the message. Errorf(format string, args ...any) RecordBuilder // Fatal sets the level to fatal and adds the message. Fatal(msg string) RecordBuilder // Fatalf sets the level to fatal and formats the message. Fatalf(format string, args ...any) RecordBuilder }