Support Trace-level log messages

- New Tracef() and IsTrace() methods for dealing with Trace-level
  debugging / logging.
- Support for "trace" as a configurable log level.

Fixes #12
This commit is contained in:
James Hunt 2017-07-19 13:38:05 -04:00
parent 630c613585
commit 2c96629058
1 changed files with 22 additions and 3 deletions

View File

@ -28,6 +28,7 @@ type LogConfig struct {
type logger struct {
out io.Writer
level syslog.Priority
trace bool
ltype string
}
@ -63,7 +64,7 @@ func SetupLogging(cfg LogConfig) {
l.out = os.Stdout
}
}
l.level = get_level(cfg.Level)
l.level, l.trace = get_level(cfg.Level)
l.ltype = cfg.Type
log = &l
}
@ -89,6 +90,14 @@ func write(msg string, args ...interface{}) {
}
}
// Logs a formatted Trace message.
// Supports fmt.Sprintf style arguments.
func Tracef(msg string, args ...interface{}) {
if log.trace && log.level >= syslog.LOG_DEBUG {
write("TRACE: "+msg, args...)
}
}
// Logs a formatted Debug message.
// Supports fmt.Sprintf style arguments.
func Debugf(msg string, args ...interface{}) {
@ -158,6 +167,11 @@ func LogLevel() syslog.Priority {
return log.level
}
// Returns true if the looger will log TRACE messages
func IsTrace() bool {
return log.trace && log.level >= syslog.LOG_DEBUG
}
// Returns true if the looger will log DEBUG messages
func IsDebug() bool {
return log.level >= syslog.LOG_DEBUG
@ -199,9 +213,14 @@ func IsEmergency() bool {
}
// Validates the log level based on config strings
func get_level(level string) syslog.Priority {
func get_level(level string) (syslog.Priority, bool) {
var priority syslog.Priority
var trace bool
switch strings.ToLower(level) {
case "trace":
priority = priority | syslog.LOG_DEBUG
trace = true
case "debug":
priority = priority | syslog.LOG_DEBUG
case "info":
@ -226,7 +245,7 @@ func get_level(level string) syslog.Priority {
panic(fmt.Sprintf("Unsupported logging priority %q", level))
}
return priority
return priority, trace
}
// Validates the syslog priority, based on config strings