formatter.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package logutil
  2. import (
  3. "fmt"
  4. "strings"
  5. "devt.de/krotik/common/testutil"
  6. "devt.de/krotik/common/timeutil"
  7. )
  8. /*
  9. Formatter is used to format log messages.
  10. */
  11. type Formatter interface {
  12. /*
  13. Format formats a given log message into a string.
  14. */
  15. Format(level Level, scope string, msg ...interface{}) string
  16. }
  17. /*
  18. ConsoleFormatter returns a simple formatter which does a simple fmt.Sprintln
  19. on all log messages. It only adds the log level.
  20. */
  21. func ConsoleFormatter() Formatter {
  22. return &consoleFormatter{}
  23. }
  24. /*
  25. consoleFormatter is the console formatter implementation.
  26. */
  27. type consoleFormatter struct {
  28. }
  29. /*
  30. Format formats a given log message into a string.
  31. */
  32. func (sf *consoleFormatter) Format(level Level, scope string, msg ...interface{}) string {
  33. return fmt.Sprintln(fmt.Sprintf("%v:", level), fmt.Sprint(msg...))
  34. }
  35. /*
  36. SimpleFormatter returns a simple formatter which does a simple fmt.Sprintln
  37. on all log messages. It also adds a current timestamp, the message scope and
  38. log level.
  39. */
  40. func SimpleFormatter() Formatter {
  41. return &simpleFormatter{timeutil.MakeTimestamp}
  42. }
  43. /*
  44. simpleFormatter is the simple formatter implementation.
  45. */
  46. type simpleFormatter struct {
  47. tsFunc func() string // Timestamp function
  48. }
  49. /*
  50. Format formats a given log message into a string.
  51. */
  52. func (sf *simpleFormatter) Format(level Level, scope string, msg ...interface{}) string {
  53. if scope == "" {
  54. return fmt.Sprintln(sf.tsFunc(), level, fmt.Sprint(msg...))
  55. }
  56. return fmt.Sprintln(sf.tsFunc(), level, scope, fmt.Sprint(msg...))
  57. }
  58. /*
  59. TemplateFormatter returns a formatter which produces log messages according to
  60. a given template string. The template string may contain one or more of the
  61. following directives:
  62. %s The scope of the log message
  63. %l The level of the log message
  64. %t Current timestamp (milliseconds elapsed since January 1, 1970 UTC)
  65. %f Function in which the log message was issued e.g. foo.bar.MyFunc()
  66. %c Code location of the log statement which issuing the log message e.g. package/somefile.go:12
  67. %m The log message and its arguments formatted with fmt.Sprintf()
  68. */
  69. func TemplateFormatter(template string) Formatter {
  70. return &templateFormatter{template, timeutil.MakeTimestamp}
  71. }
  72. /*
  73. templateFormatter is the template formatter implementation.
  74. */
  75. type templateFormatter struct {
  76. template string // Template for a log message
  77. tsFunc func() string // Timestamp function
  78. }
  79. /*
  80. Format formats a given log message into a string.
  81. */
  82. func (sf *templateFormatter) Format(level Level, scope string, msg ...interface{}) string {
  83. name, loc := testutil.GetCaller(2)
  84. out := sf.template
  85. out = strings.Replace(out, "%s", scope, -1)
  86. out = strings.Replace(out, "%l", fmt.Sprint(level), -1)
  87. out = strings.Replace(out, "%t", sf.tsFunc(), -1)
  88. out = strings.Replace(out, "%f", name, -1)
  89. out = strings.Replace(out, "%c", loc, -1)
  90. out = strings.Replace(out, "%m", fmt.Sprint(msg...), -1)
  91. return fmt.Sprintln(out)
  92. }