xlog

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2022 License: Apache-2.0, Apache-2.0 Imports: 12 Imported by: 6

README

xlog logging package

Build Status Coverage Status

Cloned from https://github.com/coreos/pkg/tree/master/capnslog This clone has slight modifications on the original code, adding ability to specify log lever per package, and exposing Logger interface, not an implementation structure.

In this implementation the DEBUG level is above TRACE as trace is used to trace important functions calls and maybe enable on the cloud more friequently than DEBUG

Design Principles

package main is the place where logging gets turned on and routed

A library should not touch log options, only generate log entries. Libraries are silent until main lets them speak.

All log options are runtime-configurable

Still the job of main to expose these configurations. main may delegate this to, say, a configuration webhook, but does so explicitly.

There is one log object per package. It is registered under its repository and package name

main activates logging for its repository and any dependency repositories it would also like to have output in its logstream. main also dictates at which level each subpackage logs.

There is one output stream, and it is an io.Writer composed with a formatter

Splitting streams is probably not the job of your program, but rather, your log aggregation framework. If you must split output streams, again, main configures this and you can write a very simple two-output struct that satisfies io.Writer.

Fancy colorful formatting and JSON output are beyond the scope of a basic logging framework -- they're application/log-collector dependant. These are, at best, provided as options, but more likely, provided by your application.

Log objects are an interface

An object knows best how to print itself. Log objects can collect more interesting metadata if they wish, however, because text isn't going away anytime soon, they must all be marshalable to text. The simplest log object is a string, which returns itself. If you wish to do more fancy tricks for printing your log objects, see also JSON output -- introspect and write a formatter which can handle your advanced log interface. Making strings is the only thing guaranteed.

Log levels have specific meanings:
  • CRITICAL: Unrecoverable. Must fail.
  • ERROR: Data has been lost, a request has failed for a bad reason, or a required resource has been lost
  • WARNING: (Hopefully) Temporary conditions that may cause errors, but may work fine. A replica disappearing (that may reconnect) is a warning.
  • NOTICE: Normal, but important (uncommon) log information.
  • INFO: Normal, working log information, everything is fine, but helpful notices for auditing or common operations.
  • TRACE: Anything goes, from logging every function call as part of a common operation, to tracing execution of a query.
  • DEBUG: Print debug data.

Documentation

Overview

Package xlog has slight modifications on the original code, adding ability to specify log lever per package, and exposing Logger interface, not an implementation structure.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

This section is empty.

Variables

View Source
var (
	ColorOff = []byte("\033[0m")
)

color pallete map

View Source
var LevelColors = map[LogLevel][]byte{
	CRITICAL: colorLightRed,
	ERROR:    colorLightRed,
	WARNING:  colorLightOrange,
	NOTICE:   colorLightGreen,
	INFO:     colorLightCyan,
	DEBUG:    colorGray,
	TRACE:    colorGray,
}

LevelColors provides colors map

View Source
var Stderr *log.Logger

Stderr is an instance of standard logger to Stderr

Functions

func SetFormatter

func SetFormatter(f Formatter)

SetFormatter sets the formatting function for all logs.

func SetGlobalLogLevel

func SetGlobalLogLevel(l LogLevel)

SetGlobalLogLevel sets the log level for all packages in all repositories registered with PackageLogger.

func SetPackageLogLevel

func SetPackageLogLevel(repo, pkg string, l LogLevel)

SetPackageLogLevel sets the log level for a package in repo logger

func SetRepoLogLevel

func SetRepoLogLevel(repo string, l LogLevel)

SetRepoLogLevel sets the log level for all packages in repo logger

func String added in v0.9.0

func String(value interface{}) string

String returns string value stuitable for logging

Types

type ColorFormatter

type ColorFormatter struct {
	// contains filtered or unexported fields
}

ColorFormatter provides colorful logs format

func (*ColorFormatter) Flush

func (c *ColorFormatter) Flush()

Flush the logs

func (*ColorFormatter) Format

func (c *ColorFormatter) Format(pkg string, l LogLevel, depth int, entries ...interface{})

Format log entry string to the stream

func (*ColorFormatter) FormatKV added in v0.7.0

func (c *ColorFormatter) FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})

FormatKV log entry string to the stream, the entries are key/value pairs

func (*ColorFormatter) WithCaller added in v0.7.0

func (c *ColorFormatter) WithCaller(val bool) Formatter

WithCaller allows to configure if the caller shall be logged

type Formatter

