log

package
v0.0.0-...-277146c Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2026 License: BSD-2-Clause Imports: 15 Imported by: 0

README

DNS-SD service discovery for printers and scanners

Logging facilities

import "github.com/OpenPrinting/go-mfp/log"

This package provides logging facilities.

Documentation

Index

Constants

This section is empty.

Variables

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

func CtxPrefix(ctx context.Context) string

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

func Debug(ctx context.Context, format string, v ...any)

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

func Dump(ctx context.Context, level Level, data []byte)

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

func Error(ctx context.Context, format string, v ...any)

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

func Fatal(ctx context.Context, format string, v ...any)

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

func Info(ctx context.Context, format string, v ...any)

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

func NewContext(parent context.Context, dest *Logger) context.Context

NewContext returns new context.Context with the associated Logger.

func Object

func Object(ctx context.Context, level Level, indent int,
	obj Marshaler) context.Context

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

func Panic(ctx context.Context, v any)

Panic writes panic message to log, including the call stack, and terminates the program

func Trace

func Trace(ctx context.Context, format string, v ...any)

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

func Warning(ctx context.Context, format string, v ...any)

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

func WithPrefix(parent context.Context, prefix string) context.Context

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

func NewFileBackend(path string, maxsize, backups int) Backend

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

func FileLockEx(file *os.File) (*FileLock, error)

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

func (fl *FileLock) Close() error

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

func CtxLogger(ctx context.Context) *Logger

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 NewLogger

func NewLogger(lvl Level, b Backend) *Logger

NewLogger returns a new logger, attached to the specified backend

func (*Logger) Attach

func (lgr *Logger) Attach(lvl Level, b Backend)

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

func (lgr *Logger) Begin(prefix string) *Record

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) Debug

func (lgr *Logger) Debug(prefix, format string, v ...any) *Logger

Debug writes a Debug-level message to the Logger.

func (*Logger) Dump

func (lgr *Logger) Dump(prefix string, level Level, data []byte)

Dump writes the hex dump to the Logger.

func (*Logger) Error

func (lgr *Logger) Error(prefix, format string, v ...any) *Logger

Error writes a Error-level message to the Logger.

func (*Logger) Fatal

func (lgr *Logger) Fatal(prefix, format string, v ...any)

Fatal writes a Fatal-level message to the Logger.

It calls os.Exit(1) and never returns.

func (*Logger) Info

func (lgr *Logger) Info(prefix, format string, v ...any) *Logger

Info writes a Info-level message to the Logger.

func (*Logger) Object

func (lgr *Logger) Object(prefix string, level Level, indent int, obj Marshaler) *Logger

Object writes any object that implements Marshaler interface to the Logger.

func (*Logger) Trace

func (lgr *Logger) Trace(prefix, format string, v ...any) *Logger

Trace writes a Trace-level message to the Logger.

func (*Logger) Warning

func (lgr *Logger) Warning(prefix, format string, v ...any) *Logger

Warning writes a Warning-level message to the Logger.

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 Begin

func Begin(ctx context.Context) *Record

Begin initiates creation of a new multi-line log Record.

See Logger.Begin for details.

func (*Record) Commit

func (rec *Record) Commit() *Logger

Commit writes Record to the parent Logger.

func (*Record) Debug

func (rec *Record) Debug(format string, v ...any) *Record

Debug writes a Debug-level message to the Record.

func (*Record) Dump

func (rec *Record) Dump(level Level, data []byte) *Record

Dump writes the hex dump to the Record.

func (*Record) Error

func (rec *Record) Error(format string, v ...any) *Record

Error writes a Error-level message to the Record.

func (*Record) Fatal

func (rec *Record) Fatal(format string, v ...any)

Fatal writes a Fatal-level message to the Record.

It calls os.Exit(1) and never returns.

func (*Record) Flush

func (rec *Record) Flush() *Record

Flush writes Record to the parent Logger and resets its buffers.

func (*Record) Info

func (rec *Record) Info(format string, v ...any) *Record

Info writes a Info-level message to the Record.

func (*Record) Object

func (rec *Record) Object(level Level, indent int, obj Marshaler) *Record

Object writes any object that implements Marshaler interface to the Record.

func (*Record) Rollback

func (rec *Record) Rollback() *Logger

Rollback drops the Record.

func (*Record) Trace

func (rec *Record) Trace(format string, v ...any) *Record

Trace writes a Trace-level message to the Record.

func (*Record) Warning

func (rec *Record) Warning(format string, v ...any) *Record

Warning writes a Warning-level message to the Record.

Jump to

Keyboard shortcuts

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