Documentation
¶
Overview ¶
Package log provides config and common log formatting for logruslogrus and zerologzerolog loggers. It is designed to be used in applications, services, jobs, and commands with integrated configuration loading.
Index ¶
- Constants
- Variables
- func IsTerminal(writer io.Writer) bool
- type Buffer
- func (b *Buffer) Bytes() ([]byte, error)
- func (b *Buffer) String() string
- func (b *Buffer) WriteByte(byt byte) *Buffer
- func (b *Buffer) WriteCaller(caller *runtime.Frame) *Buffer
- func (b *Buffer) WriteColored(color, str string) *Buffer
- func (b *Buffer) WriteData(key string, value any) *Buffer
- func (b *Buffer) WriteField(level Level, key string) *Buffer
- func (b *Buffer) WriteLevel(level Level) *Buffer
- func (b *Buffer) WriteString(str string) *Buffer
- func (b *Buffer) WriteValue(value any) *Buffer
- type BufferWriter
- type ColorMode
- type ColorModeString
- type Config
- type Formatter
- type Level
- type LogRusPretty
- type OrderMode
- type OrderModeString
- type Setup
- func (s *Setup) FormatCaller(i any) string
- func (s *Setup) FormatErrFieldName(i any) string
- func (*Setup) FormatErrFieldValue(i any) string
- func (s *Setup) FormatFieldName(i any) string
- func (*Setup) FormatFieldValue(i any) string
- func (s *Setup) FormatLevel(i any) string
- func (*Setup) FormatMessage(i any) string
- func (s *Setup) FormatTimestamp(i any) string
- type ZeroLogPretty
Constants ¶
const ( // DefaultLevel is the default log level. DefaultLevel = LevelInfo // Default report caller flag in configuration. DefaultCaller = false // TImeFormat is defining default time format. DefaultTimeFormat = "2006-01-02 15:04:05.999999" )
Default values for the log configuration.
const ( // LevelPanic is the panic log level. LevelPanic string = "panic" // LevelFatal is the fatal log level. LevelFatal string = "fatal" // LevelError is the error log level. LevelError string = "error" // LevelWarn is the warn log level. LevelWarn string = "warn" // LevelWarn is the warn log level. LevelWarning string = "warning" // LevelInfo is the info log level. LevelInfo string = "info" // LevelDebug is the debug log level. LevelDebug string = "debug" // LevelTrace is the trace log level. LevelTrace string = "trace" )
Log levels.
const ( // ColorRed is the color code for red. ColorRed = "1;91" // ColorGreen is the color code for green. ColorGreen = "1;92" // ColorYellow is the color code for yellow. ColorYellow = "1;93" // ColorGray is the color code for gray. ColorBlue = "1;94" // ColorMagenta is the color code for magenta. ColorMagenta = "1;95" // ColorCyan is the color code for cyan. ColorCyan = "1;96" // ColorGray is the color code for gray. ColorGray = "1;37" // ColorPanic is the color code for panic. ColorPanic = ColorRed // ColorFatal is the color code for fatal. ColorFatal = ColorRed // ColorError is the color code for error. ColorError = ColorRed // ColorWarn is the color code for warn. ColorWarn = ColorYellow // ColorInfo is the color code for info. ColorInfo = ColorCyan // ColorDebug is the color code for debug. ColorDebug = ColorBlue // ColorTrace is the color code for trace. ColorTrace = ColorMagenta // ColorField is the color code for fields. ColorField = ColorGray )
Color codes for the different log levels.
Variables ¶
var ( // DefaultLevelColors is the default color mapping for the log levels. DefaultLevelColors = []string{ ColorPanic, ColorFatal, ColorError, ColorWarn, ColorInfo, ColorDebug, ColorTrace, ColorField, } // DefaultLevelNames is the default name mapping for the log levels. DefaultLevelNames = []string{ "PANIC", "FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE", "-", } // DefaultErrorName is the default name used for marking errors. DefaultErrorName = "error" )
Default values for the log formatter.
Functions ¶
func IsTerminal ¶ added in v0.0.7
IsTerminal checks whether the given writer is a terminal.
Types ¶
type Buffer ¶ added in v0.0.9
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is a buffer for the pretty formatter.
func NewBuffer ¶ added in v0.0.9
func NewBuffer(p *Setup, b BufferWriter) *Buffer
NewBuffer creates a new buffer for the pretty formatter.
func (*Buffer) Bytes ¶ added in v0.0.9
Bytes returns current bytes of the buffer with the current error.
func (*Buffer) String ¶ added in v0.0.9
String returns current string of the buffer with the current error.
func (*Buffer) WriteCaller ¶ added in v0.0.9
WriteCaller writes the caller information to the buffer.
func (*Buffer) WriteColored ¶ added in v0.0.9
WriteColored writes the given text with the given color to the buffer.
func (*Buffer) WriteField ¶ added in v0.0.9
WriteField writes the given key with the given color to the buffer.
func (*Buffer) WriteLevel ¶ added in v0.0.9
WriteLevel writes the given log level to the buffer.
func (*Buffer) WriteString ¶ added in v0.0.9
WriteString writes the given string to the buffer.
func (*Buffer) WriteValue ¶ added in v0.0.9
WriteString writes the given value to the buffer.
type BufferWriter ¶ added in v0.0.9
type BufferWriter interface { // WriteByte writes the given byte to the writer. io.ByteWriter // WriteString writes the given string to the writer. io.StringWriter // Bytes returns the current bytes of the writer. Bytes() []byte // String returns the current string of the writer. String() string }
Buffer is the interface for writing bytes and strings.
type ColorMode ¶ added in v0.0.9
type ColorMode int
ColorMode is the color mode used for logging.
const ( // ColorDefault is the default color mode. ColorDefault = ColorOn // ColorUnset is the unset color mode (activates the default). ColorUnset ColorMode = 0 // ColorOff disables coloring of logs for all outputs files. ColorOff ColorMode = 1 // ColorOn enables coloring of logs for all outputs files. ColorOn ColorMode = ColorFields | ColorLevels // ColorLevels enables coloring for log levels entries only. ColorLevels ColorMode = 2 // ColorFields enables coloring for fields names only. ColorFields ColorMode = 4 )
Color modes.
type ColorModeString ¶ added in v0.0.6
type ColorModeString string
ColorModeString is the color mode used for logging.
const ( // ColorOff disables the color mode. ColorModeOff ColorModeString = "off" // ColorOn enables the color mode. ColorModeOn ColorModeString = "on" // ColorAuto enables the automatic color mode. ColorModeAuto ColorModeString = "auto" // ColorLevel enables the color mode for log level. ColorModeLevels ColorModeString = "levels" // ColorFields enables the color mode for fields. ColorModeFields ColorModeString = "fields" )
Color mode strings.
func (ColorModeString) Parse ¶ added in v0.0.9
func (m ColorModeString) Parse(colorized bool) ColorMode
Parse parses the color mode.
type Config ¶
type Config struct { // Level is defining the logger level (default `info`). Level string `default:"info"` // TImeFormat is defining the time format for timestamps. TimeFormat string `default:"2006-01-02 15:04:05.999999"` // Caller is defining whether the caller is logged (default `false`). Caller bool `default:"false"` // File is defining the file name used for the log output. File string `default:"/dev/stderr"` // ColorMode is defining the color mode used for logging. ColorMode ColorModeString `default:"auto"` // OrderMode is defining the order mode used for logging. OrderMode OrderModeString `default:"on"` // Formatter is defining the formatter used for logging. Formatter Formatter `default:"pretty"` // contains filtered or unexported fields }
Config common configuration for logging.
func (*Config) ParseZeroLevel ¶ added in v0.0.9
ParseZeroLevel parses the log level string and returns the corresponding zerolog level.
func (*Config) SetupRus ¶ added in v0.0.6
SetupRus is setting up and returning the given logger. It particular sets up the log level, the report caller flag, as well as the formatter with color and order mode. If no logger is given, the standard logger is set up.
func (*Config) SetupZero ¶ added in v0.0.9
SetupZero sets up the zerolog logger. It particular it sets up the log level, the report caller flag, as well as the formatter with color and order mode.
func (*Config) ZeroLogger ¶ added in v0.0.13
ZeroLogger returns the zerolog logger.
type Level ¶
type Level int
Level is the log level used for logging.
const ( // PanicLevel is the log level used for panics. PanicLevel Level = 0 // FatalLevel is the log level used for fatal errors. FatalLevel Level = 1 // ErrorLevel is the log level used for errors. ErrorLevel Level = 2 // WarnLevel is the log level used for warnings. WarnLevel Level = 3 // InfoLevel is the log level used for information. InfoLevel Level = 4 // DebugLevel is the log level used for debugging. DebugLevel Level = 5 // TraceLevel is the log level used for tracing. TraceLevel Level = 6 // FieldLevel is and extra log level used for field names. FieldLevel Level = 7 )
Log levels.
func ParseLevel ¶
ParseLevel parses the log level string and returns the corresponding level.
type LogRusPretty ¶ added in v0.0.9
type LogRusPretty struct {
*Setup
}
LogRusPretty formats logs into a pretty format.
func NewLogRusPretty ¶ added in v0.0.9
func NewLogRusPretty(c *Config, writer io.Writer) *LogRusPretty
NewLogRusPretty creates a new pretty formatter for logrus.
type OrderMode ¶ added in v0.0.9
type OrderMode int
OrderMode is the order mode used for logging.
type OrderModeString ¶ added in v0.0.6
type OrderModeString string
OrderModeString is the order mode used for logging.
const ( // OrderModeOff disables the order mode. OrderModeOff OrderModeString = "off" // OrderModeOn enables the order mode. OrderModeOn OrderModeString = "on" )
Order modes.
func (OrderModeString) Parse ¶ added in v0.0.9
func (m OrderModeString) Parse() OrderMode
Parse parses the order mode.
type Setup ¶ added in v0.0.9
type Setup struct { // TimeFormat is defining the time format used for printing timestamps. TimeFormat string // ColorMode is defining the color mode (default = ColorAuto). ColorMode ColorMode // OrderMode is defining the order mode. OrderMode OrderMode // Caller is defining whether the caller is reported. Caller bool // ErrorName is defining the name used for marking errors. ErrorName string // LevelNames is defining the names used for marking the different log // levels. LevelNames []string // LevelColors is defining the colors used for marking the different log // levels. LevelColors []string }
Setup is a data structure that contains all necessary setup information to format logs into a pretty format.
func (*Setup) FormatCaller ¶ added in v0.0.9
FormatCaller formats the caller.
func (*Setup) FormatErrFieldName ¶ added in v0.0.9
FormatErrFieldName formats the error field name.
func (*Setup) FormatErrFieldValue ¶ added in v0.0.9
FormatErrFieldValue formats the error field value.
func (*Setup) FormatFieldName ¶ added in v0.0.9
FormatFieldName formats the field name.
func (*Setup) FormatFieldValue ¶ added in v0.0.9
func (*Setup) FormatLevel ¶ added in v0.0.9
Format formats the log entry.
func (*Setup) FormatMessage ¶ added in v0.0.9
FormatMessage formats the message.
func (*Setup) FormatTimestamp ¶ added in v0.0.9
type ZeroLogPretty ¶ added in v0.0.9
type ZeroLogPretty struct { // Setup provides the setup for formatting logs. *Setup // ConsoleWriter is the console writer used for writing logs. zerolog.ConsoleWriter }
ZeroLogPretty formats logs into a pretty format.
func NewZeroLogPretty ¶ added in v0.0.9
func NewZeroLogPretty(c *Config, writer io.Writer) *ZeroLogPretty