Documentation ¶
Overview ¶
Fortio's log is simple logger built on top of go's default one with additional opinionated levels similar to glog but simpler to use and configure. ``` log.Debugf() // Debug level log.LogVf() // Verbose level log.Infof() // Info/default level log.Warnf() // Warning level log.Errf() // Error level log.Critf() // Critical level (always logged even if level is set to max) log.Fatalf() // Fatal level - program will panic/exit ``` See Config object for options like whether to include line number and file name of caller or not etc
Index ¶
- Variables
- func Critf(format string, rest ...interface{})
- func Debugf(format string, rest ...interface{})
- func Errf(format string, rest ...interface{})
- func FErrf(format string, rest ...interface{}) int
- func Fatalf(format string, rest ...interface{})
- func Infof(format string, rest ...interface{})
- func Log(lvl Level) bool
- func LogDebug() bool
- func LogVerbose() bool
- func LogVf(format string, rest ...interface{})
- func Logf(lvl Level, format string, rest ...interface{})
- func Printf(format string, rest ...interface{})
- func SetDefaultsForClientTools()
- func SetFlags(f int)
- func SetLogLevelStr(str string) error
- func SetOutput(w io.Writer)
- func Warnf(format string, rest ...interface{})
- type Level
- type LogConfig
- type LoggerI
Constants ¶
This section is empty.
Variables ¶
var ( Config = DefaultConfig() // Used for dynamic flag setting as strings and validation. LevelToStrA []string )
Functions ¶
func FErrf ¶
FErrF logs a fatal error and returns 1. meant for cli main functions written like: func main() { os.Exit(Main()) } and if err != nil { return log.FErrf("error: %v", err) } so they can be tested with testscript. See https://github.com/fortio/delta/ for example.
func Fatalf ¶
func Fatalf(format string, rest ...interface{})
Fatalf logs if Warning level is on and panics or exits.
func Logf ¶
Logf logs with format at the given level. 2 level of calls so it's always same depth for extracting caller file/line. Note that log.Logf(Fatal, "...") will not panic or exit, only log.Fatalf() does.
func Printf ¶
func Printf(format string, rest ...interface{})
Printf forwards to the underlying go logger to print (with only timestamp prefixing).
func SetDefaultsForClientTools ¶
func SetDefaultsForClientTools()
SetDefaultsForClientTools changes the default value of LogPrefix and LogFileAndLine to make output without caller and prefix, a default more suitable for command line tools (like dnsping). Needs to be called before flag.Parse(). Caller could also use log.Printf instead of changing this if not wanting to use levels. Also makes log.Fatalf just exit instead of panic.
func SetLogLevelStr ¶
Sets level from string (called by dflags).
_ = dflag.DynString(flag.CommandLine, "loglevel", GetLogLevel().String(), fmt.Sprintf("loglevel, one of %v", LevelToStrA)).WithInputMutator( func(inp string) string { // The validation map has full lowercase and capitalized first letter version return strings.ToLower(strings.TrimSpace(inp)) }).WithValidator( func(newStr string) error { _, err := ValidateLevel(newStr) return err }).WithSyncNotifier( func(old, newStr string) { _ = SetLogLevelStr(newStr) // will succeed as we just validated it first })
Types ¶
type Level ¶
type Level int32
Level is the level of logging (0 Debug -> 6 Fatal).
Log levels. Go can't have variable and function of the same name so we keep medium length (Dbg,Info,Warn,Err,Crit,Fatal) names for the functions.
func SetLogLevel ¶
SetLogLevel sets the log level and returns the previous one.
func SetLogLevelQuiet ¶
SetLogLevelQuiet sets the log level and returns the previous one but does not log the change of level itself.
func ValidateLevel ¶
ValidateLevel returns error if the level string is not valid.
type LogConfig ¶
type LogConfig struct { LogPrefix string // "Prefix to log lines before logged messages LogFileAndLine bool // Logs filename and line number of callers to log. FatalPanics bool // If true, log.Fatalf will panic (stack trace) instead of just exit 1 FatalExit func(int) // Function to call upon log.Fatalf. e.g. os.Exit. }
func DefaultConfig ¶
func DefaultConfig() *LogConfig