gomol: github.com/aphistic/gomol Index | Examples | Files | Directories

package gomol

import "github.com/aphistic/gomol"

Package gomol is the GO Multi-Output Logger, a structured logging library supporting multiple outputs at once. Gomol grew from a desire to have a structured logging library that could write to any number of outputs while also keeping a small in-band footprint.

Gomol has a few basic concepts and most should be familiar to those who have used other logging libraries in the past. There are multiple logging levels and the ability to limit the levels that are logged.

In order to provide maximum flexibility gomol has the concept of a base for logging functionality, represented by the Base struct. The Base can have zero or more Logger instances added to it. A Logger is an implementation of a way to display or store log messages. For example, there is a Logger for logging to the console, one for logging to Graylog and others. Once Loggers are added to the Base and initialized any messages logged using that Base will be sent to all the loggers added.

In most use cases you will probably not need to create your own Base and can just use the default one created by gomol on startup. To use the default Base, simply call the functions in the root of the gomol package.

Code:

// Add a console logger
consoleCfg := gomolconsole.NewConsoleLoggerConfig()
consoleLogger, _ := gomolconsole.NewConsoleLogger(consoleCfg)
// Set the template to display the full message, including
// attributes.
consoleLogger.SetTemplate(gomolconsole.NewTemplateFull())
gomol.AddLogger(consoleLogger)

// Add a GELF logger
gelfCfg := gomolgelf.NewGelfLoggerConfig()
gelfCfg.Hostname = "localhost"
gelfCfg.Port = 12201
gelfLogger, _ := gomolgelf.NewGelfLogger(gelfCfg)
gomol.AddLogger(gelfLogger)

// Set some global attrs that will be added to all
// messages automatically
gomol.SetAttr("facility", "gomol.example")
gomol.SetAttr("another_attr", 1234)

// Configure gomol to add the filename and line number that the
// log was generated from, the internal sequence number to help
// with ordering events if your log system doesn't support a
// small enough sub-second resolution, and set the size of the
// internal queue (default is 10k messages).
cfg := gomol.NewConfig()
cfg.FilenameAttr = "filename"
cfg.LineNumberAttr = "line"
cfg.SequenceAttr = "sequence"
cfg.MaxQueueSize = 50000
gomol.SetConfig(cfg)

// Initialize the loggers
gomol.InitLoggers()
defer gomol.ShutdownLoggers()

// Create a channel on which to receive internal (asynchronous)
// logger errors. This is optional, but recommended in order to
// determine when logging may be dropping messages.
ch := make(chan error)

go func() {
    // This consumer is expected to be efficient as writes to
    // the channel are blocking. If this handler is slow, the
    // user should add a buffer to the channel, or manually
    // queue and batch errors for processing.

    for err := range ch {
        fmt.Printf("[Internal Error] %s\n", err.Error())
    }
}()

gomol.SetErrorChan(ch)

// Log some debug messages with message-level attrs
// that will be sent only with that message
for idx := 1; idx <= 10; idx++ {
    gomol.Dbgm(gomol.NewAttrsFromMap(map[string]interface{}{
        "msg_attr1": 4321,
    }), "Test message %v", idx)
}

ExampleFallbackLogger demonstrates how to use a fallback logger.

Code:

// Create a logger that logs over TCP using JSON
jsonCfg := gomoljson.NewJSONLoggerConfig("tcp://192.0.2.125:4321")
// Continue startup even if we can't connect initially
jsonCfg.AllowDisconnectedInit = true
jsonLogger, _ := gomoljson.NewJSONLogger(jsonCfg)
gomol.AddLogger(jsonLogger)

// Create a logger that logs to the console
consoleCfg := gomolconsole.NewConsoleLoggerConfig()
consoleLogger, _ := gomolconsole.NewConsoleLogger(consoleCfg)

// Set the fallback logger to the console so if the
// TCP JSON logger is unhealthy we still get logs
// to stdout.
_ = gomol.SetFallbackLogger(consoleLogger)

gomol.InitLoggers()
defer gomol.ShutdownLoggers()

gomol.Debug("This is my message!")

Index

Examples

Package Files

attrs.go base.go config.go default.go errors.go gomol.go log_adapter.go logger.go mem_logger.go message.go queue.go runtime.go template.go

Variables

var (
    // ErrUnknownLevel is returned when the provided log level is not known
    ErrUnknownLevel = errors.New("unknown log level")

    // ErrMessageDropped is reported if loggers are backed up and an old log
    // message has been forgotten
    ErrMessageDropped = errors.New("queue full - dropping message")

    // ErrNotInitialized is returned when a resource has not been completely
    // initialized
    ErrNotInitialized = errors.New("not initialized")
)

func AddLogger Uses

func AddLogger(logger Logger)

AddLogger executes the same function on the default Base instance

func ClearAttrs Uses

func ClearAttrs()

ClearAttrs executes the same function on the default Base instance

func ClearLoggers Uses

func ClearLoggers() error

ClearLoggers executes the same function on the default Base instance

func Dbg Uses

func Dbg(msg string) error

Dbg executes the same function on the default Base instance

func Dbgf Uses

func Dbgf(msg string, a ...interface{}) error

Dbgf executes the same function on the default Base instance

func Dbgm Uses

func Dbgm(m *Attrs, msg string, a ...interface{}) error

Dbgm executes the same function on the default Base instance

func Debug Uses

func Debug(msg string) error

Debug executes the same function on the default Base instance

func Debugf Uses

func Debugf(msg string, a ...interface{}) error

Debugf executes the same function on the default Base instance

func Debugm Uses

func Debugm(m *Attrs, msg string, a ...interface{}) error

Debugm executes the same function on the default Base instance

func Die Uses

func Die(exitCode int, msg string)

Die executes the same function on the default Base instance

func Dief Uses

func Dief(exitCode int, msg string, a ...interface{})

Dief executes the same function on the default Base instance

func Diem Uses

func Diem(exitCode int, m *Attrs, msg string, a ...interface{})

Diem executes the same function on the default Base instance

func Err Uses

func Err(msg string) error

Err executes the same function on the default Base instance

func Errf Uses

func Errf(msg string, a ...interface{}) error

Errf executes the same function on the default Base instance

func Errm Uses

func Errm(m *Attrs, msg string, a ...interface{}) error

Errm executes the same function on the default Base instance

func Error Uses

func Error(msg string) error

Error executes the same function on the default Base instance

func Errorf Uses

func Errorf(msg string, a ...interface{}) error

Errorf executes the same function on the default Base instance

func Errorm Uses

func Errorm(m *Attrs, msg string, a ...interface{}) error

Errorm executes the same function on the default Base instance

func Fatal Uses

func Fatal(msg string) error

Fatal executes the same function on the default Base instance

func Fatalf Uses

func Fatalf(msg string, a ...interface{}) error

Fatalf executes the same function on the default Base instance

func Fatalm Uses

func Fatalm(m *Attrs, msg string, a ...interface{}) error

Fatalm executes the same function on the default Base instance

func Flush Uses

func Flush()

Flush will wait until all messages currently queued are distributed to all initialized loggers

func GetAttr Uses

func GetAttr(key string) interface{}

GetAttr executes the same function on the default Base instance

func Info Uses

func Info(msg string) error

Info executes the same function on the default Base instance

func Infof Uses

func Infof(msg string, a ...interface{}) error

Infof executes the same function on the default Base instance

func Infom Uses

func Infom(m *Attrs, msg string, a ...interface{}) error

Infom executes the same function on the default Base instance

func InitLoggers Uses

func InitLoggers() error

InitLoggers executes the same function on the default Base instance

func IsInitialized Uses

func IsInitialized() bool

IsInitialized executes the same function on the default Base instance

func RemoveAttr Uses

func RemoveAttr(key string)

