Skip to content

Commit

Permalink
log: add support for trace level, exit on critical
Browse files Browse the repository at this point in the history
  • Loading branch information
karalabe committed Feb 23, 2017
1 parent ec7f81f commit b9d48b4
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions log/README_ETHEREUM.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ This package is a fork of https://github.com/inconshreveable/log15, with some
minor modifications required by the go-ethereum codebase:

* Support for log level `trace`
* Modified behavior to exit on `critical` failure
2 changes: 2 additions & 0 deletions log/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func TerminalFormat() Format {
color = 32
case LvlDebug:
color = 36
case LvlTrace:
color = 34
}

b := &bytes.Buffer{}
Expand Down
12 changes: 12 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package log

import (
"fmt"
"os"
"time"

"github.com/go-stack/stack"
Expand All @@ -20,11 +21,14 @@ const (
LvlWarn
LvlInfo
LvlDebug
LvlTrace
)

// Returns the name of a Lvl
func (l Lvl) String() string {
switch l {
case LvlTrace:
return "trce"
case LvlDebug:
return "dbug"
case LvlInfo:
Expand All @@ -44,6 +48,8 @@ func (l Lvl) String() string {
// Useful for parsing command line args and configuration files.
func LvlFromString(lvlString string) (Lvl, error) {
switch lvlString {
case "trace", "trce":
return LvlTrace, nil
case "debug", "dbug":
return LvlDebug, nil
case "info":
Expand Down Expand Up @@ -87,6 +93,7 @@ type Logger interface {
SetHandler(h Handler)

// Log a message at the given level with context key/value pairs
Trace(msg string, ctx ...interface{})
Debug(msg string, ctx ...interface{})
Info(msg string, ctx ...interface{})
Warn(msg string, ctx ...interface{})
Expand Down Expand Up @@ -128,6 +135,10 @@ func newContext(prefix []interface{}, suffix []interface{}) []interface{} {
return newCtx
}

func (l *logger) Trace(msg string, ctx ...interface{}) {
l.write(msg, LvlTrace, ctx)
}

func (l *logger) Debug(msg string, ctx ...interface{}) {
l.write(msg, LvlDebug, ctx)
}
Expand All @@ -146,6 +157,7 @@ func (l *logger) Error(msg string, ctx ...interface{}) {

func (l *logger) Crit(msg string, ctx ...interface{}) {
l.write(msg, LvlCrit, ctx)
os.Exit(1)
}

func (l *logger) GetHandler() Handler {
Expand Down
6 changes: 6 additions & 0 deletions log/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ func Root() Logger {
// etc.) to keep the call depth the same for all paths to logger.write so
// runtime.Caller(2) always refers to the call site in client code.

// Trace is a convenient alias for Root().Trace
func Trace(msg string, ctx ...interface{}) {
root.write(msg, LvlTrace, ctx)
}

// Debug is a convenient alias for Root().Debug
func Debug(msg string, ctx ...interface{}) {
root.write(msg, LvlDebug, ctx)
Expand All @@ -64,4 +69,5 @@ func Error(msg string, ctx ...interface{}) {
// Crit is a convenient alias for Root().Crit
func Crit(msg string, ctx ...interface{}) {
root.write(msg, LvlCrit, ctx)
os.Exit(1)
}
2 changes: 2 additions & 0 deletions log/syslog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ func sharedSyslog(fmtr Format, sysWr *syslog.Writer, err error) (Handler, error)
syslogFn = sysWr.Info
case LvlDebug:
syslogFn = sysWr.Debug
case LvlTrace:
syslogFn = func(m string) error { return nil } // There's no syslog level for trace
}

s := strings.TrimSpace(string(fmtr.Format(r)))
Expand Down

0 comments on commit b9d48b4

Please sign in to comment.