Add json LogSystem #538

This commit is contained in:
Taylor Gerring 2015-03-21 07:26:44 +01:00
parent 4f5b362bda
commit 9edb9a21bc
3 changed files with 27 additions and 2 deletions

View File

@ -30,6 +30,8 @@ func New(datadir string, logFile string, logLevel int, logFormat string) LogSyst
switch logFormat { switch logFormat {
case "raw": case "raw":
sys = NewRawLogSystem(writer, 0, LogLevel(logLevel)) sys = NewRawLogSystem(writer, 0, LogLevel(logLevel))
case "json":
sys = NewJsonLogSystem(writer, 0, LogLevel(logLevel))
default: default:
sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel)) sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel))
} }

View File

@ -61,3 +61,27 @@ func (t *rawLogSystem) SetLogLevel(i LogLevel) {
func (t *rawLogSystem) GetLogLevel() LogLevel { func (t *rawLogSystem) GetLogLevel() LogLevel {
return LogLevel(atomic.LoadUint32(&t.level)) return LogLevel(atomic.LoadUint32(&t.level))
} }
// NewRawLogSystem creates a LogSystem that prints to the given writer without
// adding extra information. Suitable for preformatted output
func NewJsonLogSystem(writer io.Writer, flags int, level LogLevel) LogSystem {
logger := log.New(writer, "", 0)
return &jsonLogSystem{logger, uint32(level)}
}
type jsonLogSystem struct {
logger *log.Logger
level uint32
}
func (t *jsonLogSystem) LogPrint(level LogLevel, msg string) {
t.logger.Print(msg)
}
func (t *jsonLogSystem) SetLogLevel(i LogLevel) {
atomic.StoreUint32(&t.level, uint32(i))
}
func (t *jsonLogSystem) GetLogLevel() LogLevel {
return LogLevel(atomic.LoadUint32(&t.level))
}

View File

@ -76,8 +76,7 @@ func dispatchLoop() {
func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) {
for msg := range in { for msg := range in {
switch sys.(type) { switch sys.(type) {
case *rawLogSystem: case *jsonLogSystem:
// This is a semantic hack since rawLogSystem has little to do with JsonLevel
if msg.level == JsonLevel { if msg.level == JsonLevel {
sys.LogPrint(msg.level, msg.msg) sys.LogPrint(msg.level, msg.msg)
} }