Documentation
¶
Index ¶
- Variables
- func CtxPrefix(ctx context.Context) string
- func Debug(ctx context.Context, format string, v ...any)
- func Dump(ctx context.Context, level Level, data []byte)
- func Error(ctx context.Context, format string, v ...any)
- func Fatal(ctx context.Context, format string, v ...any)
- func Info(ctx context.Context, format string, v ...any)
- func NewContext(parent context.Context, dest *Logger) context.Context
- func Object(ctx context.Context, level Level, indent int, obj Marshaler) context.Context
- func Panic(ctx context.Context, v any)
- func Trace(ctx context.Context, format string, v ...any)
- func Warning(ctx context.Context, format string, v ...any)
- func WithPrefix(parent context.Context, prefix string) context.Context
- type Backend
- type FileLock
- type Level
- type Logger
- func (lgr *Logger) Attach(lvl Level, b Backend)
- func (lgr *Logger) Begin(prefix string) *Record
- func (lgr *Logger) Debug(prefix, format string, v ...any) *Logger
- func (lgr *Logger) Dump(prefix string, level Level, data []byte)
- func (lgr *Logger) Error(prefix, format string, v ...any) *Logger
- func (lgr *Logger) Fatal(prefix, format string, v ...any)
- func (lgr *Logger) Info(prefix, format string, v ...any) *Logger
- func (lgr *Logger) Object(prefix string, level Level, indent int, obj Marshaler) *Logger
- func (lgr *Logger) Trace(prefix, format string, v ...any) *Logger
- func (lgr *Logger) Warning(prefix, format string, v ...any) *Logger
- type Marshaler
- type Record
- func (rec *Record) Commit() *Logger
- func (rec *Record) Debug(format string, v ...any) *Record
- func (rec *Record) Dump(level Level, data []byte) *Record
- func (rec *Record) Error(format string, v ...any) *Record
- func (rec *Record) Fatal(format string, v ...any)
- func (rec *Record) Flush() *Record
- func (rec *Record) Info(format string, v ...any) *Record
- func (rec *Record) Object(level Level, indent int, obj Marshaler) *Record
- func (rec *Record) Rollback() *Logger
- func (rec *Record) Trace(format string, v ...any) *Record
- func (rec *Record) Warning(format string, v ...any) *Record
Constants ¶
This section is empty.
Variables ¶
var ( // StderrLogger writes logs to the stderr. StderrLogger = NewLogger(LevelAll, Stderr) // ConsoleLogger writes logs to the console. // Logs are colored, if console supports that. ConsoleLogger = NewLogger(LevelAll, Console) // FatalLogger writes LevelFatal logs to the // stderr and discards less important messages. FatalLogger = NewLogger(LevelFatal, Stderr) // DiscardLogger discards all logs written to. DiscardLogger = NewLogger(LevelNone, Discard) // DefaultLogger is the default logging destination. DefaultLogger = ConsoleLogger )
Standard loggers:
Functions ¶
func CtxPrefix ¶
CtxPrefix returns a log prefix associated with the context.Context. If no Logger is available, an empty string ("") will be returned.
Note, context.Context parameter may be safely passed as nil.
func Debug ¶
Debug writes a Debug-level message to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Dump ¶
Dump writes the hex dump to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Error ¶
Error writes a Error-level message to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Fatal ¶
Fatal writes a Fatal-level message to the Logger associated with the Context.
It calls os.Exit(1) and never returns.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Info ¶
Info writes a Info-level message to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func NewContext ¶
NewContext returns new context.Context with the associated Logger.
func Object ¶
Object writes any object that implements Marshaler interface to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Panic ¶
Panic writes panic message to log, including the call stack, and terminates the program
func Trace ¶
Trace writes a Trace-level message to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func Warning ¶
Warning writes a Warning-level message to the Logger associated with the Context.
If Logger is not available, DefaultLogger will be used. The context.Context parameter may be safely passed as nil.
func WithPrefix ¶
WithPrefix returns a new context.Context with the associated prefix.
Types ¶
type Backend ¶
type Backend interface {
// Send writes some lines to the destination, represented
// by the Backend.
//
// Backend may assume that levels and lines slices have
// the same length and 1:1 correspondence each to other.
//
// Logically, these lines comprise a single log record.
// If multiple goroutines write to log simultaneously,
// this is Backend's responsibility to write them to the
// destination in the sequential order and to avoid mixing
// of unrelated records between each other.
//
// If Backend writes log to file and implements log rotation,
// it should avoid rotation in the middle of some record.
Send(levels []Level, lines [][]byte)
}
Backend represents a logging destination, which may be console, disk file, ...
var ( // Console writes output to console. Console Backend = &backendConsole{} // Console writes output to stderr. Stderr Backend = &backendStderr{} // Discard silently discards any output. Discard Backend = &backendDiscard{} )
Standard backends:
func NewFileBackend ¶
NewFileBackend returns a Backend that writes log to file. It also supports log file rotation.
When file size exceeds maxsize, the content of the log file file gzip-ed and copied into backup. Already existent backups renamed in circular order, according to the following scheme:
file.log -> file.log.0.gz -> file.log.1.gz -> ... -> file.log.N.gz
The backups parameter defines maximum number of backup files (so N is backups-1).
If the previous run of the program has left more backup files, that current configuration allows, the excessive files will be deleted during rotation.
The file Backend doesn't break multi-line logical record between two log files, so the actual maximum file size may exceed the configured threshold.
Setting maxsize to 0 disables rotation and setting backups to 0 disables creation of the backup files.
Note, file Backend ignores any I/O errors when writing to log files, as it has no method to report them.
type FileLock ¶
type FileLock struct {
// contains filtered or unexported fields
}
FileLock represents an acquired file lock.
func FileLockEx ¶
FileLockEx acquires an exclusive file lock on file, pointed by os.File.
If lock already held by another process, this function waits until lock is released.
func (*FileLock) Close ¶
Close releases the lock, previously taken by FileLockEx.
type Level ¶
type Level int
Level enumerates possible log levels
const ( LevelTrace Level = iota // Protocol trace LevelDebug // Debug messages LevelInfo // Informational messages LevelWarning // Warning messages LevelError // Error messages LevelFatal // Fatal errors // These constants are useful for filtering LevelAll = LevelTrace // Allow all levels LevelNone = LevelFatal + 1 // Allow no levels )
Log levels:
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the logging destination. It can be connected to console, to the disk file etc...
func CtxLogger ¶
CtxLogger returns a Logger associated with the context.Context. If no Logger is available, FatalLogger will be returned.
Note, context.Context parameter may be safely passed as nil.
func (*Logger) Attach ¶
Attach adds an additional Backend to send logs to.
If this backend already attached to this logger, it only updates the log level.
func (*Logger) Begin ¶
Begin initiates creation of a new multi-line log Record.
Records are always written atomically. Records written from the concurrently running goroutines are never intermixed at output. During log rotation, Records are not split between different log files.
func (*Logger) Fatal ¶
Fatal writes a Fatal-level message to the Logger.
It calls os.Exit(1) and never returns.
type Marshaler ¶
type Marshaler interface {
// LogMarshal returns a string representation of the
// object, for logging. The returned string may be
// multi-line, delimited with the '\n' characters.
MarshalLog() []byte
}
Marshaler is the interface implemented by objects that can be written to the log, using Object, Logger.Object and Record.Object functions.
type Record ¶
type Record struct {
// contains filtered or unexported fields
}
Record allows to build a multi-line log message, which will be written atomically, and will not be intermixed with logging activity from other goroutines and/or split between different files during log rotation.
func (*Record) Fatal ¶
Fatal writes a Fatal-level message to the Record.
It calls os.Exit(1) and never returns.