2011-09-28 22:47:30 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// A simple level-based logging system.
|
2011-10-22 11:06:18 +00:00
|
|
|
|
|
|
|
// Note that higher levels of logging are still usable via Log(). They will be
|
|
|
|
// output to the debug log in split mode if --log.level is set high enough.
|
|
|
|
|
|
|
|
// Also, remember to call flag.Parse() near the start of your func main()!
|
|
|
|
|
|
|
|
// The enforced singleton style of the standard "log" pkg is very nice, but
|
|
|
|
// it encourages people to write less testable code, and while logging is one
|
|
|
|
// of the few places where a singleton is not necessarily bad practise, it's
|
|
|
|
// not *that* hard to propagate your logging to where it needs to be.
|
|
|
|
// Alternatively you can create your own damn singleton with this package ;-)
|
|
|
|
|
|
|
|
type LogLevel int
|
|
|
|
type LogMap map[LogLevel]*log.Logger
|
|
|
|
|
2011-09-28 22:47:30 +00:00
|
|
|
const (
|
2011-10-22 11:06:18 +00:00
|
|
|
Fatal LogLevel = iota - 1
|
|
|
|
Error
|
|
|
|
Warn
|
|
|
|
Info
|
|
|
|
Debug
|
2011-09-28 22:47:30 +00:00
|
|
|
)
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
var logString map[LogLevel]string = map[LogLevel]string{
|
|
|
|
Fatal: "FATAL",
|
|
|
|
Error: "ERROR",
|
|
|
|
Warn: "WARN",
|
|
|
|
Info: "INFO",
|
|
|
|
Debug: "DEBUG",
|
|
|
|
}
|
|
|
|
func LogString(lv LogLevel) string {
|
|
|
|
if s, ok := logString[lv]; ok {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("LOG(%d)", lv)
|
|
|
|
}
|
|
|
|
|
2011-09-28 22:47:30 +00:00
|
|
|
var (
|
2011-09-29 20:31:05 +00:00
|
|
|
file = flag.String("log.file", "",
|
2011-09-28 22:47:30 +00:00
|
|
|
"Log to this file rather than STDERR")
|
2011-10-22 11:06:18 +00:00
|
|
|
level = flag.Int("log.level", int(Error),
|
2011-09-28 22:47:30 +00:00
|
|
|
"Level of logging to be output")
|
2011-09-29 20:31:05 +00:00
|
|
|
only = flag.Bool("log.only", false,
|
2011-09-28 22:47:30 +00:00
|
|
|
"Only log output at the selected level")
|
2011-10-22 11:06:18 +00:00
|
|
|
split = flag.Bool("log.split", false,
|
|
|
|
"Log to one file per log level Error/Warn/Info/Debug.")
|
2011-09-28 22:47:30 +00:00
|
|
|
|
|
|
|
// Shortcut flags for great justice
|
2011-09-29 20:31:05 +00:00
|
|
|
quiet = flag.Bool("log.quiet", false,
|
2011-09-28 22:47:30 +00:00
|
|
|
"Only fatal output (equivalent to -v -1)")
|
2011-09-29 20:31:05 +00:00
|
|
|
warn = flag.Bool("log.warn", false,
|
2011-09-28 22:47:30 +00:00
|
|
|
"Warning output (equivalent to -v 1)")
|
2011-09-29 20:31:05 +00:00
|
|
|
info = flag.Bool("log.info", false,
|
2011-09-28 22:47:30 +00:00
|
|
|
"Info output (equivalent to -v 2)")
|
2011-09-29 20:31:05 +00:00
|
|
|
debug = flag.Bool("log.debug", false,
|
2011-09-28 22:47:30 +00:00
|
|
|
"Debug output (equivalent to -v 3)")
|
|
|
|
)
|
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
// Log at a given level
|
|
|
|
Log(int, string, ...interface{})
|
|
|
|
// Log at level 3
|
|
|
|
Debug(string, ...interface{})
|
|
|
|
// Log at level 2
|
|
|
|
Info(string, ...interface{})
|
|
|
|
// Log at level 1
|
|
|
|
Warn(string, ...interface{})
|
|
|
|
// Log at level 0
|
|
|
|
Error(string, ...interface{})
|
|
|
|
// Log at level -1, to STDERR always, and exit after logging.
|
|
|
|
Fatal(string, ...interface{})
|
|
|
|
// Change the current log display level
|
2011-10-22 11:06:18 +00:00
|
|
|
SetLogLevel(LogLevel)
|
|
|
|
// Set the logger to only output the current level
|
|
|
|
SetOnly(bool)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A struct to implement the above interface
|
|
|
|
type logger struct {
|
2011-10-22 11:06:18 +00:00
|
|
|
// We wrap a set of log.Logger for most of the heavy lifting
|
2011-09-28 22:47:30 +00:00
|
|
|
// but it can't be anonymous thanks to the conflicting definitions of Fatal
|
2011-10-22 11:06:18 +00:00
|
|
|
log LogMap
|
|
|
|
level LogLevel
|
2011-09-28 22:47:30 +00:00
|
|
|
only bool
|
|
|
|
*sync.Mutex // to ensure changing levels/flags is atomic
|
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// Helper function for opening log files, causes lots of Fatal :-)
|
|
|
|
func openLog(fn string) *log.Logger {
|
|
|
|
fh, err := os.OpenFile(fn, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("Error opening log file: %s", err)
|
|
|
|
}
|
|
|
|
return makeLogger(fh)
|
|
|
|
}
|
2011-09-28 22:47:30 +00:00
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// Helper function to create log.Loggers out of io.Writers
|
|
|
|
func makeLogger(w io.Writer) *log.Logger {
|
|
|
|
return log.New(w, "", log.LstdFlags | log.Lshortfile)
|
|
|
|
}
|
2011-09-28 22:47:30 +00:00
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// Creates a new logger object using the flags declared above.
|
|
|
|
// You MUST call flag.Parse before calling this ;-)
|
|
|
|
// Calling this more than once is inadvisable, you may get log corruption.
|
|
|
|
func NewFromFlags() *logger {
|
|
|
|
// Sanity checks: if log.split is set, must have a log.file.
|
|
|
|
if *split && *file == "" {
|
|
|
|
log.Fatalf("You must pass --log.file with --log.split")
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
lv := Error
|
|
|
|
logMap := make(LogMap)
|
|
|
|
|
2011-09-28 22:47:30 +00:00
|
|
|
// What are we logging?
|
|
|
|
// The shortcut flags prioritize by level, but an
|
|
|
|
// explicit level flag takes first precedence.
|
|
|
|
// I think the switch looks cleaner than if/else if, meh :-)
|
|
|
|
switch {
|
|
|
|
case *level != 0:
|
2011-10-22 11:06:18 +00:00
|
|
|
lv = LogLevel(*level)
|
2011-09-28 22:47:30 +00:00
|
|
|
case *quiet:
|
2011-10-22 11:06:18 +00:00
|
|
|
lv = Fatal
|
2011-09-28 22:47:30 +00:00
|
|
|
case *warn:
|
2011-10-22 11:06:18 +00:00
|
|
|
lv = Warn
|
2011-09-28 22:47:30 +00:00
|
|
|
case *info:
|
2011-10-22 11:06:18 +00:00
|
|
|
lv = Info
|
2011-09-28 22:47:30 +00:00
|
|
|
case *debug:
|
2011-10-22 11:06:18 +00:00
|
|
|
lv = Debug
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// Where are we logging to?
|
|
|
|
if *split {
|
|
|
|
// Fill in the logger map.
|
|
|
|
for l := Fatal; l <= Debug; l++ {
|
|
|
|
logMap[l] = openLog(*file + "." + logString[l])
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var _log *log.Logger
|
|
|
|
if *file != "" {
|
|
|
|
_log = openLog(*file)
|
|
|
|
} else {
|
|
|
|
_log = makeLogger(os.Stderr)
|
|
|
|
}
|
|
|
|
for l := Fatal; l <= Debug; l++ {
|
|
|
|
logMap[l] = _log
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return New(logMap, lv, *only)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
// You'll have to set up your own loggers for this one...
|
|
|
|
func New(m LogMap, lv LogLevel, only bool) *logger {
|
|
|
|
// Sanity check the log map we've been passed.
|
|
|
|
// We need loggers for all levels in case SetLogLevel is called.
|
|
|
|
for l := Fatal; l <= Debug; l++ {
|
|
|
|
if _log, ok := m[l]; !ok || _log == nil {
|
|
|
|
log.Fatalf("Output log level %s has no logger configured.",
|
|
|
|
logString[l])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &logger{m, lv, only, &sync.Mutex{}}
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 21:45:26 +00:00
|
|
|
// Internal function all others call to ensure identical call depth
|
2011-10-22 11:06:18 +00:00
|
|
|
func (l *logger) write(lv LogLevel, fm string, v ...interface{}) {
|
|
|
|
if lv > l.level || (l.only && lv != l.level) {
|
2011-09-28 22:47:30 +00:00
|
|
|
// Your logs are not important to us, goodnight
|
|
|
|
return
|
|
|
|
}
|
2011-10-22 11:06:18 +00:00
|
|
|
fm = fmt.Sprintf(LogString(lv)+" "+fm, v...)
|
2011-10-22 14:43:13 +00:00
|
|
|
if lv > Debug || lv < Fatal {
|
2011-10-22 11:06:18 +00:00
|
|
|
// This is an unrecognised log level, so log it to Debug
|
|
|
|
lv = Debug
|
|
|
|
}
|
2011-09-28 22:47:30 +00:00
|
|
|
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
// Writing the log is deceptively simple
|
2011-10-22 14:43:13 +00:00
|
|
|
l.log[lv].Output(3, fm)
|
2011-10-22 11:06:18 +00:00
|
|
|
if lv == Fatal {
|
|
|
|
// Always fatal to stderr too. Use panic so (a) we get a backtrace,
|
|
|
|
// and (b) it's trappable for testing (and maybe other times too).
|
|
|
|
log.Panic(fm)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
func (l *logger) Log(lv LogLevel, fm string, v ...interface{}) {
|
2011-09-29 21:45:26 +00:00
|
|
|
l.write(lv, fm, v...)
|
|
|
|
}
|
|
|
|
|
2011-09-28 22:47:30 +00:00
|
|
|
// Helper functions for specific levels
|
|
|
|
func (l *logger) Debug(fm string, v ...interface{}) {
|
2011-10-22 11:06:18 +00:00
|
|
|
l.write(Debug, fm, v...)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Info(fm string, v ...interface{}) {
|
2011-10-22 11:06:18 +00:00
|
|
|
l.write(Info, fm, v...)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Warn(fm string, v ...interface{}) {
|
2011-10-22 11:06:18 +00:00
|
|
|
l.write(Warn, fm, v...)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Error(fm string, v ...interface{}) {
|
2011-10-22 11:06:18 +00:00
|
|
|
l.write(Error, fm, v...)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l *logger) Fatal(fm string, v ...interface{}) {
|
2011-10-22 11:06:18 +00:00
|
|
|
l.write(Fatal, fm, v...)
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
func (l *logger) SetLogLevel(lv LogLevel) {
|
2011-09-28 22:47:30 +00:00
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.level = lv
|
|
|
|
}
|
|
|
|
|
2011-10-22 11:06:18 +00:00
|
|
|
func (l *logger) SetOnly(only bool) {
|
|
|
|
l.Lock()
|
|
|
|
defer l.Unlock()
|
|
|
|
l.only = only
|
2011-09-28 22:47:30 +00:00
|
|
|
}
|