logger: github.com/digitalrebar/logger Index | Files | Directories

package logger

import "github.com/digitalrebar/logger"

logger implements a shared Logger to use across the various DigitalRebar packages. It includes in-memory saving of log messages from all sources in a ring buffer and the ability to specify a callback function that should be called every time a line is logged.

It is not intended to be a standalone logging package, but intead is designed to be used in conjunction with other local logging packages that provide the usual Printf, Fatalf, and Panicf calls.

Index

Package Files

logger.go

type Buffer Uses

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

Buffer is a ringbuffer that can be shared among several different Loggers. Buffers are responsible for flushing messages to local loggers and calling Publisher callbacks as needed.

func New Uses

func New(base Local) *Buffer

New returns a new Buffer which will write to the passed-in Local logger. A new Buffer will by default save 1000 Lines in memory. This number is adjustable on the fly with the KeepLines method.

func (*Buffer) DefaultLevel Uses

func (b *Buffer) DefaultLevel() Level

func (*Buffer) KeepLines Uses

func (b *Buffer) KeepLines(lines int) *Buffer

KeepLines sets the max number of lines that will be kept in memory by the Logger. It will discard older lines as appropriate.

func (*Buffer) Lines Uses

func (b *Buffer) Lines(count int) []*Line

Lines returns up to the last count lines logged. If count is a negative number, all the lines we currently have are kept.

func (*Buffer) Log Uses

func (b *Buffer) Log(service string) Logger

Log creates or reuses a Log for the passed-in Service. All logs returned by this method will share the same Group and have a common Seq. You can force a log into a different Group using that log's Fork method.

func (*Buffer) Logs Uses

func (b *Buffer) Logs() []Logger

Logs Returns all the Logs directly created by the Log method. Logs created via other means are not tracked by the Buffer.

func (*Buffer) MaxLines Uses

func (b *Buffer) MaxLines() int

MaxLines returns the current number of lines the Logger will keep in memory.

func (*Buffer) NewGroup Uses

func (b *Buffer) NewGroup() int64

NewGroup returns a new group number.

func (*Buffer) SetDefaultLevel Uses

func (b *Buffer) SetDefaultLevel(l Level) *Buffer

func (*Buffer) SetPublisher Uses

func (b *Buffer) SetPublisher(p Publisher) *Buffer

SetPublisher sets a callback function to be called whenever a Line will be written to a Local logger.

type Level Uses

type Level int32

Level is a log level. It consists of the usual logging levels.

const (
    // Trace should be used when you want detailed log messages that map
    // to the actual flow of control through the program.
    Trace Level = iota
    // Debug should be useful for debugging low-level problems that do
    // not necessarily require the level of detail that Trace provides.
    Debug Level = iota
    // Info should be used to emit information messages that do not
    // signal an unusual condition.
    Info Level = iota
    // Warn should be used to signal when something unusual happened
    // that the program was able to handle appropriatly.
    Warn Level = iota
    // Error should be used to signal when something unusal happened that
    // could not be handled gracefully, but that did not result in a
    // condition where we had to shut down.
    Error Level = iota
    // Fatal should be used where we need to shut down the program in order
    // to avoid data corruption, and there is no possibility of handling
    // in a programmatic fashion.
    Fatal Level = iota
    // Panic should be used where we need to abort whatever is happening,
    // and there is a possibility of handling or avoiding the error in a
    // programmatic fashion.
    Panic Level = iota
    // Audit logs must always be printed.
    Audit Level = iota
)

func ParseLevel Uses

func ParseLevel(s string) (Level, error)

ParseLevel returns the log level appropriate for the passed in string. It returns an error if s refers to an unknown level.

func (*Level) MarshalText Uses

func (l *Level) MarshalText() ([]byte, error)

func (Level) String Uses

func (l Level) String() string

func (*Level) UnmarshalText Uses

func (l *Level) UnmarshalText(t []byte) error

type Line Uses

type Line struct {
    // Group is an abstract number used to group Lines together
    Group int64
    // Seq is the sequence number that the Line was emitted in.
    // Sequence numbers are globally unique.
    Seq int64
    // Time is when the Line was created.
    Time time.Time
    // Service is the name of the log.
    Service string
    // Principal is the user or system that caused the log line to be emitted
    Principal string
    // Level is the level at which the line was logged.
    Level Level
    // File is the source file that generated the line
    File string
    // Line is the line number of the line that generated the line.
    Line int
    // Message is the message that was logged.
    Message string
    // Data is any auxillary data that was captured.
    Data []interface{}
    // Should the line be published or not as an event.
    IgnorePublish bool
}

Line is the smallest unit of things we log.

func (*Line) String Uses

func (l *Line) String() string

type Local Uses

type Local interface {
    Printf(string, ...interface{})
    Fatalf(string, ...interface{})
    Panicf(string, ...interface{})
}

Local allows us to accept as a local logger anything that has the usual basic logging methods

type Logger Uses

type Logger interface {
    Logf(Level, string, ...interface{})
    Tracef(string, ...interface{})
    Debugf(string, ...interface{})
    Infof(string, ...interface{})
    Warnf(string, ...interface{})
    Errorf(string, ...interface{})
    Fatalf(string, ...interface{})
    Panicf(string, ...interface{})
    Auditf(string, ...interface{})
    IsTrace() bool
    IsDebug() bool
    IsInfo() bool
    IsWarn() bool
    IsError() bool
    With(...interface{}) Logger
    Switch(string) Logger
    Fork() Logger
    NoPublish() Logger
    NoRepublish() Logger
    Level() Level
    SetLevel(Level) Logger
    Service() string
    SetService(string) Logger
    Principal() string
    SetPrincipal(string) Logger
    Trace(Level) Logger
    Buffer() *Buffer
    AddLine(*Line)
}

Logger is the interface that users of this package should expect.

Tracef, Debugf, Infof, Warnf, Errorf, Fatalf, and Panicf are the usual logging functions. They will add Lines to the Buffer based on the current Level of the Log.

Level and SetLevel get the current logging Level of the Logger

Service and SetService get and set the Service the Log will create Lines as.

Buffer returns a reference to the Buffer that stores messages the Log generates.

Fork returns an independent copy of the Log that has its own group ID.

With returns a new Logger that shares the same Group as its parent, but that has additional data that will be added to any Lines the new Logger generates.

Switch makes a Logger that logs with the same Service and Level as a default Logger with the passed-in service name, but with the current logger's Group

Trace returns a Logger that will log at the desired log level, and it and any Loggers created from it will silently ignore any attempts to change the Level

type Publisher Uses

type Publisher func(l *Line)

Publisher is a function to be called whenever a Line would be added to the ring buffer and sent to the local logger.

Directories

PathSynopsis
hclhcl implements compatibility with github.com/hashicorp/go-hclog

Package logger imports 6 packages (graph) and is imported by 4 packages. Updated 2019-04-13. Refresh now. Tools for package owners.