diff options
author | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-21 14:26:44 +0800 |
---|---|---|
committer | Taylor Gerring <taylor.gerring@gmail.com> | 2015-03-21 14:26:44 +0800 |
commit | 9edb9a21bce97594928f8660e8e32df2cb25b74d (patch) | |
tree | a933fa349621d842f361762045eabc96772d147d /logger | |
parent | 4f5b362bda862b0d77583491d5e04193148a9349 (diff) | |
download | go-tangerine-9edb9a21bce97594928f8660e8e32df2cb25b74d.tar.gz go-tangerine-9edb9a21bce97594928f8660e8e32df2cb25b74d.tar.zst go-tangerine-9edb9a21bce97594928f8660e8e32df2cb25b74d.zip |
Add json LogSystem #538
Diffstat (limited to 'logger')
-rw-r--r-- | logger/log.go | 2 | ||||
-rw-r--r-- | logger/logsystem.go | 24 | ||||
-rw-r--r-- | logger/sys.go | 3 |
3 files changed, 27 insertions, 2 deletions
diff --git a/logger/log.go b/logger/log.go index 2dbec86de..fab004d0a 100644 --- a/logger/log.go +++ b/logger/log.go @@ -30,6 +30,8 @@ func New(datadir string, logFile string, logLevel int, logFormat string) LogSyst switch logFormat { case "raw": sys = NewRawLogSystem(writer, 0, LogLevel(logLevel)) + case "json": + sys = NewJsonLogSystem(writer, 0, LogLevel(logLevel)) default: sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel)) } diff --git a/logger/logsystem.go b/logger/logsystem.go index 8458b938f..1318a9f96 100644 --- a/logger/logsystem.go +++ b/logger/logsystem.go @@ -61,3 +61,27 @@ func (t *rawLogSystem) SetLogLevel(i LogLevel) { func (t *rawLogSystem) GetLogLevel() LogLevel { 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)) +} diff --git a/logger/sys.go b/logger/sys.go index bd826b587..db4251a52 100644 --- a/logger/sys.go +++ b/logger/sys.go @@ -76,8 +76,7 @@ func dispatchLoop() { func sysLoop(sys LogSystem, in <-chan message, wg *sync.WaitGroup) { for msg := range in { switch sys.(type) { - case *rawLogSystem: - // This is a semantic hack since rawLogSystem has little to do with JsonLevel + case *jsonLogSystem: if msg.level == JsonLevel { sys.LogPrint(msg.level, msg.msg) } |