Skip to content
This repository was archived by the owner on Feb 21, 2023. It is now read-only.

Latest commit

History

159 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

gomol

GoDocBuild StatusCode CoverageGo Report Card

Gomol (Go Multi-Output Logger) is an MIT-licensed structured logging library for Go. 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.

Features

  • Attach meta-data to each log message with attributes
  • Multiple outputs at the same time
  • Pluggable Logger interface
  • Asynchronous logging so slow loggers won't slow down your application

Installation

Gomol can also be installed the standard way for Go:

go get github.com/aphistic/gomol
...
import "github.com/aphistic/gomol"

Vendoring is recommended!

Loggers

Gomol has a growing list of supported logging formats. The known loggers are listed below. If you have a logger you've written to support gomol and you'd like to add it to this list please either submit a pull request with the updated document or let me know and I can add it!

Other Usages

In addition to the loggers listed above, gomol can be used with other projects as well.

Examples

For brevity a lot of error checking has been omitted, be sure you do your checks!

This is a super basic example of adding a number of loggers and then logging a few messages:

package main
import (
"github.com/aphistic/gomol"
gc "github.com/aphistic/gomol-console"
gg "github.com/aphistic/gomol-gelf"
)
funcmain() {
// Add a console loggerconsoleCfg:=gc.NewConsoleLoggerConfig()
consoleLogger, _:=gc.NewConsoleLogger(consoleCfg)
// Set the template to display the full message, including// attributes.consoleLogger.SetTemplate(gc.NewTemplateFull())
gomol.AddLogger(consoleLogger)
// Add a GELF loggergelfCfg:=gg.NewGelfLoggerConfig()
gelfCfg.Hostname="localhost"gelfCfg.Port=12201gelfLogger, _:=gg.NewGelfLogger(gelfCfg)
gomol.AddLogger(gelfLogger)
// Set some global attrs that will be added to all// messages automaticallygomol.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=50000gomol.SetConfig(cfg)
// Initialize the loggersgomol.InitLoggers()
defergomol.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(chanerror)
gofunc() {
// 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.forerr:=rangech {
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 messageforidx:=1; idx<=10; idx++ {
gomol.Dbgm(gomol.NewAttrsFromMap(map[string]interface{}{
"msg_attr1": 4321,
}), "Test message %v", idx)
}
}

Fallback Logger

One feature gomol supports is the concept of a fallback logger. In some cases a logger may go unhealthy (a logger to a remote server, for example) and you want to log to a different logger to ensure log messages are not lost. The SetFallbackLogger method is available for these such instances.

A fallback logger will be triggered if any of the primary loggers goes unhealthy even if all others are fine. It's recommended the fallback logger not be added to the primary loggers or you may see duplicate messages. This does mean if multiple loggers are added as primary loggers and just one is unhealthy the fallback logger logger will be triggered.

To add a fallback logger there are two options. One is to use the default gomol instance (gomol.SetFallbackLogger()) and the other is to use the method on a Base instance:

import (
"github.com/aphistic/gomol""github.com/aphistic/gomol-console""github.com/aphistic/gomol-json"
)
funcmain() {
// Create a logger that logs over TCP using JSONjsonCfg:=gomoljson.NewJSONLoggerConfig("tcp://192.0.2.125:4321")
// Continue startup even if we can't connect initiallyjsonCfg.AllowDisconnectedInit=truejsonLogger, _:=gomoljson.NewJSONLogger(jsonCfg)
gomol.AddLogger(jsonLogger)
// Create a logger that logs to the consoleconsoleCfg:=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()
defergomol.ShutdownLoggers()
gomol.Debug("This is my message!")
}

About

Gomol is a library for structured, multiple-output logging for Go with extensible logging outputs

Topics

Resources

Stars

19 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages