Documentation
¶
Overview ¶
Package lopher is inspired by Dave Cheney's blog post on logging: https://dave.cheney.net/2015/11/05/lets-talk-about-logging
lopher only supports two logging levels:
1. Things that developers care about when they are developing or debugging software. (DEBUG) 2. Things that users care about when using your software. (INFO)
If you need to log errors, use `Info`. If you need to log warnings, use `Debug“ or don't log them (most likely no one will care). If you need log.Fatal, bubble the error up to `main.main` and gracefully exit there; it makes the most sense and is cleaner than the alternatives.
Example ¶
l := New(os.Stdout, false, "App: ", LFNone)
started := time.Now()
l.Info("Started.")
l.Debug("Taking time measurement...")
// Setting debug enables the debug level
l.SetDebug(true)
time.Sleep(time.Second)
s := math.Floor(time.Since(started).Seconds())
l.Debugf("ran for %v seconds.", s)
l.Info("Exiting.")
Output: [INFO] App: Started. [DEBUG] App: ran for 1 seconds. [INFO] App: Exiting.
Example (Package) ¶
SetFlags(LFNone)
SetOutput(os.Stdout)
started := time.Now()
Info("App Started.")
Debug("Taking time measurement...")
// Setting debug enables the debug level
SetDebug(true)
time.Sleep(time.Second)
s := math.Floor(time.Since(started).Seconds())
Debugf("App ran for %.3f minutes.", time.Since(started).Minutes())
Infof("App Exiting after %v second.", s)
Output: [INFO] App Started. [DEBUG] App ran for 0.017 minutes. [INFO] App Exiting after 1 second.
Index ¶
- Constants
- func Debug(v ...interface{})
- func Debugf(fmt string, v ...interface{})
- func Info(v ...interface{})
- func Infof(fmt string, v ...interface{})
- func SetDebug(debug bool)
- func SetFlags(f LogFlags)
- func SetOutput(w io.Writer)
- type Log
- func (l *Log) Debug(v ...interface{})
- func (l *Log) Debugf(fmtStr string, v ...interface{})
- func (l *Log) Info(v ...interface{})
- func (l *Log) Infof(fmtStr string, v ...interface{})
- func (l *Log) SetDebug(v bool)
- func (l *Log) SetFlags(f LogFlags)
- func (l *Log) SetOutput(w io.Writer)
- func (l *Log) SetPrefix(prefixStr string)
- type LogFlags
- type Logger
Examples ¶
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 LFdate = 1 << iota // the date in the local time zone: 2009/01/23 LFtime // the time in the local time zone: 01:23:23 LFmicroseconds // microsecond resolution: 01:23:23.123123. assumes Ltime. LFlongfile // full file name and line number: /a/b/c/d.go:23 LFshortfile // final file name element and line number: d.go:23. overrides Llongfile LFUTC // if Ldate or Ltime is set, use UTC rather than the local time zone LFstdFlags = LFdate | LFtime | LFUTC | LFlongfile // initial values for the standard logger LFNone = 0 )
These flags define which text to prefix to each log entry generated by the Logger.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Log ¶
type Log struct {
sync.Mutex
// Output is the writer to send logs to
io.Writer
// Debug when true will enable the DEBUG logging level
DebugMode bool
// Flags are a set of flags to use for setting up log time stamps
Flags LogFlags
// Prefix is the prefix string that is prepended before each log line
Prefix string
}
Log is a simple implementation of Logger
type LogFlags ¶
type LogFlags int
LogFlags are a set of flags which define the prefix for each log entry
type Logger ¶
type Logger interface {
// Info logs at info level
Info(...interface{})
// Infof logs a formatted string at info level
Infof(string, ...interface{})
// Debug logs at debug level
Debug(...interface{})
// Debugf logs a formatted string at debug level
Debugf(string, ...interface{})
// SetFlags sets the log entry prefix flags
SetFlags(LogFlags)
// SetOutput sets the output writer for the logger
SetOutput(io.Writer)
// SetDebug sets debug mode
SetDebug(bool)
// SetPrefix sets the logger's prefix text that is prepended to each log line
SetPrefix(string)
}
Logger is a a simple logger interface that only supports two levels: INFO and DEBUG