type Formatter interface {
	// Format log entry string to the stream,
	// the entries are separated by space
	Format(pkg string, level LogLevel, depth int, entries ...interface{})
	// FormatKV log entry string to the stream,
	// the entries are key/value pairs
	FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})
	// Flush the logs
	Flush()
	// WithCaller allows to configure if the caller shall be logged
	WithCaller(bool) Formatter
}

Formatter defines an interface for formatting logs

func GetFormatter

func GetFormatter() Formatter

GetFormatter returns current formatter

func NewColorFormatter

func NewColorFormatter(w io.Writer, color bool) Formatter

NewColorFormatter returns an instance of ColorFormatter

func NewDefaultFormatter

func NewDefaultFormatter(out io.Writer) Formatter

NewDefaultFormatter returns an instance of default formatter

func NewLogFormatter

func NewLogFormatter(w io.Writer, prefix string, flag int) Formatter

NewLogFormatter is a helper to produce a new LogFormatter struct. It uses the golang log package to actually do the logging work so that logs look similar.

func NewNilFormatter

func NewNilFormatter() Formatter

NewNilFormatter is a helper to produce a new LogFormatter struct. It logs no messages so that you can cause part of your logging to be silent.

func NewPrettyFormatter

func NewPrettyFormatter(w io.Writer, debug bool) Formatter

NewPrettyFormatter returns an instance of PrettyFormatter

func NewStringFormatter

func NewStringFormatter(w io.Writer) Formatter

NewStringFormatter returns string-based formatter

type LogFormatter

type LogFormatter struct {
	// contains filtered or unexported fields
}

LogFormatter emulates the form of the traditional built-in logger.

func (*LogFormatter) Flush

func (lf *LogFormatter) Flush()

Flush is included so that the interface is complete, but is a no-op.

func (*LogFormatter) Format

func (lf *LogFormatter) Format(pkg string, _ LogLevel, _ int, entries ...interface{})

Format builds a log message for the LogFormatter. The LogLevel is ignored.

func (*LogFormatter) FormatKV added in v0.7.0

func (lf *LogFormatter) FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})

FormatKV log entry string to the stream, the entries are key/value pairs

func (*LogFormatter) WithCaller added in v0.7.0

func (lf *LogFormatter) WithCaller(val bool) Formatter

WithCaller allows to configure if the caller shall be logged

type LogLevel

type LogLevel int8

LogLevel is the set of all log levels.

const (
	// CRITICAL is the lowest log level; only errors which will end the program will be propagated.
	CRITICAL LogLevel = iota - 1
	// ERROR is for errors that are not fatal but lead to troubling behavior.
	ERROR
	// WARNING is for errors which are not fatal and not errors, but are unusual. Often sourced from misconfigurations.
	WARNING
	// NOTICE is for normal but significant conditions.
	NOTICE
	// INFO is a log level for common, everyday log updates.
	INFO
	// TRACE is for (potentially) call by call tracing of programs.
	TRACE
	// DEBUG is the default hidden level for more verbose updates about internal processes.
	DEBUG
)

func ParseLevel

func ParseLevel(s string) (LogLevel, error)

ParseLevel translates some potential loglevel strings into their corresponding levels.

func (LogLevel) Char

func (l LogLevel) Char() string

Char returns a single-character representation of the log level.

func (*LogLevel) Set

func (l *LogLevel) Set(s string) error

Set the log level

func (LogLevel) String

func (l LogLevel) String() string

String returns a multi-character representation of the log level.

type Logger

type Logger interface {
	Fatal(args ...interface{})
	Fatalf(format string, args ...interface{})

	Panic(args ...interface{})
	Panicf(format string, args ...interface{})

	Info(entries ...interface{})
	Infof(format string, args ...interface{})

	Error(entries ...interface{})
	Errorf(format string, args ...interface{})

	Warning(entries ...interface{})
	Warningf(format string, args ...interface{})

	Notice(entries ...interface{})
	Noticef(format string, args ...interface{})

	Debug(entries ...interface{})
	Debugf(format string, args ...interface{})

	Trace(entries ...interface{})
	Tracef(format string, args ...interface{})

	// KV logs entries in "key1=value1, ..., keyN=valueN" format
	KV(level LogLevel, entries ...interface{})

	// WithValues adds some key-value pairs of context to a logger.
	// See Info for documentation on how key/value pairs work.
	WithValues(keysAndValues ...interface{}) Logger
}

Logger interface for generic logger

func NewNilLogger

func NewNilLogger() Logger

NewNilLogger creates new nil logger

type NilFormatter

type NilFormatter struct {
}

NilFormatter is a no-op log formatter that does nothing.

func (*NilFormatter) Flush

