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

config.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. package log
  2. import "io"
  3. // Config stores settings that control the logger's behavior.
  4. type Config struct {
  5. // Level is one of DebugLevel, InfoLevel, WarnLevel, ErrorLevel or FatalLevel.
  6. // Messages emitted by the logger must be 'at least' this level to be logged.
  7. Level logLevel
  8. // What colors are supported? Default is NoColor. Use ANSIColor to support
  9. // legacy terminal emulators, or ExtendedColor for modern 256-color support.
  10. Colors colorLevel
  11. // Which color theme are you using? The default is DarkTheme.
  12. Theme Theme
  13. // Where to write the log messages to? If not defined with a custom io.Writer,
  14. // the default goes to standard output for Debug and Info messages and
  15. // standard error for warnings, errors, and fatal messages.
  16. Writer *io.Writer
  17. // How do you want to format your log lines? This should be a Go text format
  18. // string, with the following variable placeholders:
  19. //
  20. // {{.Time}} inserts the date/time stamp for the log message.
  21. // {{.Level}} inserts a label for the log level, e.g. "INFO" or "WARN"
  22. // {{.Message}} inserts the text of the log message itself.
  23. // {{.Primary}} inserts the color sequence for the primary color based
  24. // on the log level for the message.
  25. // {{.Secondary}} inserts the color sequence for the secondary color.
  26. // {{.Reset}} inserts the 'reset' color sequence to stop coloring
  27. // the rest of the text that follows.
  28. //
  29. // The default log format is as follows:
  30. //
  31. // {{.Secondary}}{{.Time}}{{.Reset}} {{.Primary}}[{{.Level}}]{{.Reset}} {{.Message}}
  32. Format string
  33. // How do you want to format your time stamps? (The `{{.Time}}`). This uses
  34. // the Go `time` module, so the TimeFormat should use their reference date/time.
  35. // The default TimeFormat is: `2006-01-02 15:04:05`
  36. TimeFormat string
  37. }
  38. // DefaultConfig returns a Config with the default values filled in.
  39. func DefaultConfig() *Config {
  40. return &Config{
  41. Theme: DarkTheme,
  42. Format: DefaultFormat,
  43. TimeFormat: DefaultTime,
  44. }
  45. }
  46. // Configure applies the configuration to the logger. If any of the following
  47. // keys are not defined (or have zero-values), the default value for the key will
  48. // be used instead:
  49. //
  50. // Format
  51. // TimeFormat
  52. func (l *Logger) Configure(cfg *Config) {
  53. // Important keys and their defaults.
  54. if cfg.Format == "" {
  55. cfg.Format = DefaultFormat
  56. }
  57. if cfg.TimeFormat == "" {
  58. cfg.TimeFormat = DefaultTime
  59. }
  60. l.Config = cfg
  61. l.template = nil
  62. }