PageRenderTime 65ms CodeModel.GetById 32ms RepoModel.GetById 1ms app.codeStats 0ms

/log.go

https://github.com/Letbeauty/cow
Go | 128 lines | 106 code | 19 blank | 3 comment | 15 complexity | 0a0589b3a4875037f379e222a0e5fdef MD5 | raw file
Possible License(s): BSD-2-Clause
  1. package main
  2. // This logging trick is learnt from a post by Rob Pike
  3. // https://groups.google.com/d/msg/golang-nuts/gU7oQGoCkmg/j3nNxuS2O_sJ
  4. import (
  5. "flag"
  6. "fmt"
  7. "github.com/cyfdecyf/color"
  8. "io"
  9. "log"
  10. "os"
  11. )
  12. type infoLogging bool
  13. type debugLogging bool
  14. type errorLogging bool
  15. type requestLogging bool
  16. type responseLogging bool
  17. var (
  18. info infoLogging
  19. debug debugLogging
  20. errl errorLogging
  21. dbgRq requestLogging
  22. dbgRep responseLogging
  23. logFile io.Writer
  24. // make sure logger can be called before initLog
  25. errorLog = log.New(os.Stdout, "", log.LstdFlags)
  26. debugLog = errorLog
  27. requestLog = errorLog
  28. responseLog = errorLog
  29. verbose bool
  30. colorize bool
  31. )
  32. func init() {
  33. flag.BoolVar((*bool)(&info), "info", true, "info log")
  34. flag.BoolVar((*bool)(&debug), "debug", false, "debug log, with this option, log goes to stdout with color")
  35. flag.BoolVar((*bool)(&errl), "err", true, "error log")
  36. flag.BoolVar((*bool)(&dbgRq), "request", false, "request log")
  37. flag.BoolVar((*bool)(&dbgRep), "reply", false, "reply log")
  38. flag.BoolVar(&verbose, "v", false, "more info in request/response logging")
  39. flag.BoolVar(&colorize, "color", false, "colorize log output")
  40. }
  41. func initLog() {
  42. logFile = os.Stdout
  43. if config.LogFile != "" {
  44. if f, err := os.OpenFile(expandTilde(config.LogFile),
  45. os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0600); err != nil {
  46. fmt.Printf("Can't open log file, logging to stdout: %v\n", err)
  47. } else {
  48. logFile = f
  49. }
  50. }
  51. log.SetOutput(logFile)
  52. if colorize {
  53. color.SetDefaultColor(color.ANSI)
  54. } else {
  55. color.SetDefaultColor(color.NoColor)
  56. }
  57. errorLog = log.New(logFile, color.Red("[ERROR] "), log.LstdFlags)
  58. debugLog = log.New(logFile, color.Blue("[DEBUG] "), log.LstdFlags)
  59. requestLog = log.New(logFile, color.Green("[>>>>>] "), log.LstdFlags)
  60. responseLog = log.New(logFile, color.Yellow("[<<<<<] "), log.LstdFlags)
  61. }
  62. func (d infoLogging) Printf(format string, args ...interface{}) {
  63. if d {
  64. log.Printf(format, args...)
  65. }
  66. }
  67. func (d infoLogging) Println(args ...interface{}) {
  68. if d {
  69. log.Println(args...)
  70. }
  71. }
  72. func (d debugLogging) Printf(format string, args ...interface{}) {
  73. if d {
  74. debugLog.Printf(format, args...)
  75. }
  76. }
  77. func (d debugLogging) Println(args ...interface{}) {
  78. if d {
  79. debugLog.Println(args...)
  80. }
  81. }
  82. func (d errorLogging) Printf(format string, args ...interface{}) {
  83. if d {
  84. errorLog.Printf(format, args...)
  85. }
  86. }
  87. func (d errorLogging) Println(args ...interface{}) {
  88. if d {
  89. errorLog.Println(args...)
  90. }
  91. }
  92. func (d requestLogging) Printf(format string, args ...interface{}) {
  93. if d {
  94. requestLog.Printf(format, args...)
  95. }
  96. }
  97. func (d responseLogging) Printf(format string, args ...interface{}) {
  98. if d {
  99. responseLog.Printf(format, args...)
  100. }
  101. }
  102. func Fatal(args ...interface{}) {
  103. fmt.Println(args...)
  104. os.Exit(1)
  105. }
  106. func Fatalf(format string, args ...interface{}) {
  107. fmt.Printf(format, args...)
  108. os.Exit(1)
  109. }