logger.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. package raft
  15. import (
  16. "fmt"
  17. "io/ioutil"
  18. "log"
  19. "os"
  20. "sync"
  21. )
  22. type Logger interface {
  23. Debug(v ...interface{})
  24. Debugf(format string, v ...interface{})
  25. Error(v ...interface{})
  26. Errorf(format string, v ...interface{})
  27. Info(v ...interface{})
  28. Infof(format string, v ...interface{})
  29. Warning(v ...interface{})
  30. Warningf(format string, v ...interface{})
  31. Fatal(v ...interface{})
  32. Fatalf(format string, v ...interface{})
  33. Panic(v ...interface{})
  34. Panicf(format string, v ...interface{})
  35. }
  36. func SetLogger(l Logger) {
  37. raftLoggerMu.Lock()
  38. raftLogger = l
  39. raftLoggerMu.Unlock()
  40. }
  41. var (
  42. defaultLogger = &DefaultLogger{Logger: log.New(os.Stderr, "raft", log.LstdFlags)}
  43. discardLogger = &DefaultLogger{Logger: log.New(ioutil.Discard, "", 0)}
  44. raftLoggerMu sync.Mutex
  45. raftLogger = Logger(defaultLogger)
  46. )
  47. const (
  48. calldepth = 2
  49. )
  50. // DefaultLogger is a default implementation of the Logger interface.
  51. type DefaultLogger struct {
  52. *log.Logger
  53. debug bool
  54. }
  55. func (l *DefaultLogger) EnableTimestamps() {
  56. l.SetFlags(l.Flags() | log.Ldate | log.Ltime)
  57. }
  58. func (l *DefaultLogger) EnableDebug() {
  59. l.debug = true
  60. }
  61. func (l *DefaultLogger) Debug(v ...interface{}) {
  62. if l.debug {
  63. l.Output(calldepth, header("DEBUG", fmt.Sprint(v...)))
  64. }
  65. }
  66. func (l *DefaultLogger) Debugf(format string, v ...interface{}) {
  67. if l.debug {
  68. l.Output(calldepth, header("DEBUG", fmt.Sprintf(format, v...)))
  69. }
  70. }
  71. func (l *DefaultLogger) Info(v ...interface{}) {
  72. l.Output(calldepth, header("INFO", fmt.Sprint(v...)))
  73. }
  74. func (l *DefaultLogger) Infof(format string, v ...interface{}) {
  75. l.Output(calldepth, header("INFO", fmt.Sprintf(format, v...)))
  76. }
  77. func (l *DefaultLogger) Error(v ...interface{}) {
  78. l.Output(calldepth, header("ERROR", fmt.Sprint(v...)))
  79. }
  80. func (l *DefaultLogger) Errorf(format string, v ...interface{}) {
  81. l.Output(calldepth, header("ERROR", fmt.Sprintf(format, v...)))
  82. }
  83. func (l *DefaultLogger) Warning(v ...interface{}) {
  84. l.Output(calldepth, header("WARN", fmt.Sprint(v...)))
  85. }
  86. func (l *DefaultLogger) Warningf(format string, v ...interface{}) {
  87. l.Output(calldepth, header("WARN", fmt.Sprintf(format, v...)))
  88. }
  89. func (l *DefaultLogger) Fatal(v ...interface{}) {
  90. l.Output(calldepth, header("FATAL", fmt.Sprint(v...)))
  91. os.Exit(1)
  92. }
  93. func (l *DefaultLogger) Fatalf(format string, v ...interface{}) {
  94. l.Output(calldepth, header("FATAL", fmt.Sprintf(format, v...)))
  95. os.Exit(1)
  96. }
  97. func (l *DefaultLogger) Panic(v ...interface{}) {
  98. l.Logger.Panic(v...)
  99. }
  100. func (l *DefaultLogger) Panicf(format string, v ...interface{}) {
  101. l.Logger.Panicf(format, v...)
  102. }
  103. func header(lvl, msg string) string {
  104. return fmt.Sprintf("%s: %s", lvl, msg)
  105. }