Scott Baker | eee8dd8 | 2019-09-24 12:52:34 -0700 | [diff] [blame] | 1 | // Copyright 2015 The etcd Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package raft |
| 16 | |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "io/ioutil" |
| 20 | "log" |
| 21 | "os" |
| 22 | ) |
| 23 | |
| 24 | type Logger interface { |
| 25 | Debug(v ...interface{}) |
| 26 | Debugf(format string, v ...interface{}) |
| 27 | |
| 28 | Error(v ...interface{}) |
| 29 | Errorf(format string, v ...interface{}) |
| 30 | |
| 31 | Info(v ...interface{}) |
| 32 | Infof(format string, v ...interface{}) |
| 33 | |
| 34 | Warning(v ...interface{}) |
| 35 | Warningf(format string, v ...interface{}) |
| 36 | |
| 37 | Fatal(v ...interface{}) |
| 38 | Fatalf(format string, v ...interface{}) |
| 39 | |
| 40 | Panic(v ...interface{}) |
| 41 | Panicf(format string, v ...interface{}) |
| 42 | } |
| 43 | |
| 44 | func SetLogger(l Logger) { raftLogger = l } |
| 45 | |
| 46 | var ( |
| 47 | defaultLogger = &DefaultLogger{Logger: log.New(os.Stderr, "raft", log.LstdFlags)} |
| 48 | discardLogger = &DefaultLogger{Logger: log.New(ioutil.Discard, "", 0)} |
| 49 | raftLogger = Logger(defaultLogger) |
| 50 | ) |
| 51 | |
| 52 | const ( |
| 53 | calldepth = 2 |
| 54 | ) |
| 55 | |
| 56 | // DefaultLogger is a default implementation of the Logger interface. |
| 57 | type DefaultLogger struct { |
| 58 | *log.Logger |
| 59 | debug bool |
| 60 | } |
| 61 | |
| 62 | func (l *DefaultLogger) EnableTimestamps() { |
| 63 | l.SetFlags(l.Flags() | log.Ldate | log.Ltime) |
| 64 | } |
| 65 | |
| 66 | func (l *DefaultLogger) EnableDebug() { |
| 67 | l.debug = true |
| 68 | } |
| 69 | |
| 70 | func (l *DefaultLogger) Debug(v ...interface{}) { |
| 71 | if l.debug { |
| 72 | l.Output(calldepth, header("DEBUG", fmt.Sprint(v...))) |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | func (l *DefaultLogger) Debugf(format string, v ...interface{}) { |
| 77 | if l.debug { |
| 78 | l.Output(calldepth, header("DEBUG", fmt.Sprintf(format, v...))) |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | func (l *DefaultLogger) Info(v ...interface{}) { |
| 83 | l.Output(calldepth, header("INFO", fmt.Sprint(v...))) |
| 84 | } |
| 85 | |
| 86 | func (l *DefaultLogger) Infof(format string, v ...interface{}) { |
| 87 | l.Output(calldepth, header("INFO", fmt.Sprintf(format, v...))) |
| 88 | } |
| 89 | |
| 90 | func (l *DefaultLogger) Error(v ...interface{}) { |
| 91 | l.Output(calldepth, header("ERROR", fmt.Sprint(v...))) |
| 92 | } |
| 93 | |
| 94 | func (l *DefaultLogger) Errorf(format string, v ...interface{}) { |
| 95 | l.Output(calldepth, header("ERROR", fmt.Sprintf(format, v...))) |
| 96 | } |
| 97 | |
| 98 | func (l *DefaultLogger) Warning(v ...interface{}) { |
| 99 | l.Output(calldepth, header("WARN", fmt.Sprint(v...))) |
| 100 | } |
| 101 | |
| 102 | func (l *DefaultLogger) Warningf(format string, v ...interface{}) { |
| 103 | l.Output(calldepth, header("WARN", fmt.Sprintf(format, v...))) |
| 104 | } |
| 105 | |
| 106 | func (l *DefaultLogger) Fatal(v ...interface{}) { |
| 107 | l.Output(calldepth, header("FATAL", fmt.Sprint(v...))) |
| 108 | os.Exit(1) |
| 109 | } |
| 110 | |
| 111 | func (l *DefaultLogger) Fatalf(format string, v ...interface{}) { |
| 112 | l.Output(calldepth, header("FATAL", fmt.Sprintf(format, v...))) |
| 113 | os.Exit(1) |
| 114 | } |
| 115 | |
| 116 | func (l *DefaultLogger) Panic(v ...interface{}) { |
| 117 | l.Logger.Panic(v...) |
| 118 | } |
| 119 | |
| 120 | func (l *DefaultLogger) Panicf(format string, v ...interface{}) { |
| 121 | l.Logger.Panicf(format, v...) |
| 122 | } |
| 123 | |
| 124 | func header(lvl, msg string) string { |
| 125 | return fmt.Sprintf("%s: %s", lvl, msg) |
| 126 | } |