github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/logger/logsystem.go (about) 1 // Copyright 2015 The go-ethereum Authors 2 // This file is part of the go-ethereum library. 3 // 4 // The go-ethereum library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The go-ethereum library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>. 16 17 package logger 18 19 import ( 20 "io" 21 "log" 22 "sync/atomic" 23 "time" 24 25 "github.com/ethereumproject/go-ethereum/logger/glog" 26 ) 27 28 // LogSystem is implemented by log output devices. 29 // All methods can be called concurrently from multiple goroutines. 30 type LogSystem interface { 31 LogPrint(LogMsg) 32 GetLogger() *log.Logger 33 } 34 35 // NewStdLogSystem creates a LogSystem that prints to the given writer. 36 // The flag values are defined package log. 37 func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) *StdLogSystem { 38 logger := log.New(writer, "", flags) 39 return &StdLogSystem{logger, uint32(level)} 40 } 41 42 func NewMLogSystem(writer io.Writer, flags int, level LogLevel, withTimestamp bool) *MLogSystem { 43 logger := log.New(writer, "", flags) 44 return &MLogSystem{logger, uint32(level), withTimestamp} 45 } 46 47 type MLogSystem struct { 48 logger *log.Logger 49 level uint32 50 withTimestamp bool 51 } 52 53 func (m *MLogSystem) GetLogger() *log.Logger { 54 return m.logger 55 } 56 57 func (m *MLogSystem) NewFile() io.Writer { 58 f, _, err := CreateMLogFile(time.Now()) 59 if err != nil { 60 glog.Fatal(err) 61 } 62 return f 63 } 64 65 type StdLogSystem struct { 66 logger *log.Logger 67 level uint32 68 } 69 70 // GetLogger is unused, fulfills interface 71 func (t *StdLogSystem) GetLogger() *log.Logger { 72 return t.logger 73 } 74 75 func (m *MLogSystem) LogPrint(msg LogMsg) { 76 stdmsg, ok := msg.(stdMsg) 77 if ok { 78 if m.GetLogLevel() >= stdmsg.Level() { 79 if m.withTimestamp { 80 m.logger.Print(time.Now().UTC().Format(time.RFC3339), " ", stdmsg.String()) 81 } else { 82 m.logger.Print(stdmsg.String()) 83 } 84 } 85 } 86 } 87 88 func (t *StdLogSystem) LogPrint(msg LogMsg) { 89 stdmsg, ok := msg.(stdMsg) 90 if ok { 91 if t.GetLogLevel() >= stdmsg.Level() { 92 t.logger.Print(stdmsg.String()) 93 } 94 } 95 } 96 97 func (t *StdLogSystem) SetLogLevel(i LogLevel) { 98 atomic.StoreUint32(&t.level, uint32(i)) 99 } 100 101 func (t *StdLogSystem) GetLogLevel() LogLevel { 102 return LogLevel(atomic.LoadUint32(&t.level)) 103 } 104 105 func (m *MLogSystem) GetLogLevel() LogLevel { 106 return LogLevel(atomic.LoadUint32(&m.level)) 107 } 108 109 // NewJSONLogSystem creates a LogSystem that prints to the given writer without 110 // adding extra information irrespective of loglevel only if message is JSON type 111 func NewJsonLogSystem(writer io.Writer) LogSystem { 112 logger := log.New(writer, "", 0) 113 return &jsonLogSystem{logger} 114 } 115 116 type jsonLogSystem struct { 117 logger *log.Logger 118 } 119 120 // GetLogger is unused, fulfills interface 121 func (t *jsonLogSystem) GetLogger() *log.Logger { 122 return t.logger 123 } 124 125 func (t *jsonLogSystem) LogPrint(msg LogMsg) { 126 jsonmsg, ok := msg.(jsonMsg) 127 if ok { 128 t.logger.Print(jsonmsg.String()) 129 } 130 }