...

Source file src/go.etcd.io/etcd/raft/v3/logger.go

Documentation: go.etcd.io/etcd/raft/v3

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

View as plain text