logging

package module
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2022 License: MIT Imports: 10 Imported by: 5

Documentation

Overview

Package logging provides a multi-backend leveled logging facility.

Backends

package logging defines the following builtin backends:

  • NoopBackend, which discards all messages
  • FileBackend, which redirects logs to an io.Writer object. It has constructors for files, stdout and stderr
  • SyslogBackend, only available in unix-like systems, writes log entryies to a syslog daemon.

A backend can safely be used by multiple loggers.

It is the caller's responsibility to call Close on backends when they are not used anymore to free their resources.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Backend

type Backend interface {
	Write(*Record) error
	SetFormatter(Formatter)
	SetLevel(Level)
	Level() Level
	Reopen() error
	Close() error
}

Backend is the interface that specifies the methods that a backend must implement.

type FileBackend

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

FileBackend is a backend that writes to a file.

func NewFileBackend

func NewFileBackend(filename string) (*FileBackend, error)

NewFileBackend creates a new backend to write the logs in a given file.

func NewIoBackend

func NewIoBackend(buf io.Writer) *FileBackend

NewIoBackend creates a new backend to write the logs in a given io.Writer.

func NewStderrBackend

func NewStderrBackend() *FileBackend

NewStderrBackend creates a new backend to write the logs on the error output.

func NewStdoutBackend

func NewStdoutBackend() *FileBackend

NewStdoutBackend creates a new backend to write the logs on the standard output.

func (*FileBackend) Close added in v0.3.0

func (b *FileBackend) Close() error

Close closes the underlying file used by the backend.

func (*FileBackend) Level

func (b *FileBackend) Level() Level

Level returns the log level set for this backend.

func (*FileBackend) Reopen

func (b *FileBackend) Reopen() error

Reopen closes and reopens the file it writes to. It should be used after log rotation.

func (*FileBackend) SetFormatter

func (b *FileBackend) SetFormatter(f Formatter)

SetFormatter defines the formatter for this backend.

func (*FileBackend) SetLevel

func (b *FileBackend) SetLevel(l Level)

SetLevel changes the log level of the backend.

func (FileBackend) Write

func (b FileBackend) Write(r *Record) error

type Formatter

type Formatter func(*Record) string

Formatter is the types of the functions that can be used to format a log entry. They take a pointer to a record and return a formatted string.

type Level

type Level byte

Level is the type of log levels.

const (
	Trace Level = iota
	Debug
	Info
	Notice
	Warning
	Error
	Critical
	Alert
	Fatal
	DefaultLevel = Info
)

func LevelByName

func LevelByName(l string) (Level, error)

LevelByName creates a log level given its name. It returns an error if the name does not match any level.

func (Level) Name

func (l Level) Name() string

Name returns the name of the log level.

type Logger

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

Logger is a facility that writes logs to one or more backands (files, stdout/stderr, syslog, etc.) which can be configured independently

Loggers are concurrent-safe.

func GetLogger

func GetLogger(name string) *Logger

GetLogger returns a logger given its name. if the logger does not exist, it initializes one with the defaults (it logs to stdout with level INFO).

func NewLogger

func NewLogger(name string) *Logger

NewLogger initializes a new Logger with no backend and with the default log level.

func (*Logger) AddBackend

func (l *Logger) AddBackend(b Backend)

AddBackend add a new Backend to the logger. All set backends are kept.

func (*Logger) Alert added in v0.4.0

func (l *Logger) Alert(text string)

Alert logs a message with the Alert level.

func (*Logger) Alertf added in v0.4.0

func (l *Logger) Alertf(text string, args ...interface{})

Alertf formats the message with given args and logs the result with the. Alert level.

func (*Logger) AsStdLog

func (l *Logger) AsStdLog(level Level) *log.Logger

AsStdLog encapsulate the logger in an instance of lof.Logger from the standard library and returns it.

It is there for interoperability reasons.

func (*Logger) Critical

func (l *Logger) Critical(text string)

Critical logs a message with the Critical level.

func (*Logger) Criticalf

func (l *Logger) Criticalf(text string, args ...interface{})

Criticalf formats the message with given args and logs the result with the. Critical level.

func (*Logger) Debug

func (l *Logger) Debug(text string)

Debug logs a message with the Debug level.

func (*Logger) Debugf

func (l *Logger) Debugf(text string, args ...interface{})

Debugf formats the message with given args and logs the result with the Debug level.

