blob: e3cb00cc9d13b05c5528ad33a2c3e83b034f61de [file] [log] [blame]
Abhay Kumar40252eb2025-10-13 13:25:53 +00001// 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"
20 "log"
21 "os"
22 "sync"
23)
24
25type Logger interface {
26 Debug(v ...interface{})
27 Debugf(format string, v ...interface{})
28
29 Error(v ...interface{})
30 Errorf(format string, v ...interface{})
31
32 Info(v ...interface{})
33 Infof(format string, v ...interface{})
34
35 Warning(v ...interface{})
36 Warningf(format string, v ...interface{})
37
38 Fatal(v ...interface{})
39 Fatalf(format string, v ...interface{})
40
41 Panic(v ...interface{})
42 Panicf(format string, v ...interface{})
43}
44
45func SetLogger(l Logger) {
46 raftLoggerMu.Lock()
47 raftLogger = l
48 raftLoggerMu.Unlock()
49}
50
51func ResetDefaultLogger() {
52 SetLogger(defaultLogger)
53}
54
55func getLogger() Logger {
56 raftLoggerMu.Lock()
57 defer raftLoggerMu.Unlock()
58 return raftLogger
59}
60
61var (
62 defaultLogger = &DefaultLogger{Logger: log.New(os.Stderr, "raft", log.LstdFlags)}
63 discardLogger = &DefaultLogger{Logger: log.New(io.Discard, "", 0)}
64 raftLoggerMu sync.Mutex
65 raftLogger = Logger(defaultLogger)
66)
67
68const (
69 calldepth = 2
70)
71
72// DefaultLogger is a default implementation of the Logger interface.
73type DefaultLogger struct {
74 *log.Logger
75 debug bool
76}
77
78func (l *DefaultLogger) EnableTimestamps() {
79 l.SetFlags(l.Flags() | log.Ldate | log.Ltime)
80}
81
82func (l *DefaultLogger) EnableDebug() {
83 l.debug = true
84}
85
86func (l *DefaultLogger) Debug(v ...interface{}) {
87 if l.debug {
88 l.Output(calldepth, header("DEBUG", fmt.Sprint(v...)))
89 }
90}
91
92func (l *DefaultLogger) Debugf(format string, v ...interface{}) {
93 if l.debug {
94 l.Output(calldepth, header("DEBUG", fmt.Sprintf(format, v...)))
95 }
96}
97
98func (l *DefaultLogger) Info(v ...interface{}) {
99 l.Output(calldepth, header("INFO", fmt.Sprint(v...)))
100}
101
102func (l *DefaultLogger) Infof(format string, v ...interface{}) {
103 l.Output(calldepth, header("INFO", fmt.Sprintf(format, v...)))
104}
105
106func (l *DefaultLogger) Error(v ...interface{}) {
107 l.Output(calldepth, header("ERROR", fmt.Sprint(v...)))
108}
109
110func (l *DefaultLogger) Errorf(format string, v ...interface{}) {
111 l.Output(calldepth, header("ERROR", fmt.Sprintf(format, v...)))
112}
113
114func (l *DefaultLogger) Warning(v ...interface{}) {
115 l.Output(calldepth, header("WARN", fmt.Sprint(v...)))
116}
117
118func (l *DefaultLogger) Warningf(format string, v ...interface{}) {
119 l.Output(calldepth, header("WARN", fmt.Sprintf(format, v...)))
120}
121
122func (l *DefaultLogger) Fatal(v ...interface{}) {
123 l.Output(calldepth, header("FATAL", fmt.Sprint(v...)))
124 os.Exit(1)
125}
126
127func (l *DefaultLogger) Fatalf(format string, v ...interface{}) {
128 l.Output(calldepth, header("FATAL", fmt.Sprintf(format, v...)))
129 os.Exit(1)
130}
131
132func (l *DefaultLogger) Panic(v ...interface{}) {
133 l.Logger.Panic(v...)
134}
135
136func (l *DefaultLogger) Panicf(format string, v ...interface{}) {
137 l.Logger.Panicf(format, v...)
138}
139
140func header(lvl, msg string) string {
141 return fmt.Sprintf("%s: %s", lvl, msg)
142}