Documentation
¶
Overview ¶
Package llog implements level-restricted logging. It is similar and based to standard log package but provides additional flexibility in management of logging messages.
Package provides 5 levels of logging:
DEBUG - output all messages (lowest level) INFO - output all messages except debug messages WARNING - output all messages except debug and info messages - it is default level ERROR - output only error and critical messages CRITICAL - output only critical messages (highest level)
There is a standard logger that initialized with os.Stderr as output, "" as prefix, log.LstdFlags (see https://golang.org/pkg/log/#pkg-constants for details) as flags, and WARNING as logging level.
Logging level of standard logger can be changed via SetLevel(level int). Use constants DEBUG, INFO, WARNING, ERROR or CRITICAL for setting the level value. You can also change output: SetOutput(w io.Writer), log message prefix: SetPrefix(prefix string), and message format flags: SetFlags(flag int) (see https://golang.org/pkg/log/#pkg-constants for details about flags).
To create the log message (via default logger) of the required logging level you have to use one of the following functions:
- Arguments are handled in the manner of fmt.Println Debug(v ...interface{}) // equal to log.Println() when logging level is DEBUG Info(v ...interface{}) // equal to log.Println() when logging level is INFO or less Warning(v ...interface{}) // equal to log.Println() when logging level is WARNING or less Error(v ...interface{}) // equal to log.Println() when logging level is ERROR or less Critical(v ...interface{}) // equal to log.Panicln() in any logging level
- Arguments are handled in the manner of fmt.Printf Debugf(format string, v ...interface{}) // equal to log.Printf() when logging level is DEBUG Infof(format string, v ...interface{}) // equal to log.Printf() when logging level is INFO or less Warningf(format string, v ...interface{}) // equal to log.Printf() when logging level is WARNING or less Errorf(format string, v ...interface{}) // equal to log.Printf() when logging level is ERROR or less Criticalf(format string, v ...interface{}) // equal to log.Panicf() in any logging level
Output of the functions will be additionally tagged with one letter identifier of logging level of message: D(EBUG), I(NFO), W(ARNING), E(RROR) or C(RITICAL). For example
Warning("Some message") // Output: W: Some message
When the current logging level is greater than level of created message then function do nothing.
New() allows to create the new Logger that has the same methods as standard logger.
NOTE: as llog.Logger is just extension of log.Logger the full set of log package methods/functions/constants are available to use together with llog methods. But standard logger has only llog declared methods.
Example ¶
l := New(os.Stdout, "", 0, -1) // set default logging level (WARNING)
l.Println("It is log.Println message") // original log method: printed as it is not filtered
l.Print("WARNING level is set.")
l.Debug("It is DEBUG message") // not printed as DEBUG < WARNING
l.Debugf("%s", "It is formatted DEBUG message") // not printed as DEBUG < WARNING
l.Info("It is INFO message") // not printed as INFO < WARNING
l.Infof("%s", "It is formatted INFO message") // not printed as INFO < WARNING
l.Warning("It is WARNING message")
l.Warningf("%s", "It is formatted WARNING message")
l.Error("It is ERROR message")
l.Errorf("%s", "It is formatted ERROR message")
l.SetLevel(DEBUG) // change level to INFO
l.Print("DEBUG level is set.")
l.Debug("It is DEBUG message") // printed
l.Debugf("%s", "It is formatted DEBUG message") // printed
l.Info("It is INFO message") // printed
l.Infof("%s", "It is formatted INFO message") // printed
l.Warning("It is WARNING message")
l.Warningf("%s", "It is formatted WARNING message")
l.Error("It is ERROR message")
l.Errorf("%s", "It is formatted ERROR message")
Output: It is log.Println message WARNING level is set. W: It is WARNING message W: It is formatted WARNING message E: It is ERROR message E: It is formatted ERROR message DEBUG level is set. D: It is DEBUG message D: It is formatted DEBUG message I: It is INFO message I: It is formatted INFO message W: It is WARNING message W: It is formatted WARNING message E: It is ERROR message E: It is formatted ERROR message
Index ¶
- Constants
- func Critical(v ...interface{})
- func Criticalf(f string, v ...interface{})
- func Debug(v ...interface{})
- func Debugf(f string, v ...interface{})
- func Error(v ...interface{})
- func Errorf(f string, v ...interface{})
- func Info(v ...interface{})
- func Infof(f string, v ...interface{})
- func SetFlags(flag int)
- func SetLevel(level int)
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- func Warning(v ...interface{})
- func Warningf(f string, v ...interface{})
- type Logger
- func (l *Logger) Critical(v ...interface{})
- func (l *Logger) Criticalf(f string, v ...interface{})
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(f string, v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(f string, v ...interface{})
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(f string, v ...interface{})
- func (l *Logger) SetLevel(level int)
- func (l *Logger) Warning(v ...interface{})
- func (l *Logger) Warningf(f string, v ...interface{})
Examples ¶
Constants ¶
const ( DEBUG int // 10 INFO // 20 WARNING // 30 default ERROR // 40 CRITICAL // 50 )
Logging levels
Variables ¶
This section is empty.
Functions ¶
func Critical ¶
func Critical(v ...interface{})
Critical always prints CRITICAL level message to output and then call panic() Critical is equal to log.Panicln()
Example ¶
l := New(os.Stdout, "", 0, -1) // set default logging level (WARNING)
defer func() {
_ = recover() // need recover from panic generated by Critical()
}()
l.Critical("It is CRITICAL message") // printed in any logging level
Output: C: It is CRITICAL message
func Criticalf ¶
func Criticalf(f string, v ...interface{})
Criticalf always prints CRITICAL level message to output and then call panic() Criticalf is equal to log.Panicf()
Example ¶
l := New(os.Stdout, "", 0, -1) // set default logging level (WARNING)
defer func() {
_ = recover() // need recover from panic generated by Critical()
}()
l.Criticalf("%s", "It is formatted CRITICAL message") // printed in any logging level
Output: C: It is formatted CRITICAL message
func Debug ¶
func Debug(v ...interface{})
Debug prints DEBUG level message to output if current logging level is DEBUG Debug is equal to log.Println()
Example ¶
SetOutput(os.Stdout) testset(DEBUG)
Output: llog_test.go:18: D: It is DEBUG llog_test.go:19: D: It-is-DEBUGf llog_test.go:20: I: It is INFO llog_test.go:21: I: It-is-INFOf llog_test.go:22: W: It is WARNING llog_test.go:23: W: It-is-WARNINGf llog_test.go:24: E: It is ERROR llog_test.go:25: E: It-is-ERRORf llog_test.go:30: C: It is CRITICAL llog_test.go:13: C: It-is-CRITICALf
func Debugf ¶
func Debugf(f string, v ...interface{})
Debugf prints DEBUG level message to output if current logging level is DEBUG Debugf is equal to log.Printf()
func Error ¶
func Error(v ...interface{})
Error prints ERROR level message to output if current logging level is ERROR or less Error is equal to log.Println()
Example ¶
SetOutput(os.Stdout) testset(ERROR)
Output: llog_test.go:24: E: It is ERROR llog_test.go:25: E: It-is-ERRORf llog_test.go:30: C: It is CRITICAL llog_test.go:13: C: It-is-CRITICALf
func Errorf ¶
func Errorf(f string, v ...interface{})
Errorf prints ERROR level message to output if current logging level is ERROR or less Errorf is equal to log.Printf()
func Info ¶
func Info(v ...interface{})
Info prints INFO level message to output if current logging level is INFO or less Info is equal to log.Println()
Example ¶
SetOutput(os.Stdout) testset(INFO)
Output: llog_test.go:20: I: It is INFO llog_test.go:21: I: It-is-INFOf llog_test.go:22: W: It is WARNING llog_test.go:23: W: It-is-WARNINGf llog_test.go:24: E: It is ERROR llog_test.go:25: E: It-is-ERRORf llog_test.go:30: C: It is CRITICAL llog_test.go:13: C: It-is-CRITICALf
func Infof ¶
func Infof(f string, v ...interface{})
Infof prints INFO level message to output if current logging level is INFO or less Infof is equal to log.Printf()
func SetFlags ¶
func SetFlags(flag int)
SetFlags sets messages flags for default logger. The same method is applicable to Logger.
func SetLevel ¶
func SetLevel(level int)
SetLevel sets the current logging level for default logger. Call of SetLevel is optional, if logging level is not set then logging level is WARNING.
func SetPrefix ¶
func SetPrefix(prefix string)
SetPrefix sets the messages prefix for default logger. The same method is applicable to Logger.
func Warning ¶
func Warning(v ...interface{})
Warning prints WARNING level message to output if current logging level is WARNING or less Warning is equal to log.Println()
Example ¶
SetOutput(os.Stdout) testset(WARNING)
Output: llog_test.go:22: W: It is WARNING llog_test.go:23: W: It-is-WARNINGf llog_test.go:24: E: It is ERROR llog_test.go:25: E: It-is-ERRORf llog_test.go:30: C: It is CRITICAL llog_test.go:13: C: It-is-CRITICALf
Types ¶
type Logger ¶
type Logger struct {
log.Logger // extend the Logger of standard log package
// contains filtered or unexported fields
}
Logger structure
func New ¶
New returns new Logger. Parameters out, prefix and flag are the same parameters as in log.New() from log package
out - the io.Writer used as output for messages (usually it is os.Stderr) prefix - log messages prefix flags - time&info flags (see https://golang.org/pkg/log/#pkg-constants for details) level - The logging level or -1 to set default level of logging (WARNING)
It's better to use constants DEBUG, INFO, WARNING, ERROR or CRITICAL for setting the level instead of their numeric values.
func (*Logger) Critical ¶
func (l *Logger) Critical(v ...interface{})
Critical always prints CRITICAL level message to output and then call panic() Critical is equal to log.Panicln()
func (*Logger) Criticalf ¶
Criticalf always prints CRITICAL level message to output and then call panic() Criticalf is equal to log.Panicf()
func (*Logger) Debug ¶
func (l *Logger) Debug(v ...interface{})
Debug prints debug level message to output if current logging level is DEBUG. Debug is equal to log.Println()
func (*Logger) Debugf ¶
Debugf prints DEBUG level message to output if current logging level is DEBUG. Debugf is equal to log.Printf()
func (*Logger) Error ¶
func (l *Logger) Error(v ...interface{})
Error prints ERROR level message to output if current logging level is ERROR or less Error is equal to log.Println()
func (*Logger) Errorf ¶
Errorf prints ERROR level message to output if current logging level is ERROR or less Errorf is equal to log.Printf()
func (*Logger) Info ¶
func (l *Logger) Info(v ...interface{})
Info prints INFO level message to output if current logging level is INFO or less. Info is equal to log.Println()
func (*Logger) Infof ¶
Infof prints INFO level message to output if current logging level is INFO or less. Infof is equal to log.Printf()
func (*Logger) SetLevel ¶
SetLevel sets logging level. Call of SetLevel is optional, if logging level is not set then logging level is WARNING.