log.go 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. package log
  2. import (
  3. "fmt"
  4. "os"
  5. "strconv"
  6. )
  7. // Why the interface? We may only want to print to STDOUT and STDERR for now,
  8. // but it won't neccessarily be that way forever. This interface is intended
  9. // to provide a "framework" for a variety of different logging types in the
  10. // future (log to file, log to logstash, etc.) There could be a driver model
  11. // similar to what is done with OS or machine providers.
  12. type Logger interface {
  13. Debug(...interface{})
  14. Debugf(string, ...interface{})
  15. Error(...interface{})
  16. Errorf(string, ...interface{})
  17. Errorln(...interface{})
  18. Info(...interface{})
  19. Infof(string, ...interface{})
  20. Infoln(...interface{})
  21. Fatal(...interface{})
  22. Fatalf(string, ...interface{})
  23. Print(...interface{})
  24. Printf(string, ...interface{})
  25. Warn(...interface{})
  26. Warnf(string, ...interface{})
  27. WithFields(Fields) Logger
  28. }
  29. var (
  30. l = TerminalLogger{}
  31. )
  32. // TODO: I think this is superflous and can be replaced by one check for if
  33. // debug is on that sets a variable in this module.
  34. func isDebug() bool {
  35. debugEnv := os.Getenv("DEBUG")
  36. if debugEnv != "" {
  37. showDebug, err := strconv.ParseBool(debugEnv)
  38. if err != nil {
  39. fmt.Fprintln(os.Stderr, "Error parsing boolean value from DEBUG: %s", err)
  40. os.Exit(1)
  41. }
  42. return showDebug
  43. }
  44. return false
  45. }
  46. type Fields map[string]interface{}
  47. func Debug(args ...interface{}) {
  48. l.Debug(args...)
  49. }
  50. func Debugf(fmtString string, args ...interface{}) {
  51. l.Debugf(fmtString, args...)
  52. }
  53. func Error(args ...interface{}) {
  54. l.Error(args...)
  55. }
  56. func Errorf(fmtString string, args ...interface{}) {
  57. l.Errorf(fmtString, args...)
  58. }
  59. func Errorln(args ...interface{}) {
  60. l.Errorln(args...)
  61. }
  62. func Info(args ...interface{}) {
  63. l.Info(args...)
  64. }
  65. func Infof(fmtString string, args ...interface{}) {
  66. l.Infof(fmtString, args...)
  67. }
  68. func Infoln(args ...interface{}) {
  69. l.Infoln(args...)
  70. }
  71. func Fatal(args ...interface{}) {
  72. l.Fatal(args...)
  73. }
  74. func Fatalf(fmtString string, args ...interface{}) {
  75. l.Fatalf(fmtString, args...)
  76. }
  77. func Print(args ...interface{}) {
  78. l.Print(args...)
  79. }
  80. func Printf(fmtString string, args ...interface{}) {
  81. l.Printf(fmtString, args...)
  82. }
  83. func Warn(args ...interface{}) {
  84. l.Warn(args...)
  85. }
  86. func Warnf(fmtString string, args ...interface{}) {
  87. l.Warnf(fmtString, args...)
  88. }
  89. func WithField(fieldName string, field interface{}) Logger {
  90. return l.WithFields(Fields{
  91. fieldName: field,
  92. })
  93. }
  94. func WithFields(fields Fields) Logger {
  95. return l.WithFields(fields)
  96. }