gologger

package
v0.0.0-...-a0a3655 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Overview

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 (
  "go.chromium.org/luci/common/logging"
  "go.chromium.org/luci/common/logging/gologger"
)

...

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.

Index

Constants

View Source
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.

View Source
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().

Variables

View Source
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.

Functions

func PickStdFormat

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.

Types

type LoggerConfig

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

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

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

Jump to

Keyboard shortcuts

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