RemoveAttr executes the same function on the default Base instance

func RemoveLogger Uses

func RemoveLogger(logger Logger) error

RemoveLogger executes the same function on the default Base instance

func SetAttr Uses

func SetAttr(key string, value interface{})

SetAttr executes the same function on the default Base instance

func SetConfig Uses

func SetConfig(config *Config)

SetConfig executes the same function on the default Base instance

func SetErrorChan Uses

func SetErrorChan(ch chan<- error)

SetErrorChan executes the same function on the default Base instance

func SetFallbackLogger Uses

func SetFallbackLogger(logger Logger) error

SetFallbackLogger executes the same function on the default Base instance

func SetLogLevel Uses

func SetLogLevel(level LogLevel)

SetLogLevel executes the same function on the default Base instance

func ShutdownLoggers Uses

func ShutdownLoggers() error

ShutdownLoggers executes the same function on the default Base instance

func Warn Uses

func Warn(msg string) error

Warn executes the same function on the default Base instance

func Warnf Uses

func Warnf(msg string, a ...interface{}) error

Warnf executes the same function on the default Base instance

func Warning Uses

func Warning(msg string) error

Warning executes the same function on the default Base instance

func Warningf Uses

func Warningf(msg string, a ...interface{}) error

Warningf executes the same function on the default Base instance

func Warningm Uses

func Warningm(m *Attrs, msg string, a ...interface{}) error

Warningm executes the same function on the default Base instance

func Warnm Uses

func Warnm(m *Attrs, msg string, a ...interface{}) error

Warnm executes the same function on the default Base instance

type Attrs Uses

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

Attrs represents a collection of key/value attributes

func NewAttrs Uses

func NewAttrs() *Attrs

NewAttrs will create a new Attrs struct with an empty set of attributes.

func NewAttrsFromAttrs Uses

func NewAttrsFromAttrs(attrs ...*Attrs) *Attrs

NewAttrsFromAttrs is a convenience function that will accept zero or more existing Attrs, create a new Attrs and then merge all the supplied Attrs values into the new Attrs instance.

func NewAttrsFromMap Uses

func NewAttrsFromMap(attrs map[string]interface{}) *Attrs

NewAttrsFromMap will create a new Attrs struct with the given attributes pre-populated

func (*Attrs) Attrs Uses

func (a *Attrs) Attrs() map[string]interface{}

Attrs will return a map of the attributes added to the struct.

func (*Attrs) GetAttr Uses

func (a *Attrs) GetAttr(key string) interface{}

GetAttr gets the value of the attribute with the provided name. If the attribute does not exist, nil will be returned

func (*Attrs) MergeAttrs Uses

func (a *Attrs) MergeAttrs(attrs *Attrs)

MergeAttrs accepts another existing Attrs and merges the attributes into its own.

func (*Attrs) RemoveAttr Uses

func (a *Attrs) RemoveAttr(key string)

RemoveAttr will remove the attribute with the provided name.

func (*Attrs) SetAttr Uses

func (a *Attrs) SetAttr(key string, value interface{}) *Attrs

SetAttr will set key to the provided value. If the attribute already exists the value will be replaced with the new value.

type Base Uses

type Base struct {
    BaseAttrs *Attrs
    // contains filtered or unexported fields
}

Base holds an instance of all information needed for logging. It is possible to create multiple instances of Base if multiple sets of loggers or attributes are desired.

func Default Uses

func Default() *Base

Default will return the current default gomol Base logger

func NewBase Uses

func NewBase(configs ...baseConfigFunc) *Base

NewBase creates a new instance of Base with default values set.

func (*Base) AddLogger Uses

func (b *Base) AddLogger(logger Logger) error

AddLogger adds a new logger instance to the Base

func (*Base) ClearAttrs Uses

func (b *Base) ClearAttrs()

ClearAttrs will remove all the attributes added to Base

func (*Base) ClearLoggers Uses

func (b *Base) ClearLoggers() error

