log

package module
v1.0.16 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2025 License: Apache-2.0 Imports: 9 Imported by: 2

Documentation

Index

Constants

View Source
const (
	DefaultTimestampFormat = "2006-01-02 15:04:05"

	FieldKeyMsg         = "msg"
	FieldKeyLevel       = "level"
	FieldKeyTime        = "time"
	FieldKeyLoggerError = "logger_error"
	FieldKeyEntity      = "entity"
	FieldKeyAction      = "action"
	FieldKeyMethod      = "method"
	FieldKeySubject     = "subject"
	FieldKeyData        = "data"
	FieldKeyDuration    = "duration"
	FieldKeyTraceID     = "trace_id"
)
View Source
const (
	EntityActionIncomeRequest       = ">>"
	EntityActionOutcomeRequest      = "<<"
	EntityActionIncomeResponse      = ">"
	EntityActionOutcomeResponse     = "<"
	EntityActionTransitRequest      = ">>>"
	EntityActionTransitResponse     = "<--"
	EntityActionTransitNotification = "<<<"
)

Variables

View Source
var ContextModes = enums.New[ContextMode](
	map[ContextMode]string{
		ContextModeNone:        "",
		ContextModeTransparent: "transparent",
		ContextModeOpaque:      "opaque",
	},
)
View Source
var (
	ErrRequiredFieldExporter = errors.New("exporter is required")
)
View Source
var ErrorKey = "error"
View Source
var Levels = enums.New[Level](
	map[Level]string{
		PanicLevel: "panic",
		FatalLevel: "fatal",
		ErrorLevel: "error",
		WarnLevel:  "warn",
		InfoLevel:  "info",
		DebugLevel: "debug",
		TraceLevel: "trace",
	},
)
View Source
var Resolve = func(ctx context.Context) Logger {
	log := GetLogger(ctx, nil)
	if log == nil {
		panic(fmt.Errorf("logger not found in context: %v", ctx))
	}

	return log
}

Resolve returns logger from context

Functions

func NewContext added in v1.0.15

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext returns new context with logger

Types

type Builder added in v1.0.14

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

func NewBuilder added in v1.0.14

func NewBuilder() *Builder

func (*Builder) Build added in v1.0.14

func (that *Builder) Build() (*Log, error)

func (*Builder) WithExporter added in v1.0.14

func (that *Builder) WithExporter(exporter Exporter) *Builder

func (*Builder) WithHook added in v1.0.14

func (that *Builder) WithHook(hook Hook) *Builder

func (*Builder) WithHookForLevel added in v1.0.15

func (that *Builder) WithHookForLevel(hook Hook, level Level) *Builder

func (*Builder) WithHookForLevels added in v1.0.14

func (that *Builder) WithHookForLevels(hook Hook, levels []Level) *Builder

func (*Builder) WithLevel added in v1.0.14

func (that *Builder) WithLevel(level Level) *Builder

type Constructor added in v1.0.15

type Constructor func(exporter Exporter, level Level) (Logger, error)
var DefaultConstructor Constructor = func(exporter Exporter, level Level) (Logger, error) {
	return NewBuilder().
		WithExporter(exporter).
		WithLevel(level).
		Build()
}

type ContextMode added in v1.0.15

type ContextMode int
const (
	ContextModeNone ContextMode = iota
	ContextModeTransparent
	ContextModeOpaque
)

func (ContextMode) String added in v1.0.15

func (that ContextMode) String() string

type Entry

type Entry struct {
	Logger  *Log
	Data    Fields
	Time    time.Time
	Level   Level
	Message string
	Buffer  *bytes.Buffer
	LogErr  string
}

func NewEntry

func NewEntry(logger *Log) *Entry

func (*Entry) Debug

func (that *Entry) Debug(ctx context.Context, args ...interface{})

func (*Entry) Debugf

func (that *Entry) Debugf(ctx context.Context, format string, args ...interface{})

func (*Entry) Error

func (that *Entry) Error(ctx context.Context, args ...interface{})

func (*Entry) Errorf

func (that *Entry) Errorf(ctx context.Context, format string, args ...interface{})

func (*Entry) Fatal

func (that *Entry) Fatal(ctx context.Context, args ...interface{})

func (*Entry) Fatalf

func (that *Entry) Fatalf(ctx context.Context, format string, args ...interface{})

func (*Entry) Info

func (that *Entry) Info(ctx context.Context, args ...interface{})

func (*Entry) Infof

func (that *Entry) Infof(ctx context.Context, format string, args ...interface{})

func (*Entry) Log

func (that *Entry) Log(ctx context.Context, level Level, args ...interface{})

