logger.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright 2018 Dgraph Labs, Inc. and Contributors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package badger
  17. import (
  18. "log"
  19. "os"
  20. )
  21. // Logger is implemented by any logging system that is used for standard logs.
  22. type Logger interface {
  23. Errorf(string, ...interface{})
  24. Warningf(string, ...interface{})
  25. Infof(string, ...interface{})
  26. Debugf(string, ...interface{})
  27. }
  28. // Errorf logs an ERROR log message to the logger specified in opts or to the
  29. // global logger if no logger is specified in opts.
  30. func (opt *Options) Errorf(format string, v ...interface{}) {
  31. if opt.Logger == nil {
  32. return
  33. }
  34. opt.Logger.Errorf(format, v...)
  35. }
  36. // Infof logs an INFO message to the logger specified in opts.
  37. func (opt *Options) Infof(format string, v ...interface{}) {
  38. if opt.Logger == nil {
  39. return
  40. }
  41. opt.Logger.Infof(format, v...)
  42. }
  43. // Warningf logs a WARNING message to the logger specified in opts.
  44. func (opt *Options) Warningf(format string, v ...interface{}) {
  45. if opt.Logger == nil {
  46. return
  47. }
  48. opt.Logger.Warningf(format, v...)
  49. }
  50. // Debugf logs a DEBUG message to the logger specified in opts.
  51. func (opt *Options) Debugf(format string, v ...interface{}) {
  52. if opt.Logger == nil {
  53. return
  54. }
  55. opt.Logger.Debugf(format, v...)
  56. }
  57. type loggingLevel int
  58. const (
  59. DEBUG loggingLevel = iota
  60. INFO
  61. WARNING
  62. ERROR
  63. )
  64. type defaultLog struct {
  65. *log.Logger
  66. level loggingLevel
  67. }
  68. func defaultLogger(level loggingLevel) *defaultLog {
  69. return &defaultLog{Logger: log.New(os.Stderr, "badger ", log.LstdFlags), level: level}
  70. }
  71. func (l *defaultLog) Errorf(f string, v ...interface{}) {
  72. if l.level <= ERROR {
  73. l.Printf("ERROR: "+f, v...)
  74. }
  75. }
  76. func (l *defaultLog) Warningf(f string, v ...interface{}) {
  77. if l.level <= WARNING {
  78. l.Printf("WARNING: "+f, v...)
  79. }
  80. }
  81. func (l *defaultLog) Infof(f string, v ...interface{}) {
  82. if l.level <= INFO {
  83. l.Printf("INFO: "+f, v...)
  84. }
  85. }
  86. func (l *defaultLog) Debugf(f string, v ...interface{}) {
  87. if l.level <= DEBUG {
  88. l.Printf("DEBUG: "+f, v...)
  89. }
  90. }