log

package module
Version: v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 26, 2019 License: Apache-2.0 Imports: 12 Imported by: 86

README

go-log GoDoc Build Status Build status codecov.io Go Report Card

Log is a generic logging library based on logrus (this may change in the future), that minimize the exposure of src-d projects to logrus or any other logging library, as well defines the standard for configuration and usage of the logging libraries in the organization.

Installation

The recommended way to install go-log is:

go get -u gopkg.in/src-d/go-log.v1/...

Configuration

The configuration should be done always using environment variables. The list of available variables is:

  • LOG_LEVEL: Reporting level, values are "info", "debug", "warning" or "error".
  • LOG_FORMAT: Format of the log lines, values are "text", "json" or "fluentd", by default "text" is used. unless a terminal can't be detected, in this case, "json" is used instead.
  • LOG_FIELDS: Fields in JSON or fluentd format to be included in all the loggers.
  • LOG_FORCE_FORMAT: If true the fact of being in a terminal or not is ignored.

By default the logging is disabled if go-log is being executed in tests.

Usage

Basic usage

The most basic form of logging is made using the Infof, Debugf, Warningf and Errorf functions at the top level of the packages.

log.Infof("The answer to life, the universe and everything is %d", 42)
// INFO The answer to life, the universe and everything is 42

These functions use the DefaultLogger a logger lazy instanced when this method are called. This logger reads the configuration from the environment variables.

Logger instantiation

If you prefer to keep a reference to the Logger, in your packages or structs to have more control over it (for example for tests). A default Logger, can be instantiated using the New method.

logger := log.New(nil)
logger.Infof("The answer to life, the universe and everything is %d", 42)
// INFO The answer to life, the universe and everything is 42

Also, a new Logger can be created from other Logger in order to have contextual information, using the method Logger.New

logger := log.New(nil)

authorLogger := logger.New(log.Field{"author": "Douglas Adams"})
bookLogger.Infof("The Hitchhiker's Guide to the Galaxy")
bookLogger.Infof("Life, the Universe and Everything")
// INFO The Hitchhiker's Guide to the Galaxy author=Douglas Adams
// INFO Life, the Universe and Everything author=Douglas Adams

Or if you just want to add contextual information Logger.New to one log line you can use the Logger.With method.

logger := log.New(nil)

authorLogger := logger.New(log.Field{"author": "Douglas Adams"})
bookLogger.With(log.Fields{"isbn": "0-330-25864-8"}).Infof("The Hitchhiker's Guide to the Galaxy")
bookLogger.With(log.Fields{"isbn": "0-345-39182-9"}).Infof("Life, the Universe and Everything")
// INFO The Hitchhiker's Guide to the Galaxy author=Douglas Adams isbn=0-330-25864-8
// INFO Life, the Universe and Everything author=Douglas Adams isbn=0-345-39182-9
Logging errors

In go-log the errors are logged using the function Logger.Errorf:

logger, _ := log.New()

_, err := http.Get("https://en.wikipedia.org/wiki/Douglas_Adams")
if err != nil {
    logger.Errorf(err, "unable to retrieve page")
}

License

Apache License Version 2.0, see LICENSE

Documentation

Overview

Package log is a generic logging library based on logrus (this may change in the future), that minimize the exposure of src-d projects to logrus or any other logging library, as well defines the standard for configuration and usage of the logging libraries in the organization.

Index

Constants

View Source
const (
	// DebugLevel stands for debug logging level.
	DebugLevel = "debug"
	// InfoLevel stands for info logging level (default).
	InfoLevel = "info"
	// WarningLevel stands for warning logging level.
	WarningLevel = "warning"
	// ErrorLevel stands for error logging level.
	ErrorLevel = "error"

	// TextFormat stands for text logging format.
	TextFormat = "text"
	// JSONFormat stands for json logging format.
	JSONFormat = "json"
	// FluentdFormat stands for fluentd that can be parsed by Kubernetes and Google Container Engine.
	FluentdFormat = "fluentd"

	// DefaultLevel is the level used by LoggerFactory when Level is omitted.
	DefaultLevel = InfoLevel
	// DefaultFormat is the format used by LoggerFactory when Format is omitted.
	DefaultFormat = TextFormat
)

Variables

This section is empty.

Functions

func Debugf

func Debugf(format string, args ...interface{})

Debugf logs a message at level Debug.

func Errorf

func Errorf(err error, format string, args ...interface{})

Errorf logs an error with a message at level Error.

func Infof

func Infof(format string, args ...interface{})

Infof logs a message at level Info.

func Warningf

func Warningf(format string, args ...interface{})

Warningf logs a message at level Warning.

Types

type Fields

type Fields map[string]interface{}

Fields type, used to pass to `Logger.New`.

type Logger

type Logger interface {
	// New returns a copy of the current logger, adding the given Fields.
	New(Fields) Logger
	// With returns a copy of the current logger, adding the given Fields.
	// Alias of New, must be used when is chained with any message function.
	With(Fields) Logger
	// Debugf logs a message at level Debug.
	Debugf(format string, args ...interface{})
	// Infof logs a message at level Info.
	Infof(format string, args ...interface{})
	// Warningf logs a message at level Warning.
	Warningf(format string, args ...interface{})
	// Errorf logs an error with a message at level Error.
	Errorf(err error, format string, args ...interface{})
}

Logger represents a generic logger, based on logrus.Logger

var DefaultLogger Logger

DefaultLogger logger used by the `log.Infof()`, `log.Debugf()`, `log.Warningf()` and `log.Error()` functions. The DefaultLogger is lazily instantiated if nil once this functions are called.

func New

func New(f Fields) Logger

New returns a new logger using `DefaultFactory`, it panics if the environment variables are missconfigurated.

func With

func With(f Fields) Logger

With returns a copy of the current logger, adding the given Fields. Alias of New, must be used when is chained with any message function.

type LoggerFactory

type LoggerFactory struct {
	// Level as string, values are "info", "debug", "warning" or "error".
	Level string
	// Format as string, values are "text", "json" or "fluentnd", by default "text" is used.
	// when a terminal is not detected "json" is used instead.
	Format string
	// Fields in JSON or fluentd format to be used by configured in the new Logger.
	Fields string
	// ForceFormat if true the fact of being in a terminal or not is ignored.
	ForceFormat bool
}

LoggerFactory is a logger factory used to instanciate new Loggers, from string configuration, mainly coming from console flags.

var DefaultFactory *LoggerFactory

DefaultFactory logger used by the `log.New()` function. Pre-configured with the environment variables.

The environment variables are autogenerated from the `LoggerFactory` fields, with a prefix `log`, it means that three different variables are available `LOG_LEVEL`, `LOG_FORMAT`, `LOG_FIELDS`, for more information about the values please read the `LoggerFactory` documentation.

func (*LoggerFactory) ApplyToLogrus

func (f *LoggerFactory) ApplyToLogrus() error

ApplyToLogrus configures the standard logrus Logger with the LoggerFactory values. Useful to propagate the configuration to third-party libraries using logrus.

func (*LoggerFactory) New

func (f *LoggerFactory) New(fields Fields) (Logger, error)

New returns a new logger based on the LoggerFactory values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL