github.com/ethereumproject/go-ethereum@v5.5.2+incompatible/logger/loggers.go (about) 1 // Copyright 2014 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 /* 18 Package logger implements a multi-output leveled logger. 19 20 Other packages use tagged logger to send log messages to shared 21 (process-wide) logging engine. The shared logging engine dispatches to 22 multiple log systems. The log level can be set separately per log 23 system. 24 25 Logging is asynchronous and does not block the caller. Message 26 formatting is performed by the caller goroutine to avoid incorrect 27 logging of mutable state. 28 */ 29 package logger 30 31 import ( 32 "encoding/json" 33 "fmt" 34 "os" 35 ) 36 37 type LogLevel uint32 38 39 const ( 40 // Standard log levels 41 Silence LogLevel = iota 42 ErrorLevel 43 WarnLevel 44 InfoLevel 45 DebugLevel 46 DebugDetailLevel 47 ) 48 49 // A Logger prints messages prefixed by a given tag. It provides named 50 // Printf and Println style methods for all loglevels. Each ethereum 51 // component should have its own logger with a unique prefix. 52 type Logger struct { 53 tag string 54 } 55 56 func NewLogger(tag string) *Logger { 57 return &Logger{"[" + tag + "] "} 58 } 59 60 // GetTag is a getter function to expose the logger's tag 61 func (logger *Logger) GetTag() string { 62 return logger.tag 63 } 64 65 func (logger *Logger) Sendln(level LogLevel, v ...interface{}) { 66 logMessageC <- stdMsg{level, logger.tag + fmt.Sprintln(v...)} 67 } 68 69 func (logger *Logger) Sendf(level LogLevel, format string, v ...interface{}) { 70 logMessageC <- stdMsg{level, logger.tag + fmt.Sprintf(format, v...)} 71 } 72 73 // Errorln writes a message with ErrorLevel. 74 func (logger *Logger) Errorln(v ...interface{}) { 75 logger.Sendln(ErrorLevel, v...) 76 } 77 78 // Warnln writes a message with WarnLevel. 79 func (logger *Logger) Warnln(v ...interface{}) { 80 logger.Sendln(WarnLevel, v...) 81 } 82 83 // Infoln writes a message with InfoLevel. 84 func (logger *Logger) Infoln(v ...interface{}) { 85 logger.Sendln(InfoLevel, v...) 86 } 87 88 // Debugln writes a message with DebugLevel. 89 func (logger *Logger) Debugln(v ...interface{}) { 90 logger.Sendln(DebugLevel, v...) 91 } 92 93 // DebugDetailln writes a message with DebugDetailLevel. 94 func (logger *Logger) DebugDetailln(v ...interface{}) { 95 logger.Sendln(DebugDetailLevel, v...) 96 } 97 98 // Errorf writes a message with ErrorLevel. 99 func (logger *Logger) Errorf(format string, v ...interface{}) { 100 logger.Sendf(ErrorLevel, format, v...) 101 } 102 103 // Warnf writes a message with WarnLevel. 104 func (logger *Logger) Warnf(format string, v ...interface{}) { 105 logger.Sendf(WarnLevel, format, v...) 106 } 107 108 // Infof writes a message with InfoLevel. 109 func (logger *Logger) Infof(format string, v ...interface{}) { 110 logger.Sendf(InfoLevel, format, v...) 111 } 112 113 // Debugf writes a message with DebugLevel. 114 func (logger *Logger) Debugf(format string, v ...interface{}) { 115 logger.Sendf(DebugLevel, format, v...) 116 } 117 118 // DebugDetailf writes a message with DebugDetailLevel. 119 func (logger *Logger) DebugDetailf(format string, v ...interface{}) { 120 logger.Sendf(DebugDetailLevel, format, v...) 121 } 122 123 // Fatalln writes a message with ErrorLevel and exits the program. 124 func (logger *Logger) Fatalln(v ...interface{}) { 125 logger.Sendln(ErrorLevel, v...) 126 Flush() 127 os.Exit(0) 128 } 129 130 // Fatalf writes a message with ErrorLevel and exits the program. 131 func (logger *Logger) Fatalf(format string, v ...interface{}) { 132 logger.Sendf(ErrorLevel, format, v...) 133 Flush() 134 os.Exit(0) 135 } 136 137 type JsonLogger struct { 138 Coinbase string 139 } 140 141 func NewJsonLogger() *JsonLogger { 142 return &JsonLogger{} 143 } 144 145 func (logger *JsonLogger) LogJson(v JsonLog) { 146 msgname := v.EventName() 147 obj := map[string]interface{}{ 148 msgname: v, 149 } 150 151 jsontxt, _ := json.Marshal(obj) 152 logMessageC <- (jsonMsg(jsontxt)) 153 154 }