logger.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. // NullLogger is implementation of the Logger interface that is no-op
  42. var NullLogger = &nullLogger{}
  43. type nullLogger struct{}
  44. func (l *nullLogger) Error(msg string) {}
  45. func (l *nullLogger) Infof(msg string, args ...interface{}) {}
  46. // BytesBufferLogger implements Logger backed by a bytes.Buffer.
  47. type BytesBufferLogger struct {
  48. mux sync.Mutex
  49. buf bytes.Buffer
  50. }
  51. // Error implements Logger.
  52. func (l *BytesBufferLogger) Error(msg string) {
  53. l.mux.Lock()
  54. l.buf.WriteString(fmt.Sprintf("ERROR: %s\n", msg))
  55. l.mux.Unlock()
  56. }
  57. // Infof implements Logger.
  58. func (l *BytesBufferLogger) Infof(msg string, args ...interface{}) {
  59. l.mux.Lock()
  60. l.buf.WriteString("INFO: " + fmt.Sprintf(msg, args...) + "\n")
  61. l.mux.Unlock()
  62. }
  63. // String returns string representation of the underlying buffer.
  64. func (l *BytesBufferLogger) String() string {
  65. l.mux.Lock()
  66. defer l.mux.Unlock()
  67. return l.buf.String()
  68. }
  69. // Flush empties the underlying buffer.
  70. func (l *BytesBufferLogger) Flush() {
  71. l.mux.Lock()
  72. defer l.mux.Unlock()
  73. l.buf.Reset()
  74. }