logger.go 3.1 KB

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