log

package
v0.0.0-...-3a7b102 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2024 License: MIT Imports: 14 Imported by: 7

Documentation

Index

Constants

View Source
const (
	FieldKeyMsg      = "msg"
	FieldKeyLevel    = "level"
	FieldKeyTime     = "time"
	FieldKeyLogError = "log_error"
	FieldKeyFunc     = "func"
	FieldKeyFile     = "file"
)

Default key names for the default fields

View Source
const FormatterTypeJson = "json"

Variables

This section is empty.

Functions

func LogLevelFuncForError

func LogLevelFuncForError(err error, logger Logger) func(ctx context.Context, message any, fields ...Field)

func StdLoggerWithLevel

func StdLoggerWithLevel(adapter Logger, level Level, withFields ...Field) *stdlog.Logger

func ToContext

func ToContext(ctx context.Context, fields ...Field) context.Context

Types

type Adapter

type Adapter struct {
	Out          io.Writer
	Formatter    Formatter
	ReportCaller bool

	ExitFunc exitFunc
	// contains filtered or unexported fields
}

func DefaultLoggerWithLevel

func DefaultLoggerWithLevel(lvl Level) *Adapter

func NewFromConfig

func NewFromConfig(cfg Config) (*Adapter, error)

func (*Adapter) Debug

func (l *Adapter) Debug(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Error

func (l *Adapter) Error(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Fatal

func (l *Adapter) Fatal(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Info

func (l *Adapter) Info(ctx context.Context, msg any, fields ...Field)

func (*Adapter) IsLevelEnabled

func (l *Adapter) IsLevelEnabled(level Level) bool

IsLevelEnabled checks if the log level of the logger is greater than the level param

func (*Adapter) Level

func (l *Adapter) Level() Level

func (*Adapter) Log

func (l *Adapter) Log(ctx context.Context, level Level, msg any, fields ...Field)

Log will log a message at the level given as parameter. Warning: using Log at Panic or Fatal level will not respectively Panic nor Exit. For this behaviour logger.Panic or logger.Fatal should be used instead.

func (*Adapter) Panic

func (l *Adapter) Panic(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Print

func (l *Adapter) Print(ctx context.Context, msg any, fields ...Field)

func (*Adapter) SetFormatter

func (logger *Adapter) SetFormatter(formatter Formatter)

SetFormatter sets the logger formatter.

func (*Adapter) SetOutput

func (logger *Adapter) SetOutput(output io.Writer)

SetOutput sets the logger output.

func (*Adapter) SetReportCaller

func (logger *Adapter) SetReportCaller(reportCaller bool)

func (*Adapter) Trace

func (l *Adapter) Trace(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Warn

func (l *Adapter) Warn(ctx context.Context, msg any, fields ...Field)

func (*Adapter) Warning

func (l *Adapter) Warning(ctx context.Context, msg any, fields ...Field)

type Config

type Config struct {
	Loglevel  Level
	Formatter FormatterConfig
	Output    OutputConfig
}

type Entry

type Entry struct {
	ReportCaller bool
	Data         Fields
	Time         time.Time
	Level        Level
	Caller       *runtime.Frame
	Message      string
}

func (Entry) HasCaller

func (entry Entry) HasCaller() (has bool)

type Field

type Field struct {
	Name  string
	Value any
}

func Any

func Any(name string, value any) Field

func ByteString

func ByteString(name string, value []byte) Field

for UTF-8 encoded string

type FieldMap

type FieldMap map[fieldKey]string

FieldMap allows customization of the key names for default fields.

type Fields

type Fields map[string]any

func ContextLogValues

func ContextLogValues(ctx context.Context) Fields

type Formatter

type Formatter interface {
	Format(*Entry) ([]byte, error)
}

type FormatterConfig

type FormatterConfig struct {
	Type string
	Json *JSONFormatterConfig
}

type JSONFormatter

type JSONFormatter struct {
	TimestampFormat   string
	DisableTimestamp  bool
	DisableHTMLEscape bool
	DataKey           string
	FieldMap          FieldMap
	CallerPrettifier  func(*runtime.Frame) (function string, file string)
	PrettyPrint       bool
}

JSONFormatter formats logs into parsable json

func (*JSONFormatter) Format

func (f *JSONFormatter) Format(entry *Entry) ([]byte, error)

Format renders a single log entry

type JSONFormatterConfig

type JSONFormatterConfig struct {
	TimestampFormat   string
	DisableTimestamp  bool
	DisableHTMLEscape bool
	PrettyPrint       bool
	DataKey           string
}

type Level

type Level uint32
const (
	// PanicLevel level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	PanicLevel Level = iota
	// FatalLevel level. Logs and then calls `logger.Exit(1)`. It will exit even if the
	// logging level is set to Panic.
	FatalLevel
	// ErrorLevel level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	ErrorLevel
	// WarnLevel level. Non-critical entries that deserve eyes.
	WarnLevel
	// InfoLevel level. General operational entries about what's going on inside the
	// application.
	InfoLevel
	// DebugLevel level. Usually only enabled when debugging. Very verbose logging.
	DebugLevel
	// TraceLevel level. Designates finer-grained informational events than the Debug.
	TraceLevel
)

These are the different logging levels. You can set the logging level to log on your instance of logger

func LogLevelForError

func LogLevelForError(err error) Level

func ParseLogLevel

func ParseLogLevel(lvl string) (Level, error)

func (Level) String

func (l Level) String() string

type LogLevelSpecifier

type LogLevelSpecifier interface {
	LogLevel() Level
}

type Logger

type Logger interface {
	Log(ctx context.Context, level Level, msg any, fields ...Field)
	Trace(ctx context.Context, msg any, fields ...Field)
	Debug(ctx context.Context, msg any, fields ...Field)
	Info(ctx context.Context, msg any, fields ...Field)
	Warn(ctx context.Context, msg any, fields ...Field)
	Warning(ctx context.Context, msg any, fields ...Field)
	Error(ctx context.Context, msg any, fields ...Field)
	Fatal(ctx context.Context, msg any, fields ...Field)
	Panic(ctx context.Context, msg any, fields ...Field)
}

type LoggerWriter

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

func (*LoggerWriter) Write

func (l *LoggerWriter) Write(p []byte) (int, error)

type MutexWrap

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

func (*MutexWrap) Disable

func (mw *MutexWrap) Disable()

func (*MutexWrap) Lock

func (mw *MutexWrap) Lock()

func (*MutexWrap) Unlock

func (mw *MutexWrap) Unlock()

type OutputConfig

type OutputConfig struct {
	Filepath string
	Console  bool
}

Jump to

Keyboard shortcuts

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