Documentation
¶
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 ¶
// 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) }
Output:
Example (FallbackLogger) ¶
ExampleFallbackLogger demonstrates how to use a fallback logger.
// 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!")
Output:
Index ¶
- Variables
- func AddLogger(logger Logger)
- func ClearAttrs()
- func ClearLoggers() error
- func Dbg(msg string) error
- func Dbgf(msg string, a ...interface{}) error
- func Dbgm(m *Attrs, msg string, a ...interface{}) error
- func Debug(msg string) error
- func Debugf(msg string, a ...interface{}) error
- func Debugm(m *Attrs, msg string, a ...interface{}) error
- func Die(exitCode int, msg string)
- func Dief(exitCode int, msg string, a ...interface{})
- func Diem(exitCode int, m *Attrs, msg string, a ...interface{})
- func Err(msg string) error
- func Errf(msg string, a ...interface{}) error
- func Errm(m *Attrs, msg string, a ...interface{}) error
- func Error(msg string) error
- func Errorf(msg string, a ...interface{}) error
- func Errorm(m *Attrs, msg string, a ...interface{}) error
- func Fatal(msg string) error
- func Fatalf(msg string, a ...interface{}) error
- func Fatalm(m *Attrs, msg string, a ...interface{}) error
- func Flush()
- func GetAttr(key string) interface{}
- func Info(msg string) error
- func Infof(msg string, a ...interface{}) error
- func Infom(m *Attrs, msg string, a ...interface{}) error
- func InitLoggers() error
- func IsInitialized() bool
- func RemoveAttr(key string)
- func RemoveLogger(logger Logger) error
- func SetAttr(key string, value interface{})
- func SetConfig(config *Config)
- func SetErrorChan(ch chan<- error)
- func SetFallbackLogger(logger Logger) error
- func SetLogLevel(level LogLevel)
- func ShutdownLoggers() error
- func Warn(msg string) error
- func Warnf(msg string, a ...interface{}) error
- func Warning(msg string) error
- func Warningf(msg string, a ...interface{}) error
- func Warningm(m *Attrs, msg string, a ...interface{}) error
- func Warnm(m *Attrs, msg string, a ...interface{}) error
- type Attrs
- type Base
- func (b *Base) AddLogger(logger Logger) error
- func (b *Base) ClearAttrs()
- func (b *Base) ClearLoggers() error
- func (b *Base) Dbg(msg string) error
- func (b *Base) Dbgf(msg string, a ...interface{}) error
- func (b *Base) Dbgm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Debug(msg string) error
- func (b *Base) Debugf(msg string, a ...interface{}) error
- func (b *Base) Debugm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Die(exitCode int, msg string)
- func (b *Base) Dief(exitCode int, msg string, a ...interface{})
- func (b *Base) Diem(exitCode int, m *Attrs, msg string, a ...interface{})
- func (b *Base) Err(msg string) error
- func (b *Base) Errf(msg string, a ...interface{}) error
- func (b *Base) Errm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Error(msg string) error
- func (b *Base) Errorf(msg string, a ...interface{}) error
- func (b *Base) Errorm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Fatal(msg string) error
- func (b *Base) Fatalf(msg string, a ...interface{}) error
- func (b *Base) Fatalm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Flush()
- func (b *Base) GetAttr(key string) interface{}
- func (b *Base) Info(msg string) error
- func (b *Base) Infof(msg string, a ...interface{}) error
- func (b *Base) Infom(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) InitLoggers() error
- func (b *Base) IsInitialized() bool
- func (b *Base) Log(level LogLevel, m *Attrs, msg string, a ...interface{}) error
- func (b *Base) LogWithTime(level LogLevel, ts time.Time, m *Attrs, msg string, a ...interface{}) error
- func (b *Base) NewLogAdapter(attrs *Attrs) *LogAdapter
- func (b *Base) RemoveAttr(key string)
- func (b *Base) RemoveLogger(logger Logger) error
- func (b *Base) SetAttr(key string, value interface{})
- func (b *Base) SetConfig(config *Config)
- func (b *Base) SetErrorChan(ch chan<- error)
- func (b *Base) SetFallbackLogger(logger Logger) error
- func (b *Base) SetLogLevel(level LogLevel)
- func (b *Base) ShutdownLoggers() error
- func (b *Base) Warn(msg string) error
- func (b *Base) Warnf(msg string, a ...interface{}) error
- func (b *Base) Warning(msg string) error
- func (b *Base) Warningf(msg string, a ...interface{}) error
- func (b *Base) Warningm(m *Attrs, msg string, a ...interface{}) error
- func (b *Base) Warnm(m *Attrs, msg string, a ...interface{}) error
- type Config
- type HealthCheckLogger
- type HookPreQueue
- type LogAdapter
- func (la *LogAdapter) ClearAttrs()
- func (la *LogAdapter) Dbg(msg string) error
- func (la *LogAdapter) Dbgf(msg string, a ...interface{}) error
- func (la *LogAdapter) Dbgm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Debug(msg string) error
- func (la *LogAdapter) Debugf(msg string, a ...interface{}) error
- func (la *LogAdapter) Debugm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Die(exitCode int, msg string)
- func (la *LogAdapter) Dief(exitCode int, msg string, a ...interface{})
- func (la *LogAdapter) Diem(exitCode int, m *Attrs, msg string, a ...interface{})
- func (la *LogAdapter) Err(msg string) error
- func (la *LogAdapter) Errf(msg string, a ...interface{}) error
- func (la *LogAdapter) Errm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Error(msg string) error
- func (la *LogAdapter) Errorf(msg string, a ...interface{}) error
- func (la *LogAdapter) Errorm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Fatal(msg string) error
- func (la *LogAdapter) Fatalf(msg string, a ...interface{}) error
- func (la *LogAdapter) Fatalm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) GetAttr(key string) interface{}
- func (la *LogAdapter) Info(msg string) error
- func (la *LogAdapter) Infof(msg string, a ...interface{}) error
- func (la *LogAdapter) Infom(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Log(level LogLevel, attrs *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) LogWithTime(level LogLevel, ts time.Time, attrs *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) RemoveAttr(key string)
- func (la *LogAdapter) SetAttr(key string, value interface{})
- func (la *LogAdapter) SetLogLevel(level LogLevel)
- func (la *LogAdapter) ShutdownLoggers() error
- func (la *LogAdapter) Warn(msg string) error
- func (la *LogAdapter) Warnf(msg string, a ...interface{}) error
- func (la *LogAdapter) Warning(msg string) error
- func (la *LogAdapter) Warningf(msg string, a ...interface{}) error
- func (la *LogAdapter) Warningm(m *Attrs, msg string, a ...interface{}) error
- func (la *LogAdapter) Warnm(m *Attrs, msg string, a ...interface{}) error
- type LogLevel
- type Logger
- type Message
- type Template
- type TemplateMsg
- type WrappableLogger
Examples ¶
Constants ¶
This section is empty.
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") )
Functions ¶
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 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 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 ¶
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 ¶
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
Types ¶
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 ¶
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 ¶
NewAttrsFromMap will create a new Attrs struct with the given attributes pre-populated
func (*Attrs) GetAttr ¶
GetAttr gets the value of the attribute with the provided name. If the attribute does not exist, nil will be returned
func (*Attrs) MergeAttrs ¶
MergeAttrs accepts another existing Attrs and merges the attributes into its own.
func (*Attrs) RemoveAttr ¶
RemoveAttr will remove the attribute with the provided name.
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 NewBase ¶
func NewBase(configs ...baseConfigFunc) *Base
NewBase creates a new instance of Base with default values set.
func (*Base) ClearAttrs ¶
func (b *Base) ClearAttrs()
ClearAttrs will remove all the attributes added to Base
func (*Base) ClearLoggers ¶
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) Debugf ¶
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 ¶
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 ¶
Die will log a message using Fatal, call ShutdownLoggers and then exit the application with the provided exit code.
func (*Base) Dief ¶
Dief will log a message using Fatalf, call ShutdownLoggers and then exit the application with the provided exit code.
func (*Base) Diem ¶
Diem will log a message using Fatalm, call ShutdownLoggers and then exit the application with the provided exit code.
func (*Base) 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
GetAttr will return the current value for the given attribute key. If the key isn't set this will return nil
func (*Base) Infof ¶
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 ¶
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 ¶
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 ¶
IsInitialized returns true if InitLoggers has been successfully run on the Base
func (*Base) Log ¶
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 ¶
RemoveAttr will remove the attribute with the name key.
func (*Base) RemoveLogger ¶
RemoveLogger will run ShutdownLogger on the given logger and then remove the given Logger from the list in Base
func (*Base) SetAttr ¶
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) SetErrorChan ¶
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 ¶
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 ¶
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 ¶
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) Warning ¶
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 ¶
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 ¶
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.
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
type HealthCheckLogger ¶
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 ¶
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.
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 ¶
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 (*LogLevel) UnmarshalJSON ¶
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 ¶
NewTemplate creates a new Template from the given string. An error is returned if the template fails to compile.
func NewTemplateWithFuncMap ¶
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.
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.