blob: 426a77d344548fb9c7c6140df80d809d90467e9e [file] [log] [blame]
Stephane Barbarie260a5632019-02-26 16:12:49 -05001// 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
15package raft
16
17import (
18 "fmt"
19 "io/ioutil"
20 "log"
21 "os"
22)
23
24type 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
44func SetLogger(l Logger) { raftLogger = l }
45
46var (
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
52const (
53 calldepth = 2
54)
55
56// DefaultLogger is a default implementation of the Logger interface.
57type DefaultLogger struct {
58 *log.Logger
59 debug bool
60}
61
62func (l *DefaultLogger) EnableTimestamps() {
63 l.SetFlags(l.Flags() | log.Ldate | log.Ltime)
64}
65
66func (l *DefaultLogger) EnableDebug() {
67 l.debug = true
68}
69
70func (l *DefaultLogger) Debug(v ...interface{}) {
71 if l.debug {
72 l.Output(calldepth, header("DEBUG", fmt.Sprint(v...)))
73 }
74}
75
76func (l *DefaultLogger) Debugf(format string, v ...interface{}) {
77 if l.debug {
78 l.Output(calldepth, header("DEBUG", fmt.Sprintf(format, v...)))
79 }
80}
81
82func (l *DefaultLogger) Info(v ...interface{}) {
83 l.Output(calldepth, header("INFO", fmt.Sprint(v...)))
84}
85
86func (l *DefaultLogger) Infof(format string, v ...interface{}) {
87 l.Output(calldepth, header("INFO", fmt.Sprintf(format, v...)))
88}
89
90func (l *DefaultLogger) Error(v ...interface{}) {
91 l.Output(calldepth, header("ERROR", fmt.Sprint(v...)))
92}
93
94func (l *DefaultLogger) Errorf(format string, v ...interface{}) {
95 l.Output(calldepth, header("ERROR", fmt.Sprintf(format, v...)))
96}
97
98func (l *DefaultLogger) Warning(v ...interface{}) {
99 l.Output(calldepth, header("WARN", fmt.Sprint(v...)))
100}
101
102func (l *DefaultLogger) Warningf(format string, v ...interface{}) {
103 l.Output(calldepth, header("WARN", fmt.Sprintf(format, v...)))
104}
105
106func (l *DefaultLogger) Fatal(v ...interface{}) {
107 l.Output(calldepth, header("FATAL", fmt.Sprint(v...)))
108 os.Exit(1)
109}
110
111func (l *DefaultLogger) Fatalf(format string, v ...interface{}) {
112 l.Output(calldepth, header("FATAL", fmt.Sprintf(format, v...)))
113 os.Exit(1)
114}
115
116func (l *DefaultLogger) Panic(v ...interface{}) {
117 l.Logger.Panic(v...)
118}
119
120func (l *DefaultLogger) Panicf(format string, v ...interface{}) {
121 l.Logger.Panicf(format, v...)
122}
123
124func header(lvl, msg string) string {
125 return fmt.Sprintf("%s: %s", lvl, msg)
126}