ClearLoggers will shut down and remove any loggers added to the Base. If an error occurs while shutting down one of the loggers, the list will not be cleared but any loggers that have already been shut down before the error occurred will remain shut down.

func (*Base) Dbg Uses

func (b *Base) Dbg(msg string) error

Dbg is a short-hand version of Debug

func (*Base) Dbgf Uses

func (b *Base) Dbgf(msg string, a ...interface{}) error

Dbgf is a short-hand version of Debugf

func (*Base) Dbgm Uses

func (b *Base) Dbgm(m *Attrs, msg string, a ...interface{}) error

Dbgm is a short-hand version of Debugm

func (*Base) Debug Uses

func (b *Base) Debug(msg string) error

Debug logs msg to all added loggers at LogLevel.LevelDebug

func (*Base) Debugf Uses

func (b *Base) Debugf(msg string, a ...interface{}) error

Debugf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelDebug

func (*Base) Debugm Uses

func (b *Base) Debugm(m *Attrs, msg string, a ...interface{}) error

Debugm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelDebug. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*Base) Die Uses

func (b *Base) Die(exitCode int, msg string)

Die will log a message using Fatal, call ShutdownLoggers and then exit the application with the provided exit code.

func (*Base) Dief Uses

func (b *Base) Dief(exitCode int, msg string, a ...interface{})

Dief will log a message using Fatalf, call ShutdownLoggers and then exit the application with the provided exit code.

func (*Base) Diem Uses

func (b *Base) Diem(exitCode int, m *Attrs, msg string, a ...interface{})

Diem will log a message using Fatalm, call ShutdownLoggers and then exit the application with the provided exit code.

func (*Base) Err Uses

func (b *Base) Err(msg string) error

Err is a short-hand version of Error

func (*Base) Errf Uses

func (b *Base) Errf(msg string, a ...interface{}) error

Errf is a short-hand version of Errorf

func (*Base) Errm Uses

func (b *Base) Errm(m *Attrs, msg string, a ...interface{}) error

Errm is a short-hand version of Errorm

func (*Base) Error Uses

func (b *Base) Error(msg string) error

Error uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError

func (*Base) Errorf Uses

func (b *Base) Errorf(msg string, a ...interface{}) error

Errorf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError

func (*Base) Errorm Uses

func (b *Base) Errorm(m *Attrs, msg string, a ...interface{}) error

Errorm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*Base) Fatal Uses

func (b *Base) Fatal(msg string) error

Fatal uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal

func (*Base) Fatalf Uses

func (b *Base) Fatalf(msg string, a ...interface{}) error

Fatalf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal

func (*Base) Fatalm Uses

func (b *Base) Fatalm(m *Attrs, msg string, a ...interface{}) error

Fatalm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*Base) Flush Uses

func (b *Base) Flush()

Flush will wait until all messages currently queued are distributed to all initialized loggers

func (*Base) GetAttr Uses

func (b *Base) GetAttr(key string) interface{}

GetAttr will return the current value for the given attribute key. If the key isn't set this will return nil

func (*Base) Info Uses

func (b *Base) Info(msg string) error

Info logs msg to all added loggers at LogLevel.LevelInfo

func (*Base) Infof Uses

func (b *Base) Infof(msg string, a ...interface{}) error

Infof uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelInfo

func (*Base) Infom Uses

func (b *Base) Infom(m *Attrs, msg string, a ...interface{}) error

Infom uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelInfo. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*Base) InitLoggers Uses

func (b *Base) InitLoggers() error

InitLoggers will run InitLogger on each Logger that has been added to the Base. If an error occurs in initializing a logger, the loggers that have already been initialized will continue to be initialized.

func (*Base) IsInitialized Uses

func (b *Base) IsInitialized() bool

IsInitialized returns true if InitLoggers has been successfully run on the Base

func (*Base) Log Uses

func (b *Base) Log(level LogLevel, m *Attrs, msg string, a ...interface{}) error

