luci: Index | Files

package gologger

import ""

Package gologger is a compatibility layer between go-logging library and luci-go/common/logging.

Use it if you want to log to files or console.

Default usage (logging to stderr):

import (


ctx := context.Background()
ctx = gologger.StdConfig.Use(ctx)
logging.Infof(ctx, "Hello %s", "world")

If you want more control over where log goes or how it looks, instantiate custom LoggerConfig struct:

logCfg := gologger.LoggerConfig{Out: logFileWriter}
ctx = logCfg.Use(ctx)

If you know what you are doing you even can prepare go-logging.Logger instance yourself and plug it in into luci-go/common/logging:

logCfg := gologger.LoggerConfig{Logger: goLoggingLogger}
ctx = logCfg.Use(ctx)

Note that you almost never want to change logging level of the go-logging logger (that's why LoggerConfig doesn't expose it), use logging.SetLevel instead to do filtering before messages hit go-logging logger.


Package Files

config.go doc.go logger.go


const StdFormat = `[%{level:.1s}%{time:2006-01-02T15:04:05.000000Z07:00} ` +
    `%{pid} 0 %{shortfile}] %{message}`

StdFormat is a preferred logging format to use.

It is compatible with logging format used by luci-py. The zero after %{pid} is "thread ID" which is unavailable in go.

const StdFormatWithColor = `%{color}[%{level:.1s}%{time:2006-01-02T15:04:05.000000Z07:00} ` +
    `%{pid} 0 %{shortfile}]%{color:reset} %{message}`

StdFormatWithColor is same as StdFormat, except with fancy colors.

Use it when logging to terminal. Note that StdConfig will pick it automatically if it detects that given io.Writer is an os.File and it is a terminal. See PickStdFormat().


var StdConfig = LoggerConfig{Out: os.Stderr}

StdConfig defines default logger configuration.

It logs to Stderr using default logging format compatible with luci-py, see StdFormat.

Call StdConfig.Use(ctx) to install it as a default context logger.

func PickStdFormat Uses

func PickStdFormat(w io.Writer) string

PickStdFormat returns StdFormat for non terminal-backed files or StdFormatWithColor for io.Writers that are io.Files backed by a terminal.

Used by default StdConfig.

type LoggerConfig Uses

type LoggerConfig struct {
    Out    io.Writer // where to write the log to, required
    Format string    // how to format the log, default is PickStdFormat(Out)

    Logger *gol.Logger // if set, will be used as is, overrides everything else
    // contains filtered or unexported fields

LoggerConfig owns a go-logging logger, configured in some way.

Despite its name it is not a configuration. It is a stateful object that lazy-initializes go-logging logger on a first use.

If you are using os.File as Out, you are responsible for closing it when you are done with logging.

func (*LoggerConfig) NewLogger Uses

func (lc *LoggerConfig) NewLogger(c context.Context) logging.Logger

NewLogger returns new go-logging based logger bound to the given context.

It will use logging level and fields specified in the context. Pass 'nil' as a context to completely disable context-related checks. Note that default context (e.g. context.Background()) is configured for Info logging level, not Debug.

lc.NewLogger is in fact logging.Factory and can be used in SetFactory.

All loggers produced by LoggerConfig share single underlying go-logging Logger instance.

func (*LoggerConfig) Use Uses

func (lc *LoggerConfig) Use(c context.Context) context.Context

Use registers go-logging based logger as default logger of the context.

Package gologger imports 10 packages (graph) and is imported by 60 packages. Updated 2018-08-14. Refresh now. Tools for package owners.