Back to godoc.org
github.com/aphistic/gomol

package gomol

v0.0.0 (1546845)
Latest Go to latest
Published: Mar 14, 2019 | License: MIT | Module: github.com/aphistic/gomol

Overview

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.

Example

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)
}
Example (FallbackLogger)

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

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

func AddLogger(logger Logger)

AddLogger executes the same function on the default Base instance

func ClearAttrs

func ClearAttrs()

ClearAttrs executes the same function on the default Base instance

func ClearLoggers

func ClearLoggers() error

ClearLoggers executes the same function on the default Base instance

func Dbg

func Dbg(msg string) error

Dbg executes the same function on the default Base instance

func Dbgf

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

Dbgf executes the same function on the default Base instance

func Dbgm

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

Dbgm executes the same function on the default Base instance

func Debug

func Debug(msg string) error

Debug executes the same function on the default Base instance

func Debugf

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

Debugf executes the same function on the default Base instance

func Debugm

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

Debugm executes the same function on the default Base instance

func Die

func Die(exitCode int, msg string)

Die executes the same function on the default Base instance

func Dief

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

Dief executes the same function on the default Base instance

func Diem

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

Diem executes the same function on the default Base instance

func Err

func Err(msg string) error

Err executes the same function on the default Base instance

func Errf

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

Errf executes the same function on the default Base instance

func Errm

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

Errm executes the same function on the default Base instance

func Error

func Error(msg string) error

Error executes the same function on the default Base instance

func Errorf

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

Errorf executes the same function on the default Base instance

func Errorm

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

Errorm executes the same function on the default Base instance

func Fatal

func Fatal(msg string) error

Fatal executes the same function on the default Base instance

func Fatalf

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

Fatalf executes the same function on the default Base instance

func Fatalm

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

Fatalm executes the same function on the default Base instance

func Flush

func Flush()

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

func GetAttr

func GetAttr(key string) interface{}

GetAttr executes the same function on the default Base instance

func Info

func Info(msg string) error

Info executes the same function on the default Base instance

func Infof

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

Infof executes the same function on the default Base instance

func Infom

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

Infom executes the same function on the default Base instance

func InitLoggers

func InitLoggers() error

InitLoggers executes the same function on the default Base instance

func IsInitialized

func IsInitialized() bool

IsInitialized executes the same function on the default Base instance

func RemoveAttr

func RemoveAttr(key string)

RemoveAttr executes the same function on the default Base instance

func RemoveLogger

func RemoveLogger(logger Logger) error

RemoveLogger executes the same function on the default Base instance

func SetAttr

func SetAttr(key string, value interface{})

SetAttr executes the same function on the default Base instance

func SetConfig

func SetConfig(config *Config)

SetConfig executes the same function on the default Base instance

func SetErrorChan

func SetErrorChan(ch chan<- error)

SetErrorChan executes the same function on the default Base instance

func SetFallbackLogger

func SetFallbackLogger(logger Logger) error

SetFallbackLogger executes the same function on the default Base instance

func SetLogLevel

func SetLogLevel(level LogLevel)

SetLogLevel executes the same function on the default Base instance

func ShutdownLoggers

func ShutdownLoggers() error

ShutdownLoggers executes the same function on the default Base instance

func Warn

func Warn(msg string) error

Warn executes the same function on the default Base instance

func Warnf

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

Warnf executes the same function on the default Base instance

func Warning

func Warning(msg string) error

Warning executes the same function on the default Base instance

func Warningf

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

Warningf executes the same function on the default Base instance

func Warningm

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

Warningm executes the same function on the default Base instance

func Warnm

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

Warnm executes the same function on the default Base instance

type Attrs

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

Attrs represents a collection of key/value attributes

func NewAttrs

func NewAttrs() *Attrs

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

func NewAttrsFromAttrs

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

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

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

func (*Attrs) Attrs

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

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

func (*Attrs) GetAttr

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

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

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

func (*Attrs) RemoveAttr

func (a *Attrs) RemoveAttr(key string)

RemoveAttr will remove the attribute with the provided name.

func (*Attrs) SetAttr

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

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

func Default() *Base

Default will return the current default gomol Base logger

func NewBase

func NewBase(configs ...baseConfigFunc) *Base

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

func (*Base) AddLogger

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

AddLogger adds a new logger instance to the Base

func (*Base) ClearAttrs

func (b *Base) ClearAttrs()

ClearAttrs will remove all the attributes added to Base

func (*Base) ClearLoggers

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

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

Dbg is a short-hand version of Debug

func (*Base) Dbgf

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

Dbgf is a short-hand version of Debugf

func (*Base) Dbgm

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

Dbgm is a short-hand version of Debugm

func (*Base) Debug

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

Debug logs msg to all added loggers at LogLevel.LevelDebug

func (*Base) Debugf

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

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

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

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

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

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

Err is a short-hand version of Error

func (*Base) Errf

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

Errf is a short-hand version of Errorf

func (*Base) Errm

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

Errm is a short-hand version of Errorm

func (*Base) Error

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

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

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

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

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

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

func (b *Base) Flush()

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

func (*Base) GetAttr

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

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

Info logs msg to all added loggers at LogLevel.LevelInfo

func (*Base) Infof

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

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

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

func (b *Base) IsInitialized() bool

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

func (*Base) Log

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

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

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

NewLogAdapter creates a LogAdapter using Base to log messages

func (*Base) RemoveAttr

func (b *Base) RemoveAttr(key string)

RemoveAttr will remove the attribute with the name key.

func (*Base) RemoveLogger

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

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

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

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

func (*Base) SetErrorChan

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

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

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

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

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

Warn is a short-hand version of Warning

func (*Base) Warnf

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

Warnf is a short-hand version of Warningf

func (*Base) Warning

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

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

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

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

Warnm is a short-hand version of Warningm

type Config

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

func NewConfig() *Config

NewConfig creates a new configuration with default settings

type HealthCheckLogger

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

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

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

func NewLogAdapter(attrs *Attrs) *LogAdapter

NewLogAdapter executes the same function on the default Base instance

func NewLogAdapterFor

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

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

func (*LogAdapter) ClearAttrs

func (la *LogAdapter) ClearAttrs()

ClearAttrs removes all attributes for this LogAdapter only

func (*LogAdapter) Dbg

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

Dbg is a short-hand version of Debug

func (*LogAdapter) Dbgf

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

Dbgf is a short-hand version of Debugf

func (*LogAdapter) Dbgm

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

Dbgm is a short-hand version of Debugm

func (*LogAdapter) Debug

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

Debug logs msg to all added loggers at LogLevel.LevelDebug

func (*LogAdapter) Debugf

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

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

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

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

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

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

Err is a short-hand version of Error

func (*LogAdapter) Errf

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

Errf is a short-hand version of Errorf

func (*LogAdapter) Errm

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

Errm is a short-hand version of Errorm

func (*LogAdapter) Error

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

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

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

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

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

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

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

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

Info logs msg to all added loggers at LogLevel.LevelInfo

func (*LogAdapter) Infof

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

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

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

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

func (la *LogAdapter) RemoveAttr(key string)

RemoveAttr removes the attribute key for this LogAdapter only

func (*LogAdapter) SetAttr

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

SetAttr sets the attribute key to value for this LogAdapter only

func (*LogAdapter) SetLogLevel

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

func (la *LogAdapter) ShutdownLoggers() error

ShutdownLoggers will call the wrapped logger's ShutdownLoggers method.

func (*LogAdapter) Warn

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

Warn is a short-hand version of Warning

func (*LogAdapter) Warnf

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

Warnf is a short-hand version of Warningf

func (*LogAdapter) Warning

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

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

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

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

Warnm is a short-hand version of Warningm

type LogLevel

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

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

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

func (LogLevel) String

func (ll LogLevel) String() string

func (*LogLevel) UnmarshalJSON

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

type Logger

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

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

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

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

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

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

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

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

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.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
f or F : Jump to identifier