func (*NilFormatter) Flush()

Flush is included so that the interface is complete, but is a no-op.

func (*NilFormatter) Format

func (*NilFormatter) Format(_ string, _ LogLevel, _ int, _ ...interface{})

Format does nothing.

func (*NilFormatter) FormatKV added in v0.7.0

func (*NilFormatter) FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})

FormatKV log entry string to the stream, the entries are key/value pairs

func (*NilFormatter) WithCaller added in v0.7.0

func (c *NilFormatter) WithCaller(val bool) Formatter

WithCaller allows to configure if the caller shall be logged

type NilLogger

type NilLogger struct {
}

NilLogger does not produce any output

func (*NilLogger) Debug

func (l *NilLogger) Debug(entries ...interface{})

Debug does nothing

func (*NilLogger) Debugf

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

Debugf does nothing

func (*NilLogger) Error

func (l *NilLogger) Error(entries ...interface{})

Error does nothing

func (*NilLogger) Errorf

func (l *NilLogger) Errorf(format string, args ...interface{})

Errorf does nothing

func (*NilLogger) Fatal

func (l *NilLogger) Fatal(args ...interface{})

Fatal does nothing

func (*NilLogger) Fatalf

func (l *NilLogger) Fatalf(format string, args ...interface{})

Fatalf does nothing

func (*NilLogger) Fatalln

func (l *NilLogger) Fatalln(args ...interface{})

Fatalln does nothing

func (*NilLogger) Info

func (l *NilLogger) Info(entries ...interface{})

Info does nothing

func (*NilLogger) Infof

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

Infof does nothing

func (*NilLogger) KV added in v0.7.0

func (l *NilLogger) KV(_ LogLevel, entries ...interface{})

KV does nothing

func (*NilLogger) Notice

func (l *NilLogger) Notice(entries ...interface{})

Notice does nothing

func (*NilLogger) Noticef

func (l *NilLogger) Noticef(format string, args ...interface{})

Noticef does nothing

func (*NilLogger) Panic

func (l *NilLogger) Panic(args ...interface{})

Panic does nothing

func (*NilLogger) Panicf

func (l *NilLogger) Panicf(format string, args ...interface{})

Panicf does nothing

func (*NilLogger) Trace

func (l *NilLogger) Trace(entries ...interface{})

Trace does nothing

func (*NilLogger) Tracef

func (l *NilLogger) Tracef(format string, args ...interface{})

Tracef does nothing

func (*NilLogger) Warning

func (l *NilLogger) Warning(entries ...interface{})

Warning does nothing

func (*NilLogger) Warningf

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

Warningf does nothing

func (*NilLogger) WithValues added in v0.7.0

func (l *NilLogger) WithValues(keysAndValues ...interface{}) Logger

WithValues adds some key-value pairs of context to a logger. See Info for documentation on how key/value pairs work.

type PackageLogger

type PackageLogger struct {
	// contains filtered or unexported fields
}

PackageLogger is logger implementation for packages

func NewPackageLogger

func NewPackageLogger(repo string, pkg string) (p *PackageLogger)

NewPackageLogger creates a package logger object. This should be defined as a global var in your package, referencing your repo.

func (*PackageLogger) Debug

func (p *PackageLogger) Debug(entries ...interface{})

Debug is implementation for stdlib compatibility

func (*PackageLogger) Debugf

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

Debugf is implementation for stdlib compatibility

func (*PackageLogger) Error

func (p *PackageLogger) Error(entries ...interface{})

Error is implementation for stdlib compatibility

func (*PackageLogger) Errorf

func (p *PackageLogger) Errorf(format string, args ...interface{})

Errorf is implementation for stdlib compatibility

func (*PackageLogger) Fatal

func (p *PackageLogger) Fatal(args ...interface{})

Fatal is implementation for stdlib compatibility

func (*PackageLogger) Fatalf

func (p *PackageLogger) Fatalf(format string, args ...interface{})

Fatalf is implementation for stdlib compatibility

func (*PackageLogger) Flush

func (p *PackageLogger) Flush()

Flush the logs

func (*PackageLogger) Info

func (p *PackageLogger) Info(entries ...interface{})

Info is implementation for stdlib compatibility

func (*PackageLogger) Infof

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

Infof is implementation for stdlib compatibility

func (*PackageLogger) KV added in v0.7.0

func (p *PackageLogger) KV(l LogLevel, entries ...interface{})

KV prints key=value pairs

func (*PackageLogger) LevelAt

func (p *PackageLogger) LevelAt(l LogLevel) bool

LevelAt returns the current log level