Log will log a message at the provided level to all added loggers with the timestamp set to the time Log was called.

func (*Base) LogWithTime Uses

func (b *Base) LogWithTime(level LogLevel, ts time.Time, m *Attrs, msg string, a ...interface{}) error

LogWithTime will log a message at the provided level to all added loggers with the timestamp set to the value of ts.

func (*Base) NewLogAdapter Uses

func (b *Base) NewLogAdapter(attrs *Attrs) *LogAdapter

NewLogAdapter creates a LogAdapter using Base to log messages

func (*Base) RemoveAttr Uses

func (b *Base) RemoveAttr(key string)

RemoveAttr will remove the attribute with the name key.

func (*Base) RemoveLogger Uses

func (b *Base) RemoveLogger(logger Logger) error

RemoveLogger will run ShutdownLogger on the given logger and then remove the given Logger from the list in Base

func (*Base) SetAttr Uses

func (b *Base) SetAttr(key string, value interface{})

SetAttr will set the value for the attribute with the name key. If the key already exists it will be overwritten with the new value.

func (*Base) SetConfig Uses

func (b *Base) SetConfig(config *Config)

SetConfig will set the configuration for the Base to the given Config

func (*Base) SetErrorChan Uses

func (b *Base) SetErrorChan(ch chan<- error)

SetErrorChan will register a channel as the consumer of internal error events. This channel will be closed once ShutdownLoggers has finished. The consumer of this channel is expected to be efficient as writing to this channel will block.

func (*Base) SetFallbackLogger Uses

func (b *Base) SetFallbackLogger(logger Logger) error

SetFallbackLogger sets a Logger to be used if there aren't any loggers added or any of the added loggers are in a degraded or unhealthy state. A Logger passed to SetFallbackLogger will be initialized if it hasn't been already. In addition, if the Logger fails to initialize completely the fallback logger will fail to be set.

func (*Base) SetLogLevel Uses

func (b *Base) SetLogLevel(level LogLevel)

SetLogLevel sets the level messages will be logged at. It will log any message that is at the level or more severe than the level.

func (*Base) ShutdownLoggers Uses

func (b *Base) ShutdownLoggers() error

ShutdownLoggers will run ShutdownLogger on each Logger in Base. If an error occurs while shutting down a Logger, the error will be returned and all the loggers that were already shut down will remain shut down.

func (*Base) Warn Uses

func (b *Base) Warn(msg string) error

Warn is a short-hand version of Warning

func (*Base) Warnf Uses

func (b *Base) Warnf(msg string, a ...interface{}) error

Warnf is a short-hand version of Warningf

func (*Base) Warning Uses

func (b *Base) Warning(msg string) error

Warning uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning

func (*Base) Warningf Uses

func (b *Base) Warningf(msg string, a ...interface{}) error

Warningf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning

func (*Base) Warningm Uses

func (b *Base) Warningm(m *Attrs, msg string, a ...interface{}) error

Warningm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*Base) Warnm Uses

func (b *Base) Warnm(m *Attrs, msg string, a ...interface{}) error

Warnm is a short-hand version of Warningm

type Config Uses

type Config struct {
    // FilenameAttr is the name of the attribute to put the log location's
    // filename in.  This comes at a slight performance penalty.
    FilenameAttr string

    // LineNumberAttr is the name of the attribute to put the log location's
    // line number in.  This comes at a slight performance penalty.
    LineNumberAttr string

    // SequenceAttr is the name of the attribute to put the log message's sequence
    // number in.  The sequence number is an incrementing number for each log message
    // processed by a Base.
    SequenceAttr string

    // MaxQueueSize is the number of log messages which will be queued before old
    // messages are discarded.  This value takes effect once InitLoggers is called.
    // Further changes to this value will not increase or decrease the queue size.
    MaxQueueSize uint
}

Config is the runtime configuration for Gomol

func NewConfig Uses

func NewConfig() *Config