func (*Entry) Logf

func (that *Entry) Logf(ctx context.Context, level Level, format string, args ...interface{})

func (*Entry) Panic

func (that *Entry) Panic(ctx context.Context, args ...interface{})

func (*Entry) Panicf

func (that *Entry) Panicf(ctx context.Context, format string, args ...interface{})

func (*Entry) Trace

func (that *Entry) Trace(ctx context.Context, args ...interface{})

func (*Entry) Tracef

func (that *Entry) Tracef(ctx context.Context, format string, args ...interface{})

func (*Entry) Warning

func (that *Entry) Warning(ctx context.Context, args ...interface{})

func (*Entry) Warningf

func (that *Entry) Warningf(ctx context.Context, format string, args ...interface{})

func (*Entry) WithError

func (that *Entry) WithError(err error) LoggerEntry

func (*Entry) WithField

func (that *Entry) WithField(key string, value interface{}) LoggerEntry

func (*Entry) WithFields

func (that *Entry) WithFields(fields Fields) LoggerEntry

func (*Entry) WithTime

func (that *Entry) WithTime(t time.Time) *Entry

type Exporter added in v1.0.5

type Exporter interface {
	Export(ctx context.Context, entry *Entry)
}

type Factory added in v1.0.15

type Factory struct {
	sync.Mutex
	// contains filtered or unexported fields
}

func NewFactory added in v1.0.15

func NewFactory(exporter Exporter, constructor Constructor) *Factory

func (*Factory) NewLogger added in v1.0.15

func (that *Factory) NewLogger(level Level) (Logger, error)

type FieldKey added in v1.0.9

type FieldKey string

type FieldMap

type FieldMap map[FieldKey]string

func (FieldMap) DecodePrefixFieldClashes added in v1.0.1

func (that FieldMap) DecodePrefixFieldClashes(data Fields)

func (FieldMap) EncodePrefixFieldClashes added in v1.0.1

func (that FieldMap) EncodePrefixFieldClashes(data Fields)

func (FieldMap) Resolve

func (that FieldMap) Resolve(key FieldKey) string

type Fields

type Fields map[string]interface{}

func (Fields) Clone

func (that Fields) Clone() Fields

func (Fields) Expand

func (that Fields) Expand() Fields

func (Fields) Fetch

func (that Fields) Fetch(key string) interface{}

type Formatter

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

type Hook

type Hook interface {
	Fire(ctx context.Context, entry *Entry) error
}

type HookFunc added in v1.0.14

type HookFunc func(ctx context.Context, entry *Entry) error

func (HookFunc) Fire added in v1.0.14

func (fn HookFunc) Fire(ctx context.Context, entry *Entry) error

type Hooks

type Hooks struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func NewHooks

func NewHooks() *Hooks

func (*Hooks) Add

func (that *Hooks) Add(levels []Level, hook Hook)

func (*Hooks) Fire

func (that *Hooks) Fire(ctx context.Context, level Level, entry *Entry) error

type Level

type Level uint8
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
)

func (Level) String

func (that Level) String() string

type Log

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

func NewDummyLogger

func NewDummyLogger() *Log

func (*Log) AddHook

func (that *Log) AddHook(levels []Level, hook Hook)

func (*Log) Debug

func (that *Log) Debug(ctx context.Context, args ...interface{})

func (*Log) DebugFn

func (that *Log) DebugFn(ctx context.Context, fn LogFunction)

func (*Log) Debugf

func (that *Log) Debugf(ctx context.Context, format string, args ...interface{})

func (*Log) Error

func (that *Log) Error(ctx context.Context, args ...interface{})

func (*Log) ErrorFn

func (that *Log) ErrorFn(ctx context.Context, fn LogFunction)

func (*Log) Errorf

func (that *Log) Errorf(ctx context.Context, format string, args ...interface{})

func (*Log) Fatal

func (that *Log) Fatal(ctx context.Context, args ...interface{})

func (*Log) FatalFn

func (that *Log) FatalFn(ctx context.Context, fn LogFunction)

func (*Log) Fatalf

func (that *Log) Fatalf(ctx context.Context, format string, args ...interface{})

func (*Log) FreeBuffer

func (that *Log) FreeBuffer(buffer *bytes.Buffer)

func (*Log) GetBuffer

func (that *Log) GetBuffer() *bytes.Buffer

func (*Log) Info

func (that *Log) Info(ctx context.Context, args ...interface{})

func (*Log) InfoFn

func (that *Log) InfoFn(ctx context.Context, fn LogFunction)

func (*Log) Infof

func (that *Log) Infof(ctx context.Context, format string, args ...interface{})

