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.
Copyright 2018, Denis Issoupov ¶
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 ¶
- Variables
- func SetFormatter(f Formatter)
- func SetGlobalLogLevel(l LogLevel)
- func SetPackageLogLevel(repo, pkg string, l LogLevel)
- func SetRepoLogLevel(repo string, l LogLevel)
- type ColorFormatter
- type Formatter
- func GetFormatter() Formatter
- func NewColorFormatter(w io.Writer, color bool) Formatter
- func NewDefaultFormatter(out io.Writer) Formatter
- func NewLogFormatter(w io.Writer, prefix string, flag int) Formatter
- func NewNilFormatter() Formatter
- func NewPrettyFormatter(w io.Writer, debug bool) Formatter
- func NewStringFormatter(w io.Writer) Formatter
- type LogFormatter
- type LogLevel
- type Logger
- type NilFormatter
- type NilLogger
- func (l *NilLogger) Debug(entries ...interface{})
- func (l *NilLogger) Debugf(format string, args ...interface{})
- func (l *NilLogger) Error(entries ...interface{})
- func (l *NilLogger) Errorf(format string, args ...interface{})
- func (l *NilLogger) Fatal(args ...interface{})
- func (l *NilLogger) Fatalf(format string, args ...interface{})
- func (l *NilLogger) Fatalln(args ...interface{})
- func (l *NilLogger) Info(entries ...interface{})
- func (l *NilLogger) Infof(format string, args ...interface{})
- func (l *NilLogger) KV(_ LogLevel, entries ...interface{})
- func (l *NilLogger) Notice(entries ...interface{})
- func (l *NilLogger) Noticef(format string, args ...interface{})
- func (l *NilLogger) Panic(args ...interface{})
- func (l *NilLogger) Panicf(format string, args ...interface{})
- func (l *NilLogger) Trace(entries ...interface{})
- func (l *NilLogger) Tracef(format string, args ...interface{})
- func (l *NilLogger) Warning(entries ...interface{})
- func (l *NilLogger) Warningf(format string, args ...interface{})
- func (l *NilLogger) WithValues(keysAndValues ...interface{}) Logger
- type PackageLogger
- func (p *PackageLogger) Debug(entries ...interface{})
- func (p *PackageLogger) Debugf(format string, args ...interface{})
- func (p *PackageLogger) Error(entries ...interface{})
- func (p *PackageLogger) Errorf(format string, args ...interface{})
- func (p *PackageLogger) Fatal(args ...interface{})
- func (p *PackageLogger) Fatalf(format string, args ...interface{})
- func (p *PackageLogger) Flush()
- func (p *PackageLogger) Info(entries ...interface{})
- func (p *PackageLogger) Infof(format string, args ...interface{})
- func (p *PackageLogger) KV(l LogLevel, entries ...interface{})
- func (p *PackageLogger) LevelAt(l LogLevel) bool
- func (p *PackageLogger) Log(l LogLevel, args ...interface{})
- func (p *PackageLogger) Logf(l LogLevel, format string, args ...interface{})
- func (p *PackageLogger) Notice(entries ...interface{})
- func (p *PackageLogger) Noticef(format string, args ...interface{})
- func (p *PackageLogger) Panic(args ...interface{})
- func (p *PackageLogger) Panicf(format string, args ...interface{})
- func (p *PackageLogger) Trace(entries ...interface{})
- func (p *PackageLogger) Tracef(format string, args ...interface{})
- func (p *PackageLogger) Warning(entries ...interface{})
- func (p *PackageLogger) Warningf(format string, args ...interface{})
- func (p *PackageLogger) WithValues(keysAndValues ...interface{}) Logger
- type PrettyFormatter
- type RepoLogger
- type StringFormatter
Constants ¶
This section is empty.
Variables ¶
var (
ColorOff = []byte("\033[0m")
)
color pallete map
var LevelColors = map[LogLevel][]byte{
CRITICAL: colorLightRed,
ERROR: colorLightRed,
WARNING: colorLightOrange,
NOTICE: colorLightGreen,
INFO: colorLightCyan,
DEBUG: colorGray,
TRACE: colorGray,
}
LevelColors provides colors map
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 ¶
SetPackageLogLevel sets the log level for a package in repo logger
func SetRepoLogLevel ¶
SetRepoLogLevel sets the log level for all packages in repo logger
Types ¶
type ColorFormatter ¶
type ColorFormatter struct {
// contains filtered or unexported fields
}
ColorFormatter provides colorful logs format
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 NewColorFormatter ¶
NewColorFormatter returns an instance of ColorFormatter
func NewDefaultFormatter ¶
NewDefaultFormatter returns an instance of default formatter
func NewLogFormatter ¶
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 ¶
NewPrettyFormatter returns an instance of PrettyFormatter
func NewStringFormatter ¶
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 ¶
ParseLevel translates some potential loglevel strings into their corresponding levels.
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
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) Warning ¶
func (l *NilLogger) Warning(entries ...interface{})
Warning does nothing
func (*NilLogger) WithValues ¶ added in v0.7.0
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) 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) 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) 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