func (*Logger) Error

func (l *Logger) Error(text string)

Error logs a message with the Error level.

func (*Logger) Errorf

func (l *Logger) Errorf(text string, args ...interface{})

Errorf formats the message with given args and logs the result with the Error level.

func (*Logger) Fatal

func (l *Logger) Fatal(text string)

Fatal logs a message with the Fatal level.

func (*Logger) Fatalf

func (l *Logger) Fatalf(text string, args ...interface{})

Fatalf formats the message with given args and logs the result with the Fatal level.

func (*Logger) Info

func (l *Logger) Info(text string)

Info logs a message with the Info level.

func (*Logger) Infof

func (l *Logger) Infof(text string, args ...interface{})

Infof formats the message with given args and logs the result with the Info level.

func (*Logger) Log

func (l *Logger) Log(level Level, m string)

Log sends a record containing the message `m` to the registered backends whose level is at least `level`.

func (*Logger) Notice added in v0.4.0

func (l *Logger) Notice(text string)

Notice logs a message with the Notice level.

func (*Logger) Noticef added in v0.4.0

func (l *Logger) Noticef(text string, args ...interface{})

Noticef formats the message with given args and logs the result with the Notice level.

func (*Logger) SetBackend

func (l *Logger) SetBackend(b ...Backend)

SetBackend sets the backend list to the logger. Any existing backend will be lost.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel changes the log level of all regisered backends to the given level.

func (*Logger) Trace added in v0.4.0

func (l *Logger) Trace(text string)

Trace logs a message with the Trace level.

func (*Logger) Tracef added in v0.4.0

func (l *Logger) Tracef(text string, args ...interface{})

Tracef formats the message with given args and logs the result with the Trace level.

func (*Logger) Warning

func (l *Logger) Warning(text string)

Warning logs a message with the Warning level.

func (*Logger) Warningf

func (l *Logger) Warningf(text string, args ...interface{})

Warningf formats the message with given args and logs the result with the Warning level.

type NoopBackend

type NoopBackend struct{}

NoopBackend does nothing and discards all log entries without writing them anywhere.

func NewNoopBackend

func NewNoopBackend() (*NoopBackend, error)

NewNoopBackend creates a noop backend.

func (*NoopBackend) Close added in v0.3.0

func (*NoopBackend) Close() error

Close is a noop.

func (*NoopBackend) Level

func (*NoopBackend) Level() Level

Level always returns DefeultLevel.

func (*NoopBackend) Reopen

func (*NoopBackend) Reopen() error

Reopen is a noop.

func (*NoopBackend) SetFormatter

func (*NoopBackend) SetFormatter(_ Formatter)

SetFormatter is a noop.

func (*NoopBackend) SetLevel

func (*NoopBackend) SetLevel(_ Level)

SetLevel is a noop.

func (*NoopBackend) Write

func (*NoopBackend) Write(_ *Record) error

Write is a noop.

type Record

type Record struct {
	Logger    string
	Timestamp time.Time
	Level     Level
	Message   string
}

Record contains the data to be logged. It is passed to a formatter to generate the logged message.

func NewRecord

func NewRecord(name string, l Level, m string) *Record

NewRecord creates a new record, setting its timestamp to time.Now().

type SyslogBackend

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

SyslogBackend writes the logs to a syslog system.

func NewSyslogBackend

func NewSyslogBackend(facilityName, tag string) (*SyslogBackend, error)

NewSyslogBackend initializes a new connection to a syslog server with the given facility. tag can contain an identifier for the log stream. It defaults to os.Arg[0].

func (*SyslogBackend) Close added in v0.3.0

func (sb *SyslogBackend) Close() error

Close closes the connection to the syslog daemon.

func (*SyslogBackend) Level

func (sb *SyslogBackend) Level() Level

Level returns the log level set for this backend.

func (*SyslogBackend) Reopen

func (*SyslogBackend) Reopen() error

Reopen is a no-op.

func (*SyslogBackend) SetFormatter

func (sb *SyslogBackend) SetFormatter(f Formatter)

SetFormatter defines the formatter for this backend.

func (*SyslogBackend) SetLevel

func (sb *SyslogBackend) SetLevel(level Level)

SetLevel changes the log level of the backend.

func (*SyslogBackend) Write

func (sb *SyslogBackend) Write(r *Record) error

Write sends an entry to the syslog server.

Jump to

Keyboard shortcuts

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