logger.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package aws
  2. import (
  3. "log"
  4. "os"
  5. )
  6. // A LogLevelType defines the level logging should be performed at. Used to instruct
  7. // the SDK which statements should be logged.
  8. type LogLevelType uint
  9. // LogLevel returns the pointer to a LogLevel. Should be used to workaround
  10. // not being able to take the address of a non-composite literal.
  11. func LogLevel(l LogLevelType) *LogLevelType {
  12. return &l
  13. }
  14. // Value returns the LogLevel value or the default value LogOff if the LogLevel
  15. // is nil. Safe to use on nil value LogLevelTypes.
  16. func (l *LogLevelType) Value() LogLevelType {
  17. if l != nil {
  18. return *l
  19. }
  20. return LogOff
  21. }
  22. // Matches returns true if the v LogLevel is enabled by this LogLevel. Should be
  23. // used with logging sub levels. Is safe to use on nil value LogLevelTypes. If
  24. // LogLevel is nil, will default to LogOff comparison.
  25. func (l *LogLevelType) Matches(v LogLevelType) bool {
  26. c := l.Value()
  27. return c&v == v
  28. }
  29. // AtLeast returns true if this LogLevel is at least high enough to satisfies v.
  30. // Is safe to use on nil value LogLevelTypes. If LogLevel is nil, will default
  31. // to LogOff comparison.
  32. func (l *LogLevelType) AtLeast(v LogLevelType) bool {
  33. c := l.Value()
  34. return c >= v
  35. }
  36. const (
  37. // LogOff states that no logging should be performed by the SDK. This is the
  38. // default state of the SDK, and should be use to disable all logging.
  39. LogOff LogLevelType = iota * 0x1000
  40. // LogDebug state that debug output should be logged by the SDK. This should
  41. // be used to inspect request made and responses received.
  42. LogDebug
  43. )
  44. // Debug Logging Sub Levels
  45. const (
  46. // LogDebugWithSigning states that the SDK should log request signing and
  47. // presigning events. This should be used to log the signing details of
  48. // requests for debugging. Will also enable LogDebug.
  49. LogDebugWithSigning LogLevelType = LogDebug | (1 << iota)
  50. // LogDebugWithHTTPBody states the SDK should log HTTP request and response
  51. // HTTP bodys in addition to the headers and path. This should be used to
  52. // see the body content of requests and responses made while using the SDK
  53. // Will also enable LogDebug.
  54. LogDebugWithHTTPBody
  55. // LogDebugWithRequestRetries states the SDK should log when service requests will
  56. // be retried. This should be used to log when you want to log when service
  57. // requests are being retried. Will also enable LogDebug.
  58. LogDebugWithRequestRetries
  59. // LogDebugWithRequestErrors states the SDK should log when service requests fail
  60. // to build, send, validate, or unmarshal.
  61. LogDebugWithRequestErrors
  62. )
  63. // A Logger is a minimalistic interface for the SDK to log messages to. Should
  64. // be used to provide custom logging writers for the SDK to use.
  65. type Logger interface {
  66. Log(...interface{})
  67. }
  68. // A LoggerFunc is a convenience type to convert a function taking a variadic
  69. // list of arguments and wrap it so the Logger interface can be used.
  70. //
  71. // Example:
  72. // s3.New(sess, &aws.Config{Logger: aws.LoggerFunc(func(args ...interface{}) {
  73. // fmt.Fprintln(os.Stdout, args...)
  74. // })})
  75. type LoggerFunc func(...interface{})
  76. // Log calls the wrapped function with the arguments provided
  77. func (f LoggerFunc) Log(args ...interface{}) {
  78. f(args...)
  79. }
  80. // NewDefaultLogger returns a Logger which will write log messages to stdout, and
  81. // use same formatting runes as the stdlib log.Logger
  82. func NewDefaultLogger() Logger {
  83. return &defaultLogger{
  84. logger: log.New(os.Stdout, "", log.LstdFlags),
  85. }
  86. }
  87. // A defaultLogger provides a minimalistic logger satisfying the Logger interface.
  88. type defaultLogger struct {
  89. logger *log.Logger
  90. }
  91. // Log logs the parameters to the stdlib logger. See log.Println.
  92. func (l defaultLogger) Log(args ...interface{}) {
  93. l.logger.Println(args...)
  94. }