log

package
v0.0.14 Latest Latest
Warning

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

Go to latest
Published: Feb 24, 2025 License: MIT Imports: 15 Imported by: 0

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

View Source
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.

View Source
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.

View Source
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

View Source
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

func IsTerminal(writer io.Writer) bool

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

func (b *Buffer) Bytes() ([]byte, error)

Bytes returns current bytes of the buffer with the current error.

func (*Buffer) String added in v0.0.9

func (b *Buffer) String() string

String returns current string of the buffer with the current error.

func (*Buffer) WriteByte added in v0.0.9

func (b *Buffer) WriteByte(byt byte) *Buffer

WriteByte writes the given byte to the buffer.

func (*Buffer) WriteCaller added in v0.0.9

func (b *Buffer) WriteCaller(caller *runtime.Frame) *Buffer

WriteCaller writes the caller information to the buffer.

func (*Buffer) WriteColored added in v0.0.9

func (b *Buffer) WriteColored(color, str string) *Buffer

WriteColored writes the given text with the given color to the buffer.

func (*Buffer) WriteData added in v0.0.9

func (b *Buffer) WriteData(key string, value any) *Buffer

WriteData writes the data to the buffer.

func (*Buffer) WriteField added in v0.0.9

func (b *Buffer) WriteField(level Level, key string) *Buffer

WriteField writes the given key with the given color to the buffer.

func (*Buffer) WriteLevel added in v0.0.9

func (b *Buffer) WriteLevel(level Level) *Buffer

WriteLevel writes the given log level to the buffer.

func (*Buffer) WriteString added in v0.0.9

func (b *Buffer) WriteString(str string) *Buffer

WriteString writes the given string to the buffer.

func (*Buffer) WriteValue added in v0.0.9

func (b *Buffer) WriteValue(value any) *Buffer

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.

func (ColorMode) CheckFlag added in v0.0.9

func (m ColorMode) CheckFlag(flag ColorMode) bool

CheckFlag checks if the given color mode flag is set.

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

func (c *Config) ParseZeroLevel() zerolog.Level

ParseZeroLevel parses the log level string and returns the corresponding zerolog level.

func (*Config) Setup

func (c *Config) Setup(writer io.Writer) *Setup

Setup creates a new pretty formatter config.

func (*Config) SetupRus added in v0.0.6

func (c *Config) SetupRus(writer io.Writer, logger *logrus.Logger) *logrus.Logger

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

func (c *Config) SetupZero(writer io.Writer) *Config

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

func (c *Config) ZeroLogger() zerolog.Logger

ZeroLogger returns the zerolog logger.

type Formatter

type Formatter string

Formatter is the formatter used for logging.

const (
	// Pretty is the pretty formatter.
	FormatterPretty Formatter = "pretty"
	// Text is the text formatter.
	FormatterText Formatter = "text"
	// JSON is the JSON formatter.
	FormatterJSON Formatter = "json"
)

Formatters.

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

func ParseLevel(level string) Level

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.

func (*LogRusPretty) Format added in v0.0.9

func (p *LogRusPretty) Format(entry *logrus.Entry) ([]byte, error)

Format formats the log entry to a pretty format.

type OrderMode added in v0.0.9

type OrderMode int

OrderMode is the order mode used for logging.

const (
	// OrderDefault is the default order mode.
	OrderDefault = OrderOn
	// OrderUnset is the unset order mode.
	OrderUnset OrderMode = 0
	// OrderOff disables the order mode.
	OrderOff OrderMode = 1
	// OrderOn enables the order mode.
	OrderOn OrderMode = 2
)

Order modes.

func (OrderMode) CheckFlag added in v0.0.9

func (m OrderMode) CheckFlag(flag OrderMode) bool

CheckFlag checks if the given order mode flag is set.

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

func (s *Setup) FormatCaller(i any) string

FormatCaller formats the caller.

func (*Setup) FormatErrFieldName added in v0.0.9

func (s *Setup) FormatErrFieldName(i any) string

FormatErrFieldName formats the error field name.

func (*Setup) FormatErrFieldValue added in v0.0.9

func (*Setup) FormatErrFieldValue(i any) string

FormatErrFieldValue formats the error field value.

func (*Setup) FormatFieldName added in v0.0.9

func (s *Setup) FormatFieldName(i any) string

FormatFieldName formats the field name.

func (*Setup) FormatFieldValue added in v0.0.9

func (*Setup) FormatFieldValue(i any) string

func (*Setup) FormatLevel added in v0.0.9

func (s *Setup) FormatLevel(i any) string

Format formats the log entry.

func (*Setup) FormatMessage added in v0.0.9

func (*Setup) FormatMessage(i any) string

FormatMessage formats the message.

func (*Setup) FormatTimestamp added in v0.0.9

func (s *Setup) FormatTimestamp(i any) string

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

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