Categorygithub.com/op/go-logging
modulepackage
0.0.0-20160315200505-970db520ece7
Repository: https://github.com/op/go-logging.git
Documentation: pkg.go.dev

# README

Golang logging library

godoc build

Package logging implements a logging infrastructure for Go. Its output format is customizable and supports different logging backends like syslog, file and memory. Multiple backends can be utilized with different log levels per backend and logger.

NOTE: backwards compatibility promise have been dropped for master. Please vendor this package or use gopkg.in/op/go-logging.v1 for previous version. See changelog for details.

Example

Let's have a look at an example which demonstrates most of the features found in this library.

Example Output

package main

import (
	"os"

	"github.com/op/go-logging"
)

var log = logging.MustGetLogger("example")

// Example format string. Everything except the message has a custom color
// which is dependent on the log level. Many fields have a custom output
// formatting too, eg. the time returns the hour down to the milli second.
var format = logging.MustStringFormatter(
	`%{color}%{time:15:04:05.000} %{shortfunc} ▶ %{level:.4s} %{id:03x}%{color:reset} %{message}`,
)

// Password is just an example type implementing the Redactor interface. Any
// time this is logged, the Redacted() function will be called.
type Password string

func (p Password) Redacted() interface{} {
	return logging.Redact(string(p))
}

func main() {
	// For demo purposes, create two backend for os.Stderr.
	backend1 := logging.NewLogBackend(os.Stderr, "", 0)
	backend2 := logging.NewLogBackend(os.Stderr, "", 0)

	// For messages written to backend2 we want to add some additional
	// information to the output, including the used log level and the name of
	// the function.
	backend2Formatter := logging.NewBackendFormatter(backend2, format)

	// Only errors and more severe messages should be sent to backend1
	backend1Leveled := logging.AddModuleLevel(backend1)
	backend1Leveled.SetLevel(logging.ERROR, "")

	// Set the backends to be used.
	logging.SetBackend(backend1Leveled, backend2Formatter)

	log.Debugf("debug %s", Password("secret"))
	log.Info("info")
	log.Notice("notice")
	log.Warning("warning")
	log.Error("err")
	log.Critical("crit")
}

Installing

Using go get

$ go get github.com/op/go-logging

After this command go-logging is ready to use. Its source will be in:

$GOPATH/src/pkg/github.com/op/go-logging

You can use go get -u to update the package.

Documentation

For docs, see http://godoc.org/github.com/op/go-logging or run:

$ godoc github.com/op/go-logging

Additional resources

  • wslog -- exposes log messages through a WebSocket.

# Packages

# Functions

AddModuleLevel wraps a log backend with knobs to have different log levels for different modules.
ConvertColors takes a list of ints representing colors for log levels and converts them into strings for ANSI color formatting.
GetLevel returns the logging level for the specified module.
GetLogger creates and returns a Logger object based on the module name.
InitForTesting is a convenient method when using logging in a test.
LogLevel returns the log level from a string representation.
MultiLogger creates a logger which contain multiple loggers.
MustGetLogger is like GetLogger but panics if the logger can't be created.
MustStringFormatter is equivalent to NewStringFormatter with a call to panic on error.
NewBackendFormatter creates a new backend which makes all records that passes through it beeing formatted by the specific formatter.
NewChannelMemoryBackend creates a simple in-memory logging backend which utilizes a go channel for communication.
NewLogBackend creates a new LogBackend.
NewMemoryBackend creates a simple in-memory logging backend.
NewStringFormatter returns a new Formatter which outputs the log record as a string based on the 'verbs' specified in the format string.
NewSyslogBackend connects to the syslog daemon using UNIX sockets with the given prefix.
NewSyslogBackendPriority is the same as NewSyslogBackend, but with custom syslog priority, like syslog.LOG_LOCAL3|syslog.LOG_DEBUG etc.
Redact returns a string of * having the same length as s.
Reset restores the internal state of the logging library.
SetBackend replaces the backend currently set with the given new logging backend.
SetFormatter sets the default formatter for all new backends.
SetLevel sets the logging level for the specified module.

# Constants

# Variables

DefaultFormatter is the default formatter used and is only the message.
ErrInvalidLogLevel is used when an invalid log level has been used.
GlogFormatter mimics the glog format.

# Structs

ChannelMemoryBackend is very similar to the MemoryBackend, except that it internally utilizes a channel.
LogBackend utilizes the standard log module.
Logger is the actual logger which creates log records based on the functions called and passes them to the underlying logging backend.
MemoryBackend is a simple memory based logging backend that will not produce any output but merly keep records, up to the given size, in memory.
Record represents a log record and contains the timestamp when the record was created, an increasing id, filename and line and finally the actual formatted log line.
SyslogBackend is a simple logger to syslog backend.

# Interfaces

Backend is the interface which a log backend need to implement to be able to be used as a logging backend.
Formatter is the required interface for a custom log record formatter.
Leveled interface is the interface required to be able to add leveled logging.
LeveledBackend is a log backend with additional knobs for setting levels on individual modules to different levels.
Redactor is an interface for types that may contain sensitive information (like passwords), which shouldn't be printed to the log.

# Type aliases

Level defines all available log levels for log messages.