Yet another Go logging library for user-friendly applications, with colors!

formatter.go 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package log
  2. import (
  3. "bytes"
  4. "fmt"
  5. "text/template"
  6. "time"
  7. "git.kirsle.net/go/log/ansi"
  8. )
  9. // Convenient log formats to use in your logger.
  10. const (
  11. // DefaultFormat: shows the date in the secondary (dark) color, the label
  12. // in the bright color, and the message text in the normal color.
  13. DefaultFormat = `{{.Secondary}}{{.Time}}{{.Reset}} {{.Primary}}[{{.Level}}]{{.Reset}} {{.Message}}`
  14. // ColorfulFormat: like the DefaultFormat, but the message itself is also
  15. // colored using the secondary color.
  16. ColorfulFormat = `{{.Secondary}}{{.Time}}{{.Reset}} {{.Primary}}[{{.Level}}]{{.Reset}} {{.Secondary}}{{.Message}}{{.Reset}}`
  17. )
  18. // Convenient time formats to use in your logger.
  19. const (
  20. // DefaultTime is the default, in `yyyy-mm-dd hh:mm:ss` format.
  21. DefaultTime = `2006-01-02 15:04:05`
  22. // FriendlyTime is a human readable `Jan 2 15:04:05 2006` format.
  23. FriendlyTime = `Jan 2 15:04:05 2006`
  24. )
  25. // formatter provides the variables that can be used in the log format.
  26. type formatter struct {
  27. Time string
  28. Level string
  29. Message string
  30. Primary string
  31. Secondary string
  32. Reset string
  33. }
  34. // Format and return a log message.
  35. func (l *Logger) Format(level logLevel, tmpl string, args ...interface{}) string {
  36. // Prepare the variables to apply to the log message format.
  37. format := formatter{
  38. Time: time.Now().Format(l.Config.TimeFormat),
  39. Level: levelNames[level],
  40. Message: fmt.Sprintf(tmpl, args...),
  41. }
  42. // Find the theme color to use.
  43. if interactive && l.Config.Colors != NoColor {
  44. var (
  45. primary ThemeColor
  46. secondary ThemeColor
  47. )
  48. switch level {
  49. case DebugLevel:
  50. primary = l.Config.Theme.Debug
  51. secondary = l.Config.Theme.DebugSecondary
  52. case InfoLevel:
  53. primary = l.Config.Theme.Info
  54. secondary = l.Config.Theme.InfoSecondary
  55. case WarnLevel:
  56. primary = l.Config.Theme.Warn
  57. secondary = l.Config.Theme.WarnSecondary
  58. case ErrorLevel:
  59. primary = l.Config.Theme.Error
  60. secondary = l.Config.Theme.ErrorSecondary
  61. }
  62. // What color level are we supporting?
  63. if l.Config.Colors == ANSIColor {
  64. format.Primary = fmt.Sprintf("\x1B[%sm", primary.ANSI)
  65. format.Secondary = fmt.Sprintf("\x1B[%sm", secondary.ANSI)
  66. format.Reset = fmt.Sprintf("\x1B[%sm", ansi.Reset)
  67. } else if l.Config.Colors == ExtendedColor {
  68. format.Primary = fmt.Sprintf("\x1B[%sm", primary.Extended)
  69. format.Secondary = fmt.Sprintf("\x1B[%sm", secondary.Extended)
  70. format.Reset = fmt.Sprintf("\x1B[%sm", ansi.Reset)
  71. }
  72. }
  73. // Do we have the template cached?
  74. if l.template == nil {
  75. template, err := template.New("golog").Parse(l.Config.Format)
  76. if err != nil {
  77. return fmt.Sprintf("[GoLog format error: %s]", err)
  78. }
  79. l.template = template
  80. }
  81. // Evaluate the template.
  82. var buf bytes.Buffer
  83. err := l.template.Execute(&buf, format)
  84. if err != nil {
  85. return fmt.Sprintf("[GoLog template error: %s]", err)
  86. }
  87. return buf.String()
  88. }