NewConfig creates a new configuration with default settings

type HealthCheckLogger Uses

type HealthCheckLogger interface {
    Logger

    Healthy() bool
}

HealthCheckLogger is an interface a Logger can implement to provide hinting at whether it is healthy or not. If a Logger does not implement HealthCheckLogger it is always assumed to be healthy.

type HookPreQueue Uses

type HookPreQueue interface {
    Logger

    PreQueue(msg *Message) error
}

HookPreQueue is an interface a Logger can implement to be able to inspect and modify a Message before it is added to the queue

type LogAdapter Uses

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

LogAdapter provides a way to easily override certain log attributes without modifying the base attributes or specifying them for every log message.

func NewLogAdapter Uses

func NewLogAdapter(attrs *Attrs) *LogAdapter

NewLogAdapter executes the same function on the default Base instance

func NewLogAdapterFor Uses

func NewLogAdapterFor(base WrappableLogger, attrs *Attrs) *LogAdapter

NewLogAdapterFor creates a LogAdapter that wraps the given loger with the given attributes.

func (*LogAdapter) ClearAttrs Uses

func (la *LogAdapter) ClearAttrs()

ClearAttrs removes all attributes for this LogAdapter only

func (*LogAdapter) Dbg Uses

func (la *LogAdapter) Dbg(msg string) error

Dbg is a short-hand version of Debug

func (*LogAdapter) Dbgf Uses

func (la *LogAdapter) Dbgf(msg string, a ...interface{}) error

Dbgf is a short-hand version of Debugf

func (*LogAdapter) Dbgm Uses

func (la *LogAdapter) Dbgm(m *Attrs, msg string, a ...interface{}) error

Dbgm is a short-hand version of Debugm

func (*LogAdapter) Debug Uses

func (la *LogAdapter) Debug(msg string) error

Debug logs msg to all added loggers at LogLevel.LevelDebug

func (*LogAdapter) Debugf Uses

func (la *LogAdapter) Debugf(msg string, a ...interface{}) error

Debugf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelDebug

func (*LogAdapter) Debugm Uses

func (la *LogAdapter) Debugm(m *Attrs, msg string, a ...interface{}) error

Debugm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelDebug. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*LogAdapter) Die Uses

func (la *LogAdapter) Die(exitCode int, msg string)

Die will log a message using Fatal, call ShutdownLoggers and then exit the application with the provided exit code.

func (*LogAdapter) Dief Uses

func (la *LogAdapter) Dief(exitCode int, msg string, a ...interface{})

Dief will log a message using Fatalf, call ShutdownLoggers and then exit the application with the provided exit code.

func (*LogAdapter) Diem Uses

func (la *LogAdapter) Diem(exitCode int, m *Attrs, msg string, a ...interface{})

Diem will log a message using Fatalm, call ShutdownLoggers and then exit the application with the provided exit code.

func (*LogAdapter) Err Uses

func (la *LogAdapter) Err(msg string) error

Err is a short-hand version of Error

func (*LogAdapter) Errf Uses

func (la *LogAdapter) Errf(msg string, a ...interface{}) error

Errf is a short-hand version of Errorf

func (*LogAdapter) Errm Uses

func (la *LogAdapter) Errm(m *Attrs, msg string, a ...interface{}) error

Errm is a short-hand version of Errorm

func (*LogAdapter) Error Uses

func (la *LogAdapter) Error(msg string) error

Error uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError

func (*LogAdapter) Errorf Uses

func (la *LogAdapter) Errorf(msg string, a ...interface{}) error

Errorf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError

func (*LogAdapter) Errorm Uses

func (la *LogAdapter) Errorm(m *Attrs, msg string, a ...interface{}) error

Errorm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelError. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*LogAdapter) Fatal Uses

func (la *LogAdapter) Fatal(msg string) error

Fatal uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal

func (*LogAdapter) Fatalf Uses

func (la *LogAdapter) Fatalf(msg string, a ...interface{}) error

