capnslog

package
v2.1.3+incompatible Latest Latest
Warning

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

Go to latest
Published: Mar 16, 2019 License: Apache-2.0 Imports: 13 Imported by: 0

README

capnslog, the CoreOS logging package

There are far too many logging packages out there, with varying degrees of licenses, far too many features (colorization, all sorts of log frameworks) or are just a pain to use (lack of Fatalln()?). capnslog provides a simple but consistent logging interface suitable for all kinds of projects.

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 dependent. 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.
  • Debug: Everything is still fine, but even common operations may be logged, and less helpful but more quantity of notices.
  • Trace: Anything goes, from logging every function call as part of a common operation, to tracing execution of a query.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GlogHeader

func GlogHeader(level LogLevel, depth int) []byte

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 capnslog.

Types

type Formatter

type Formatter interface {
	Format(pkg string, level LogLevel, depth int, entries ...interface{})
	Flush()
}

func NewDefaultFormatter

func NewDefaultFormatter(out io.Writer) Formatter

func NewDefaultSyslogFormatter

func NewDefaultSyslogFormatter(tag string) (Formatter, error)

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

func NewStringFormatter

func NewStringFormatter(w io.Writer) Formatter

func NewSyslogFormatter

func NewSyslogFormatter(w *syslog.Writer) Formatter

type GlogFormatter

type GlogFormatter struct {
	StringFormatter
}

func NewGlogFormatter

func NewGlogFormatter(w io.Writer) *GlogFormatter

func (GlogFormatter) Format

func (g GlogFormatter) Format(pkg string, level LogLevel, depth int, entries ...interface{})

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.

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
	// DEBUG is the default hidden level for more verbose updates about internal processes.
	DEBUG
	// TRACE is for (potentially) call by call tracing of programs.
	TRACE
)

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

Update using the given string value. Fulfills the flag.Value interface.

func (LogLevel) String

func (l LogLevel) String() string

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

func (*LogLevel) Type

func (l *LogLevel) Type() string

Returns an empty string, only here to fulfill the pflag.Value interface.

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.

type PackageLogger

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

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{})

func (*PackageLogger) Debugf

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

func (*PackageLogger) Error

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

func (*PackageLogger) Errorf

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

func (*PackageLogger) Fatal

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

func (*PackageLogger) Fatalf

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

func (*PackageLogger) Fatalln

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

func (*PackageLogger) Flush

func (p *PackageLogger) Flush()

func (*PackageLogger) Info

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

func (*PackageLogger) Infof

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

func (*PackageLogger) LevelAt

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

LevelAt checks if the given log level will be outputted under current setting.

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{})

Log a formatted string at any level between ERROR and TRACE

func (*PackageLogger) Notice

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

func (*PackageLogger) Noticef

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

func (*PackageLogger) Panic

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

func (*PackageLogger) Panicf

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

func (*PackageLogger) Panicln

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

func (*PackageLogger) Print

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

func (*PackageLogger) Printf

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

func (*PackageLogger) Println

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

func (*PackageLogger) SetLevel

func (p *PackageLogger) SetLevel(l LogLevel)

SetLevel allows users to change the current logging level.

func (*PackageLogger) Trace

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

func (*PackageLogger) Tracef

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

func (*PackageLogger) Warning

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

func (*PackageLogger) Warningf

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

type PrettyFormatter

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

func (*PrettyFormatter) Flush

func (c *PrettyFormatter) Flush()

func (*PrettyFormatter) Format

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

type RepoLogger

type RepoLogger map[string]*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
}

func (*StringFormatter) Flush

func (s *StringFormatter) Flush()

func (*StringFormatter) Format

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

Jump to

Keyboard shortcuts

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