logger.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright (c) 2017 Uber Technologies, Inc.
  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 log
  15. import (
  16. "bytes"
  17. "fmt"
  18. "log"
  19. "sync"
  20. )
  21. // Logger provides an abstract interface for logging from Reporters.
  22. // Applications can provide their own implementation of this interface to adapt
  23. // reporters logging to whatever logging library they prefer (stdlib log,
  24. // logrus, go-logging, etc).
  25. type Logger interface {
  26. // Error logs a message at error priority
  27. Error(msg string)
  28. // Infof logs a message at info priority
  29. Infof(msg string, args ...interface{})
  30. }
  31. // StdLogger is implementation of the Logger interface that delegates to default `log` package
  32. var StdLogger = &stdLogger{}
  33. type stdLogger struct{}
  34. func (l *stdLogger) Error(msg string) {
  35. log.Printf("ERROR: %s", msg)
  36. }
  37. // Infof logs a message at info priority
  38. func (l *stdLogger) Infof(msg string, args ...interface{}) {
  39. log.Printf(msg, args...)
  40. }
  41. // Debugf logs a message at debug priority
  42. func (l *stdLogger) Debugf(msg string, args ...interface{}) {
  43. log.Printf(fmt.Sprintf("DEBUG: %s", msg), args...)
  44. }
  45. // NullLogger is implementation of the Logger interface that is no-op
  46. var NullLogger = &nullLogger{}
  47. type nullLogger struct{}
  48. func (l *nullLogger) Error(msg string) {}
  49. func (l *nullLogger) Infof(msg string, args ...interface{}) {}
  50. func (l *nullLogger) Debugf(msg string, args ...interface{}) {}
  51. // BytesBufferLogger implements Logger backed by a bytes.Buffer.
  52. type BytesBufferLogger struct {
  53. mux sync.Mutex
  54. buf bytes.Buffer
  55. }
  56. // Error implements Logger.
  57. func (l *BytesBufferLogger) Error(msg string) {
  58. l.mux.Lock()
  59. l.buf.WriteString(fmt.Sprintf("ERROR: %s\n", msg))
  60. l.mux.Unlock()
  61. }
  62. // Infof implements Logger.
  63. func (l *BytesBufferLogger) Infof(msg string, args ...interface{}) {
  64. l.mux.Lock()
  65. l.buf.WriteString("INFO: " + fmt.Sprintf(msg, args...) + "\n")
  66. l.mux.Unlock()
  67. }
  68. // Debugf implements Logger.
  69. func (l *BytesBufferLogger) Debugf(msg string, args ...interface{}) {
  70. l.mux.Lock()
  71. l.buf.WriteString("DEBUG: " + fmt.Sprintf(msg, args...) + "\n")
  72. l.mux.Unlock()
  73. }
  74. // String returns string representation of the underlying buffer.
  75. func (l *BytesBufferLogger) String() string {
  76. l.mux.Lock()
  77. defer l.mux.Unlock()
  78. return l.buf.String()
  79. }
  80. // Flush empties the underlying buffer.
  81. func (l *BytesBufferLogger) Flush() {
  82. l.mux.Lock()
  83. defer l.mux.Unlock()
  84. l.buf.Reset()
  85. }
  86. // DebugLogger is an interface which adds a debug logging level
  87. type DebugLogger interface {
  88. Logger
  89. // Debugf logs a message at debug priority
  90. Debugf(msg string, args ...interface{})
  91. }
  92. // DebugLogAdapter is a log adapter that converts a Logger into a DebugLogger
  93. // If the provided Logger doesn't satisfy the interface, a logger with debug
  94. // disabled is returned
  95. func DebugLogAdapter(logger Logger) DebugLogger {
  96. if logger == nil {
  97. return nil
  98. }
  99. if debugLogger, ok := logger.(DebugLogger); ok {
  100. return debugLogger
  101. }
  102. logger.Infof("debug logging disabled")
  103. return debugDisabledLogAdapter{logger: logger}
  104. }
  105. type debugDisabledLogAdapter struct {
  106. logger Logger
  107. }
  108. func (d debugDisabledLogAdapter) Error(msg string) {
  109. d.logger.Error(msg)
  110. }
  111. func (d debugDisabledLogAdapter) Infof(msg string, args ...interface{}) {
  112. d.logger.Infof(msg, args...)
  113. }
  114. // Debugf is a nop
  115. func (d debugDisabledLogAdapter) Debugf(msg string, args ...interface{}) {
  116. }