Documentation
¶
Overview ¶
Package ezlog provides simple leveled log output using stdlib's log.Logger. A type Logger is defined with methods for leveled log line and formatted leveled log line output. For convenience, a 'standard' logger is available through the helper functions. The 'standard' logger's log level is LogError and writes output to stderr with LstdFlags.
In addition to LogNone, which discards all log lines, three common log levels are supported: error (LogError), info (LogInfo), and debug (LogDebug). Any log lines that are for log levels higher than the logger's log level are discarded.
Leveled log lines are written with the Error[f|ln], Info[f|ln], Debug[f|ln] methods. Aside from the leveled log lines, two other types of prefixed log lines can be written: Fatal[f|ln] and Panic[f|ln]. Log lines w/o levels can be written with the Print[f|ln] methods. These methods will always result in the log lines being written.
On Fatal and Panic calls, Logger can run functions prior to os.Exit or panic. These functions are added using the AddFunc call and are expected to have a func() error signature. Any errors that may occur during the execution of these functions will be ignored.
These functions can also be run by calling the Close method.
Index ¶
- Constants
- func AddFunc(f func() error)
- func Close()
- func Debug(v ...interface{})
- func Debugf(format string, v ...interface{})
- func Debugln(v ...interface{})
- func Error(v ...interface{})
- func Errorf(format string, v ...interface{})
- func Errorln(v ...interface{})
- func Fatal(v ...interface{})
- func Fatalf(format string, v ...interface{})
- func Fatalln(v ...interface{})
- func Flags() int
- func Info(v ...interface{})
- func Infof(format string, v ...interface{})
- func Infoln(v ...interface{})
- func Panic(v ...interface{})
- func Panicf(format string, v ...interface{})
- func Panicln(v ...interface{})
- func ParseFlag(s string) (l int, err error)
- func Prefix() string
- func Print(v ...interface{})
- func Printf(format string, v ...interface{})
- func Println(v ...interface{})
- func SetFlags(flags int)
- func SetLevel(i Level)
- func SetLevelStringType(v LevelStringType)
- func SetOutput(w io.Writer)
- func SetPrefix(prefix string)
- type Level
- type LevelStringType
- type Logger
- func (l *Logger) AddFunc(f func() error)
- func (l *Logger) Close()
- func (l *Logger) Debug(v ...interface{})
- func (l *Logger) Debugf(format string, v ...interface{})
- func (l *Logger) Debugln(v ...interface{})
- func (l *Logger) Error(v ...interface{})
- func (l *Logger) Errorf(format string, v ...interface{})
- func (l *Logger) Errorln(v ...interface{})
- func (l *Logger) Fatal(v ...interface{})
- func (l *Logger) Fatalf(format string, v ...interface{})
- func (l *Logger) Fatalln(v ...interface{})
- func (l *Logger) Flags() int
- func (l *Logger) GetLevel() Level
- func (l *Logger) GetLevelStringType() LevelStringType
- func (l *Logger) Info(v ...interface{})
- func (l *Logger) Infof(format string, v ...interface{})
- func (l *Logger) Infoln(v ...interface{})
- func (l *Logger) Panic(v ...interface{})
- func (l *Logger) Panicf(format string, v ...interface{})
- func (l *Logger) Panicln(v ...interface{})
- func (l *Logger) Prefix() string
- func (l *Logger) Print(v ...interface{})
- func (l *Logger) Printf(format string, v ...interface{})
- func (l *Logger) Println(v ...interface{})
- func (l *Logger) SetFlags(flags int)
- func (l *Logger) SetLevel(i Level)
- func (l *Logger) SetLevelStringType(v LevelStringType)
- func (l *Logger) SetOutput(w io.Writer)
- func (l *Logger) SetPrefix(prefix string)
- type UnknownFlagError
Constants ¶
const ( // Bits or'ed together to control what's printed. // There is no control over the order they appear (the order listed // here) or the format they present (as described in the comments). // The prefix is followed by a colon only when Llongfile or Lshortfile // is specified. // For example, flags Ldate | Ltime (or LstdFlags) produce, // 2009/01/23 01:23:23 message // while flags Ldate | Ltime | Lmicroseconds | Llongfile produce, // 2009/01/23 01:23:23.123123 /a/b/c/d.go:23: message Ldate = 1 << iota // the date in the local time zone: 2009/01/23 Ltime // the time in the local time zone: 01:23:23 Lmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. Llongfile // full file name and line number: /a/b/c/d.go:23 Lshortfile // final file name element and line number: d.go:23. overrides Llongfile LUTC // if Ldate or Ltime is set, use UTC rather than the local time zone LstdFlags = Ldate | Ltime // initial values for the standard logger )
These flags define which text to prefix to each log entry generated by the Logger.
Variables ¶
This section is empty.
Functions ¶
func AddFunc ¶
func AddFunc(f func() error)
AddFunc adds a func to the standard logger that is to be run by the Close, Fatal, and Panic methods. Funcs will be run in the order they are added.
func Close ¶
func Close()
Close runs any funcs that standard logger was given. Any errors that occurs during the execution of these funcs are ignored as this is expected to occur immediately before the application exits.
func Debug ¶
func Debug(v ...interface{})
Debug writes a debug line to the standard logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func Debugf ¶
func Debugf(format string, v ...interface{})
Debugf writes a debug line to the standard logger using the provided format and data. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func Debugln ¶
func Debugln(v ...interface{})
Debugln writes a debug line to the stadard logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func Error ¶
func Error(v ...interface{})
Error writes an error entry to the standard logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func Errorf ¶
func Errorf(format string, v ...interface{})
Errorf writes an error line to the standard logger using the provided format and data. If the level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func Errorln ¶
func Errorln(v ...interface{})
Errorln writes an error line to the standard logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func Fatal ¶
func Fatal(v ...interface{})
Fatal writes a fatal line to the standard logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Print.
func Fatalf ¶
func Fatalf(format string, v ...interface{})
Fatalf writes a fatal line to the standard logger using the provided format and data followed by a call to os.Exit(1).
func Fatalln ¶
func Fatalln(v ...interface{})
Fatalln writes a fatal line to the standard logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Println.
func Info ¶
func Info(v ...interface{})
Info writes an info line to the standard logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func Infof ¶
func Infof(format string, v ...interface{})
Infof writes an info line to the standard logger using the provided format and data. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func Infoln ¶
func Infoln(v ...interface{})
Infoln writes an info line to the standard logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func Panic ¶
func Panic(v ...interface{})
Panic writes a panic line to the standard logger followed by a call to panic(). Arguments are handled in the manner of fmt.Print.
func Panicf ¶
func Panicf(format string, v ...interface{})
Panicf writes a panic line to the standard logger using the provided format and data followed by a call to panic(). Arguments are handled in the manner of fmt.Printf.
func Panicln ¶
func Panicln(v ...interface{})
Panicln writes a panic line to the standard logger followed by a call to panic(). Arguments are handled in the manner of fmt.Println.
func ParseFlag ¶
ParseFlag returns the log Flag for s. A match is found when s is equal to either the name of a Flag constant or the name of a Flag constant without the leading 'l', e.g. both "lstdflags" and "stdflags" will return the value for the LstdFlags constant. The match is case-insensitve. Empty string is treated as wanting the default LstdFlags; returning LstdFlags. An UnknownFlagError is returned if no match for s is found.
func Print ¶
func Print(v ...interface{})
Print writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Print.
func Printf ¶
func Printf(format string, v ...interface{})
Printf writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Printf.
func Println ¶
func Println(v ...interface{})
Println writes a log line to the standard logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Println.
func SetLevel ¶
func SetLevel(i Level)
SetLevel sets the maximum level for the standard logger's log output. Any log lines whose levels are higher than the standard logger's level will be discarded.
func SetLevelStringType ¶
func SetLevelStringType(v LevelStringType)
SetLevelStringType sets what the standard logger should use for the level name (string) in log lines.
Types ¶
type Level ¶
type Level int32 // for sync.Atomic.Int32
Level: log levels.
func LevelByName ¶
LevelByName gets the Level corresponding to s. A false will be returned if s doesn't match any Levels. S is uppper-cased prior to evaluation.
Supported values:
LogNone: none, n, empty string ("")
LogError: error, e, error
LogInfo: info, i, inf
LogDebug: debug, d, dbg
type LevelStringType ¶
type LevelStringType int
LevelStringType is the type of string output that will be used for the log level.
const ( Full LevelStringType = iota // use the level's full name Short // use the level's short description Char // use the first character of the level's name )
func GetLevelStringType ¶
func GetLevelStringType() LevelStringType
GetLevelStringType returns what the standard logger is using for the error level name (string) in log lines.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger generates leveled log lines of output to an io.Writer if the log level is <= the logger's level. This is safe for concurrent use.
func New ¶
func New(level Level, levelStringType LevelStringType, out io.Writer, prefix string, flag int) *Logger
New creates a new Logger. The level argument sets the Logger's log level. The levelStringType is what should be used for the level's name: the first character, Char, the short version, Short, or the full name, Full. The out argument sets the log data output destination. The prefix argument sets what each line will start with. The flag argument sets the logger's properties.
func (*Logger) AddFunc ¶
AddFunc adds a func to the logger that is to be run by the Close, Fatal, and Panic methods. Funcs will be run in the order they are added.
func (*Logger) Close ¶
func (l *Logger) Close()
Close runs any funcs that the logger was given. Any errors that occurs during the execution of these funcs are ignored as this is expected to occur immediately before the application exits.
func (*Logger) Debug ¶
func (l *Logger) Debug(v ...interface{})
Debug writes a debug line to the logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func (*Logger) Debugf ¶
Debugf writes a debug line to the logger using the provided format and data. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Debugln ¶
func (l *Logger) Debugln(v ...interface{})
Debugln writes a debug line to the logger. If the level is less than LogDebug, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func (*Logger) Error ¶
func (l *Logger) Error(v ...interface{})
Error writes an error line to the logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func (*Logger) Errorf ¶
Errorf writes an error line to the logger using the provided format and data. If the level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Errorln ¶
func (l *Logger) Errorln(v ...interface{})
Errorln writes an error line to the logger. If the logger's level is less than LogError, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func (*Logger) Fatal ¶
func (l *Logger) Fatal(v ...interface{})
Fatal writes a fatal line to the logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Print.
func (*Logger) Fatalf ¶
Fatalf writes a fatal line to the logger using the provided format and data followed by a call to os.Exit(1).
func (*Logger) Fatalln ¶
func (l *Logger) Fatalln(v ...interface{})
Fatalln writes a fatal line to the logger followed by a call to os.Exit(1). Arguments are handled in the manner of fmt.Println.
func (*Logger) GetLevelStringType ¶
func (l *Logger) GetLevelStringType() LevelStringType
GetLevelStringType returns what the logger is using for the error level name (string) in log lines.
func (*Logger) Info ¶
func (l *Logger) Info(v ...interface{})
Info writes an info entry to the logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Print.
func (*Logger) Infof ¶
Infof writes an info line to the logger using the provided format and data. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Infoln ¶
func (l *Logger) Infoln(v ...interface{})
Infoln writes an info entry to the logger. If the level is less than LogInfo, the line will be discarded. Arguments are handled in the manner of fmt.Println.
func (*Logger) Panic ¶
func (l *Logger) Panic(v ...interface{})
Panic writes a panic line to the logger followed by a call to panic(). Arguments are handled in the manner of fmt.Print.
func (*Logger) Panicf ¶
Panicf writes a panic line to the logger using the provided format and data followed by a call to panic().
func (*Logger) Panicln ¶
func (l *Logger) Panicln(v ...interface{})
Panicln writes a panic line to the logger followed by a call to panic(). Arguments are handled in the manner of fmt.Println.
func (*Logger) Print ¶
func (l *Logger) Print(v ...interface{})
Print writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Print.
func (*Logger) Printf ¶
Printf writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Printf.
func (*Logger) Println ¶
func (l *Logger) Println(v ...interface{})
Println writes a log line to the logger. Unless the logger's level is LogNone, the line will always be written. Arguments are handled in the manner of fmt.Println.
func (*Logger) SetLevel ¶
SetLevel sets the maximum level for the logger's output. Any log lines whose levels are higher than the logger's level will be discarded.
func (*Logger) SetLevelStringType ¶
func (l *Logger) SetLevelStringType(v LevelStringType)
SetLevelStringType sets what the logger should use for the level name (string) in log lines.
type UnknownFlagError ¶
type UnknownFlagError struct {
S string // The string that could not be parsed to a valid Flag.
}
UnknownFlagError occurs when a string cannot be parsed into a log Flag.
func (UnknownFlagError) Error ¶
func (e UnknownFlagError) Error() string
