Categorygithub.com/google/deck
modulepackage
1.1.0
Repository: https://github.com/google/deck.git
Documentation: pkg.go.dev

# README

Deck - Flexible Logging Framework for Go

The Deck package provides a flexible logging framework for Go apps. Deck supports Windows Event Log, syslog, Go's standard logging, logging to file, and log replays for testing. Deck is extensible, so new log backends can integrate seamlessly into existing application code.

Standard Logging

Deck can be a drop in replacement for some other logging packages, with support for common functions like Info, Error, & Warning. The standard logging functions write their outputs immediately and don't support additional attributes.

deck.Info("an info message")
deck.Errorf("an %v message", errors.New("error!"))

Extensible Logging with Attributes

Sometimes we want to be able to pass more complex inputs to our log messages. Deck facilitates this by allowing custom attributes to be attached to log messages.

When using the Attribute-supporting log functions, the message isn't output immediately. We use the With() function to attach additional metadata to the message first. The metadata can be anything supported by an attached backend. Once fully marked up with attributes, the Go() function then performs the final write of the message.

In this example, a log message is marked with Verbosity level 2. Verbosity can be used to dynamically show or hide log events at runtime.

deck.InfoA("a verbose message").With(deck.V(2)).Go()

The EventLog backend for Windows supports Event IDs:

deck.InfoA("a windows event").With(eventlog.EventID(123)).Go()

Multiple attributes can be attributed to the same message:

deck.InfoA("a verbose windows event").With(eventlog.EventID(123), deck.V(3)).Go()

Backends

Deck's logging functionality revolves around backends. A backend is any logging destination that deck should write messages to. Backends are plug-and-play, so you can reconfigure your application's logging behavior simply by adding and removing different backends.

import (
  "github.com/google/deck"
  "github.com/google/deck/backends/logger"
)

deck.Add(logger.Init(os.Stdout, 0))

Cross-platform builds can support platform-specific log outputs by calling Add from platform-specific source files.

// my_app_windows.go

func init() {
  evt, err := eventlog.Init("My App")
  if err != nil {
    panic(err)
  }
  deck.Add(evt)
}
// my_app_linux.go

func init() {
  sl, err := syslog.Init("my-app", syslog.LOG_USER)
  if err != nil {
    panic(err)
  }
  deck.Add(sl)
}

eventlog Backend

The eventlog backend is for Windows only. This backend supports logging to the Windows Event Log. It exports the EventID attribute that allows logging messages with custom Event IDs.

eventlog Documentation.

logger Backend

The logger backend is based on Go's core log package. It can take any io.Writer, including os.Stdout, os.Stderr, io.Multiwriter, and any open file handles.

logger Documentation.

syslog Backend

The syslog backend is based on Go's core syslog package for Linux/Unix.

syslog Documentation.

discard Backend

The discard backend discards all log events. Deck requires at least one backend to be registered to handle logs. To suppress all output, add the discard backend.

discard Documentation.

glog Backend

The glog backend provides support for logging to the glog package from github.com/golang/glog. This backend supports the Depth attribute as well as exporting a V() attribute for glog's V-style logging.

glog Documentation.

replay Backend

The replay backend provides the ability to record and replay log events for use in testing.

replay Documentation.

Message Verbosity

Verbosity is a special attribute implemented by the deck core package. The V() function decorates logs with a custom verbosity, and the SetVerbosity() function determines which verbosity levels get output. This allows the verbosity level to be changed at runtime, such as via a flag or setting.

deck.SetVerbosity(*verbosityFlag)
...
log.InfoA("a level one message").With(deck.V(1)).Go()
log.InfoA("a level three message").With(deck.V(3)).Go()

In this example, if verbosityFlag is 2 or lower, only "a level one message" will print. If it's 3 or higher, both messages will print. Verbosity defaults to 0, and all non-Attribute functions will be at verbosity 0.

Custom Decks

The deck package builds a global deck whenever it's imported, and most implementations can just use this deck directly via the package-level logging functions. For more advanced use cases, multiple decks can be constructed using deck.New(). Each deck can have its own set of attached backends, and supports the same functionality as the global deck.

# Packages

No description provided by the author

# Functions

Add adds a backend to the default log deck.
Close closes all backends in the default deck.
Default returns the default (global) deck.
Depth is a general attribute that allows specifying log depth to backends.
Error immediately logs a message with no attributes to the default deck at the ERROR level.
ErrorA constructs a message in the default deck at the ERROR level.
Errorf immediately logs a message with no attributes according to the format specifier to the default deck at the ERROR level.
ErrorfA constructs a message according to the format specifier in the default deck at the ERROR level.
Errorln immediately logs a message with no attributes and with a trailing newline to the default deck at the ERROR level.
ErrorlnA constructs a message with a trailing newline in the default deck at the ERROR level.
Fatal immediately logs a message with no attributes to the default deck at the FATAL level.
FatalA constructs a message in the default deck at the FATAL level.
Fatalf immediately logs a message with no attributes according to the format specifier to the default deck at the FATAL level.
FatalfA constructs a message according to the format specifier in the default deck at the FATAL level.
Fatalln immediately logs a message with no attributes and with a trailing newline to the default deck at the FATAL level.
FatallnA constructs a message with a trailing newline in the default deck at the FATAL level.
Info immediately logs a message with no attributes to the default deck at the INFO level.
InfoA constructs a message in the default deck at the INFO level.
Infof immediately logs a message with no attributes according to the format specifier to the default deck at the INFO level.
InfofA constructs a message according to the format specifier in the default deck at the INFO level.
Infoln immediately logs a message with no attributes and with a trailing newline to the default deck at the INFO level.
InfolnA constructs a message with a trailing newline in the default deck at the INFO level.
New returns a new initialized log deck.
NewLog returns a new Log.
SetVerbosity sets the internal verbosity level of the default deck.
V is a special attribute that sets the verbosity level on a message.
Warning immediately logs a message with no attributes to the default deck at the WARNING level.
WarningA constructs a message in the default deck at the WARNING level.
Warningf immediately logs a message with no attributes according to the format specifier to the default deck at the WARNING level.
WarningfA constructs a message according to the format specifier in the default deck at the WARNING level.
Warningln immediately logs a message with no attributes with a trailing newline to the default deck at the WARNING level.
WarninglnA constructs a message with a trailing newline in the default deck at the WARNING level.

# Constants

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Structs

The Deck is the highest level of the logging hierarchy, consisting of one or more backends.
Log is a single log event that will be flushed to all registered backends.

# Interfaces

Backend is the interface that identifies logging backends with NewMessage and Close methods.
Composer is the interface that groups Compose and Write methods.

# Type aliases

An Attrib is an attribute that can be associated with Logs.
An AttribStore stores unique attributes associated with a given Log.
A Level is a recognized log level (Info, Error, etc).