github.com/hellobchain/third_party@v0.0.0-20230331131523-deb0478a2e52/cloudflare/cfssl/log/log.go (about) 1 // Package log implements a wrapper around the Go standard library's 2 // logging package. Clients should set the current log level; only 3 // messages below that level will actually be logged. For example, if 4 // Level is set to LevelWarning, only log messages at the Warning, 5 // Error, and Critical levels will be logged. 6 package log 7 8 import ( 9 "fmt" 10 "log" 11 "os" 12 ) 13 14 // The following constants represent logging levels in increasing levels of seriousness. 15 const ( 16 // LevelDebug is the log level for Debug statements. 17 LevelDebug = iota 18 // LevelInfo is the log level for Info statements. 19 LevelInfo 20 // LevelWarning is the log level for Warning statements. 21 LevelWarning 22 // LevelError is the log level for Error statements. 23 LevelError 24 // LevelCritical is the log level for Critical statements. 25 LevelCritical 26 // LevelFatal is the log level for Fatal statements. 27 LevelFatal 28 ) 29 30 var levelPrefix = [...]string{ 31 LevelDebug: "DEBUG", 32 LevelInfo: "INFO", 33 LevelWarning: "WARNING", 34 LevelError: "ERROR", 35 LevelCritical: "CRITICAL", 36 LevelFatal: "FATAL", 37 } 38 39 // Level stores the current logging level. 40 var Level = LevelInfo 41 42 // SyslogWriter specifies the necessary methods for an alternate output 43 // destination passed in via SetLogger. 44 // 45 // SyslogWriter is satisfied by *syslog.Writer. 46 type SyslogWriter interface { 47 Debug(string) 48 Info(string) 49 Warning(string) 50 Err(string) 51 Crit(string) 52 Emerg(string) 53 } 54 55 // syslogWriter stores the SetLogger() parameter. 56 var syslogWriter SyslogWriter 57 58 // SetLogger sets the output used for output by this package. 59 // A *syslog.Writer is a good choice for the logger parameter. 60 // Call with a nil parameter to revert to default behavior. 61 func SetLogger(logger SyslogWriter) { 62 syslogWriter = logger 63 } 64 65 func print(l int, msg string) { 66 if l >= Level { 67 if syslogWriter != nil { 68 switch l { 69 case LevelDebug: 70 syslogWriter.Debug(msg) 71 case LevelInfo: 72 syslogWriter.Info(msg) 73 case LevelWarning: 74 syslogWriter.Warning(msg) 75 case LevelError: 76 syslogWriter.Err(msg) 77 case LevelCritical: 78 syslogWriter.Crit(msg) 79 case LevelFatal: 80 syslogWriter.Emerg(msg) 81 } 82 } else { 83 log.Printf("[%s] %s", levelPrefix[l], msg) 84 } 85 } 86 } 87 88 func outputf(l int, format string, v []interface{}) { 89 print(l, fmt.Sprintf(format, v...)) 90 } 91 92 func output(l int, v []interface{}) { 93 print(l, fmt.Sprint(v...)) 94 } 95 96 // Fatalf logs a formatted message at the "fatal" level and then exits. The 97 // arguments are handled in the same manner as fmt.Printf. 98 func Fatalf(format string, v ...interface{}) { 99 outputf(LevelFatal, format, v) 100 os.Exit(1) 101 } 102 103 // Fatal logs its arguments at the "fatal" level and then exits. 104 func Fatal(v ...interface{}) { 105 output(LevelFatal, v) 106 os.Exit(1) 107 } 108 109 // Criticalf logs a formatted message at the "critical" level. The 110 // arguments are handled in the same manner as fmt.Printf. 111 func Criticalf(format string, v ...interface{}) { 112 outputf(LevelCritical, format, v) 113 } 114 115 // Critical logs its arguments at the "critical" level. 116 func Critical(v ...interface{}) { 117 output(LevelCritical, v) 118 } 119 120 // Errorf logs a formatted message at the "error" level. The arguments 121 // are handled in the same manner as fmt.Printf. 122 func Errorf(format string, v ...interface{}) { 123 outputf(LevelError, format, v) 124 } 125 126 // Error logs its arguments at the "error" level. 127 func Error(v ...interface{}) { 128 output(LevelError, v) 129 } 130 131 // Warningf logs a formatted message at the "warning" level. The 132 // arguments are handled in the same manner as fmt.Printf. 133 func Warningf(format string, v ...interface{}) { 134 outputf(LevelWarning, format, v) 135 } 136 137 // Warning logs its arguments at the "warning" level. 138 func Warning(v ...interface{}) { 139 output(LevelWarning, v) 140 } 141 142 // Infof logs a formatted message at the "info" level. The arguments 143 // are handled in the same manner as fmt.Printf. 144 func Infof(format string, v ...interface{}) { 145 outputf(LevelInfo, format, v) 146 } 147 148 // Info logs its arguments at the "info" level. 149 func Info(v ...interface{}) { 150 output(LevelInfo, v) 151 } 152 153 // Debugf logs a formatted message at the "debug" level. The arguments 154 // are handled in the same manner as fmt.Printf. 155 func Debugf(format string, v ...interface{}) { 156 outputf(LevelDebug, format, v) 157 } 158 159 // Debug logs its arguments at the "debug" level. 160 func Debug(v ...interface{}) { 161 output(LevelDebug, v) 162 }