func (*PackageLogger) Log

func (p *PackageLogger) Log(l LogLevel, args ...interface{})

Log a message at any level between ERROR and TRACE

func (*PackageLogger) Logf

func (p *PackageLogger) Logf(l LogLevel, format string, args ...interface{})

Logf a formatted string at any level between ERROR and TRACE

func (*PackageLogger) Notice

func (p *PackageLogger) Notice(entries ...interface{})

Notice is implementation for stdlib compatibility

func (*PackageLogger) Noticef

func (p *PackageLogger) Noticef(format string, args ...interface{})

Noticef is implementation for stdlib compatibility

func (*PackageLogger) Panic

func (p *PackageLogger) Panic(args ...interface{})

Panic is implementation for stdlib compatibility

func (*PackageLogger) Panicf

func (p *PackageLogger) Panicf(format string, args ...interface{})

Panicf is implementation for stdlib compatibility

func (*PackageLogger) Trace

func (p *PackageLogger) Trace(entries ...interface{})

Trace is implementation for stdlib compatibility

func (*PackageLogger) Tracef

func (p *PackageLogger) Tracef(format string, args ...interface{})

Tracef is implementation for stdlib compatibility

func (*PackageLogger) Warning

func (p *PackageLogger) Warning(entries ...interface{})

Warning is implementation for stdlib compatibility

func (*PackageLogger) Warningf

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

Warningf is implementation for stdlib compatibility

func (*PackageLogger) WithValues added in v0.7.0

func (p *PackageLogger) WithValues(keysAndValues ...interface{}) Logger

WithValues adds some key-value pairs of context to a logger. See Info for documentation on how key/value pairs work.

type PrettyFormatter

type PrettyFormatter struct {
	// contains filtered or unexported fields
}

PrettyFormatter provides default logs format

func (*PrettyFormatter) Flush

func (c *PrettyFormatter) Flush()

Flush the logs

func (*PrettyFormatter) Format

func (c *PrettyFormatter) Format(pkg string, l LogLevel, depth int, entries ...interface{})

Format log entry string to the stream

func (*PrettyFormatter) FormatKV added in v0.7.0

func (c *PrettyFormatter) FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})

FormatKV log entry string to the stream, the entries are key/value pairs

func (*PrettyFormatter) WithCaller added in v0.7.0

func (c *PrettyFormatter) WithCaller(val bool) Formatter

WithCaller allows to configure if the caller shall be logged

type RepoLogger

type RepoLogger map[string]*PackageLogger

RepoLogger specifies a map of repo => PackageLogger

func GetRepoLogger

func GetRepoLogger(repo string) (RepoLogger, error)

GetRepoLogger may return the handle to the repository's set of packages' loggers.

func MustRepoLogger

func MustRepoLogger(repo string) RepoLogger

MustRepoLogger returns the handle to the repository's packages' loggers.

func (RepoLogger) ParseLogLevelConfig

func (r RepoLogger) ParseLogLevelConfig(conf string) (map[string]LogLevel, error)

ParseLogLevelConfig parses a comma-separated string of "package=loglevel", in order, and returns a map of the results, for use in SetLogLevel.

func (RepoLogger) SetLogLevel

func (r RepoLogger) SetLogLevel(m map[string]LogLevel)

SetLogLevel takes a map of package names within a repository to their desired loglevel, and sets the levels appropriately. Unknown packages are ignored. "*" is a special package name that corresponds to all packages, and will be processed first.

func (RepoLogger) SetRepoLogLevel

func (r RepoLogger) SetRepoLogLevel(l LogLevel)

SetRepoLogLevel sets the log level for all packages in the repository.

type StringFormatter

type StringFormatter struct {
	// contains filtered or unexported fields
}

StringFormatter defines string-based formatter

func (*StringFormatter) Flush

func (s *StringFormatter) Flush()

Flush the logs

func (*StringFormatter) Format

func (s *StringFormatter) Format(pkg string, l LogLevel, depth int, entries ...interface{})

Format log entry string to the stream

func (*StringFormatter) FormatKV added in v0.7.0

func (s *StringFormatter) FormatKV(pkg string, level LogLevel, depth int, entries ...interface{})

FormatKV log entry string to the stream, the entries are key/value pairs

func (*StringFormatter) WithCaller added in v0.7.0

func (s *StringFormatter) WithCaller(val bool) Formatter

WithCaller allows to configure if the caller shall be logged

Directories

Path Synopsis
Package logrotate implements additional functionality for io writers & closers
Package logrotate implements additional functionality for io writers & closers

Jump to

Keyboard shortcuts

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