Fatalf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal

func (*LogAdapter) Fatalm Uses

func (la *LogAdapter) Fatalm(m *Attrs, msg string, a ...interface{}) error

Fatalm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelFatal. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*LogAdapter) GetAttr Uses

func (la *LogAdapter) GetAttr(key string) interface{}

GetAttr gets the attribute with the given key for this LogAdapter only. If the key doesn't exist on this LogAdapter it will return nil

func (*LogAdapter) Info Uses

func (la *LogAdapter) Info(msg string) error

Info logs msg to all added loggers at LogLevel.LevelInfo

func (*LogAdapter) Infof Uses

func (la *LogAdapter) Infof(msg string, a ...interface{}) error

Infof uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelInfo

func (*LogAdapter) Infom Uses

func (la *LogAdapter) Infom(m *Attrs, msg string, a ...interface{}) error

Infom uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelInfo. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*LogAdapter) Log Uses

func (la *LogAdapter) Log(level LogLevel, attrs *Attrs, msg string, a ...interface{}) error

Log will log a message at the provided level to all loggers added to the Base associated with this LogAdapter

func (*LogAdapter) LogWithTime Uses

func (la *LogAdapter) LogWithTime(level LogLevel, ts time.Time, attrs *Attrs, msg string, a ...interface{}) error

LogWithTime will log a message at the provided level to all loggers added to the Base associated with this LogAdapter. It is similar to Log except the timestamp will be set to the value of ts.

func (*LogAdapter) RemoveAttr Uses

func (la *LogAdapter) RemoveAttr(key string)

RemoveAttr removes the attribute key for this LogAdapter only

func (*LogAdapter) SetAttr Uses

func (la *LogAdapter) SetAttr(key string, value interface{})

SetAttr sets the attribute key to value for this LogAdapter only

func (*LogAdapter) SetLogLevel Uses

func (la *LogAdapter) SetLogLevel(level LogLevel)

SetLogLevel sets the level the current LogAdapter will log at. The level is still filtered at the parent level, though, so if a LogAdapter is set to log at Debug while the parent is set to log at Info, the Debug message will not be logged because it will be filtered by the parent logger.

func (*LogAdapter) ShutdownLoggers Uses

func (la *LogAdapter) ShutdownLoggers() error

ShutdownLoggers will call the wrapped logger's ShutdownLoggers method.

func (*LogAdapter) Warn Uses

func (la *LogAdapter) Warn(msg string) error

Warn is a short-hand version of Warning

func (*LogAdapter) Warnf Uses

func (la *LogAdapter) Warnf(msg string, a ...interface{}) error

Warnf is a short-hand version of Warningf

func (*LogAdapter) Warning Uses

func (la *LogAdapter) Warning(msg string) error

Warning uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning

func (*LogAdapter) Warningf Uses

func (la *LogAdapter) Warningf(msg string, a ...interface{}) error

Warningf uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning

func (*LogAdapter) Warningm Uses

func (la *LogAdapter) Warningm(m *Attrs, msg string, a ...interface{}) error

Warningm uses msg as a format string with subsequent parameters as values and logs the resulting message to all added loggers at LogLevel.LevelWarning. It will also merge all attributes passed in m with any attributes added to Base and include them with the message if the Logger supports it.

func (*LogAdapter) Warnm Uses

func (la *LogAdapter) Warnm(m *Attrs, msg string, a ...interface{}) error

Warnm is a short-hand version of Warningm

type LogLevel Uses

type LogLevel int

LogLevel represents the level a message is logged at.

const (
    // LevelDebug designates messages that are most useful when debugging applications.
    LevelDebug LogLevel = 7
    // LevelInfo designates messages that show application progression
    LevelInfo LogLevel = 6
    // LevelWarning designates messages that could potentially cause problems
    LevelWarning LogLevel = 4
    // LevelError designates error messages that don't stop the application from running
    LevelError LogLevel = 3
    // LevelFatal designates messages for severe errors where the application cannot continue
    LevelFatal LogLevel = 2

    // LevelNone is used when configuring log levels to disable all log levels
    LevelNone LogLevel = math.MinInt32
)

func ToLogLevel Uses

func ToLogLevel(level string) (LogLevel, error)

ToLogLevel will take a string and return the appropriate log level for the string if known. If the string is not recognized it will return an ErrUnknownLevel error.

func (*LogLevel) MarshalJSON Uses

func (ll *LogLevel) MarshalJSON() ([]byte, error)

func (LogLevel) String Uses

func (ll LogLevel) String() string

func (*LogLevel) UnmarshalJSON Uses

func (ll *LogLevel) UnmarshalJSON(data []byte) error

type Logger Uses

type Logger interface {
    SetBase(*Base)

    InitLogger() error
    ShutdownLogger() error
    IsInitialized() bool // TODO This should be named Initialized()

    Logm(time.Time, LogLevel, map[string]interface{}, string) error
}

Logger is an interface libraries can implement to create their own loggers to be used with gomol.

type Message Uses

type Message struct {
    Level     LogLevel
    Timestamp time.Time
    Attrs     *Attrs
    Msg       string
    // contains filtered or unexported fields
}

Message holds the information for a log message

type Template Uses

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

Template represents a Go template (See text/template) that can be used when logging messages. Template includes a few useful template functions:

title
	Title cases a string
lcase
	Lower cases a string
ucase
	Upper cases a string
json
	JSON marshals an object
color
	Changes the color of any text after it to the log level's color
reset
	Resets the current color to the default color

func NewTemplate Uses

func NewTemplate(tpl string) (*Template, error)

NewTemplate creates a new Template from the given string. An error is returned if the template fails to compile.

func NewTemplateWithFuncMap Uses

func NewTemplateWithFuncMap(tpl string, funcMap template.FuncMap) (*Template, error)

NewTemplateWithFuncMap creates a new Template from the given string and a template FuncMap. The FuncMap available to the template during evaluation will also include the default values, if not overridden. An error is returned if the template fails to compile.

func (*Template) Execute Uses

func (t *Template) Execute(msg *TemplateMsg, colorize bool) (string, error)

Execute takes a TemplateMsg and applies it to the Go template. If colorize is true the template will insert ANSI color codes within the resulting string.

type TemplateMsg Uses

type TemplateMsg struct {
    Timestamp time.Time              `json:"timestamp"`
    Level     LogLevel               `json:"level"`
    LevelName string                 `json:"level_name"`
    Message   string                 `json:"message"`
    Attrs     map[string]interface{} `json:"attrs"`
}

TemplateMsg represents the parts of a message required to render a template

func NewTemplateMsg Uses

func NewTemplateMsg(timestamp time.Time, level LogLevel, m map[string]interface{}, msg string) *TemplateMsg

NewTemplateMsg will create a new TemplateMsg with values from the given parameters

type WrappableLogger Uses

type WrappableLogger interface {
    // LogWithTime will log a message at the provided level to all added loggers with the
    // timestamp set to the value of ts.
    LogWithTime(level LogLevel, ts time.Time, m *Attrs, msg string, a ...interface{}) error

    // Log will log a message at the provided level to all added loggers with the timestamp
    // set to the time Log was called.
    Log(level LogLevel, m *Attrs, msg string, a ...interface{}) error

    // ShutdownLoggers will run ShutdownLogger on each Logger in Base.  If an error occurs
    // while shutting down a Logger, the error will be returned and all the loggers that
    //were already shut down will remain shut down.
    ShutdownLoggers() error
}

WrappableLogger is an interface for a logger which can be wrapped by a LogAdapter. his interface is implemented by both Base and LogAdapter itself so that adapters can stack.

Directories

PathSynopsis
examples

Package gomol imports 17 packages (graph) and is imported by 14 packages. Updated 2019-03-23. Refresh now. Tools for package owners.