func (*Log) IsLevelEnabled

func (that *Log) IsLevelEnabled(level Level) bool

func (*Log) Log

func (that *Log) Log(ctx context.Context, level Level, args ...interface{})

func (*Log) LogFn

func (that *Log) LogFn(ctx context.Context, level Level, fn LogFunction)

func (*Log) Logf

func (that *Log) Logf(ctx context.Context, level Level, format string, args ...interface{})

func (*Log) Panic

func (that *Log) Panic(ctx context.Context, args ...interface{})

func (*Log) PanicFn

func (that *Log) PanicFn(ctx context.Context, fn LogFunction)

func (*Log) Panicf

func (that *Log) Panicf(ctx context.Context, format string, args ...interface{})

func (*Log) Trace

func (that *Log) Trace(ctx context.Context, args ...interface{})

func (*Log) TraceFn

func (that *Log) TraceFn(ctx context.Context, fn LogFunction)

func (*Log) Tracef

func (that *Log) Tracef(ctx context.Context, format string, args ...interface{})

func (*Log) Warning

func (that *Log) Warning(ctx context.Context, args ...interface{})

func (*Log) WarningFn

func (that *Log) WarningFn(ctx context.Context, fn LogFunction)

func (*Log) Warningf

func (that *Log) Warningf(ctx context.Context, format string, args ...interface{})

func (*Log) WithError

func (that *Log) WithError(err error) LoggerEntry

func (*Log) WithField

func (that *Log) WithField(key string, value interface{}) LoggerEntry

func (*Log) WithFields

func (that *Log) WithFields(fields Fields) LoggerEntry

func (*Log) WithTime

func (that *Log) WithTime(t time.Time) *Entry

type LogFunction

type LogFunction func(ctx context.Context, logger LoggerEntry)

type Logger

type Logger interface {
	LoggerEntry

	PanicFn(ctx context.Context, fn LogFunction)
	FatalFn(ctx context.Context, fn LogFunction)
	ErrorFn(ctx context.Context, fn LogFunction)
	InfoFn(ctx context.Context, fn LogFunction)
	DebugFn(ctx context.Context, fn LogFunction)
	TraceFn(ctx context.Context, fn LogFunction)
}

func GetLogger added in v1.0.15

func GetLogger(ctx context.Context, defVal Logger) Logger

type LoggerEntry

type LoggerEntry interface {
	WithField(key string, value interface{}) LoggerEntry
	WithFields(fields Fields) LoggerEntry
	WithError(err error) LoggerEntry

	Panicf(ctx context.Context, format string, args ...interface{})
	Fatalf(ctx context.Context, format string, args ...interface{})
	Errorf(ctx context.Context, format string, args ...interface{})
	Warningf(ctx context.Context, format string, args ...interface{})
	Infof(ctx context.Context, format string, args ...interface{})
	Debugf(ctx context.Context, format string, args ...interface{})
	Tracef(ctx context.Context, format string, args ...interface{})

	Panic(ctx context.Context, args ...interface{})
	Fatal(ctx context.Context, args ...interface{})
	Error(ctx context.Context, args ...interface{})
	Warning(ctx context.Context, args ...interface{})
	Info(ctx context.Context, args ...interface{})
	Debug(ctx context.Context, args ...interface{})
	Trace(ctx context.Context, args ...interface{})
}

type LoggerPiece added in v1.0.15

type LoggerPiece interface {
	WithField(key string, value interface{}) LoggerPiece
	WithFields(fields Fields) LoggerPiece
	WithError(err error) LoggerPiece

	Message(ctx context.Context, msg string)
	Messagef(ctx context.Context, format string, args ...interface{})
}

type Piece added in v1.0.15

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

func (*Piece) Message added in v1.0.15

func (that *Piece) Message(ctx context.Context, msg string)

func (*Piece) Messagef added in v1.0.15

func (that *Piece) Messagef(ctx context.Context, format string, args ...interface{})

func (*Piece) WithError added in v1.0.15

func (that *Piece) WithError(err error) LoggerPiece

func (*Piece) WithField added in v1.0.15

func (that *Piece) WithField(key string, value interface{}) LoggerPiece

func (*Piece) WithFields added in v1.0.15

func (that *Piece) WithFields(fields Fields) LoggerPiece

type Resolver added in v1.0.15

type Resolver interface {
	Resolve(ctx context.Context) Logger
	NewContext(ctx context.Context) context.Context
}

func NewResolver added in v1.0.15

func NewResolver(logger Logger, mode ContextMode) Resolver

Directories

Path Synopsis
exporters
formatters
importers
rex

Jump to

Keyboard shortcuts

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