log.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // Copyright (c) 2021 Tulir Asokan
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this
  5. // file, You can obtain one at http://mozilla.org/MPL/2.0/.
  6. // Package waLog contains a simple logger interface used by the other whatsmeow packages.
  7. package waLog
  8. import (
  9. "fmt"
  10. "strings"
  11. "time"
  12. )
  13. // Logger is a simple logger interface that can have subloggers for specific areas.
  14. type Logger interface {
  15. Warnf(msg string, args ...interface{})
  16. Errorf(msg string, args ...interface{})
  17. Infof(msg string, args ...interface{})
  18. Debugf(msg string, args ...interface{})
  19. Sub(module string) Logger
  20. }
  21. type noopLogger struct{}
  22. func (n *noopLogger) Errorf(_ string, _ ...interface{}) {}
  23. func (n *noopLogger) Warnf(_ string, _ ...interface{}) {}
  24. func (n *noopLogger) Infof(_ string, _ ...interface{}) {}
  25. func (n *noopLogger) Debugf(_ string, _ ...interface{}) {}
  26. func (n *noopLogger) Sub(_ string) Logger { return n }
  27. // Noop is a no-op Logger implementation that silently drops everything.
  28. var Noop Logger = &noopLogger{}
  29. type stdoutLogger struct {
  30. mod string
  31. color bool
  32. min int
  33. }
  34. var colors = map[string]string{
  35. "INFO": "\033[36m",
  36. "WARN": "\033[33m",
  37. "ERROR": "\033[31m",
  38. }
  39. var levelToInt = map[string]int{
  40. "": -1,
  41. "DEBUG": 0,
  42. "INFO": 1,
  43. "WARN": 2,
  44. "ERROR": 3,
  45. }
  46. func (s *stdoutLogger) outputf(level, msg string, args ...interface{}) {
  47. if levelToInt[level] < s.min {
  48. return
  49. }
  50. var colorStart, colorReset string
  51. if s.color {
  52. colorStart = colors[level]
  53. colorReset = "\033[0m"
  54. }
  55. fmt.Printf("%s%s [%s %s] %s%s\n", time.Now().Format("15:04:05.000"), colorStart, s.mod, level, fmt.Sprintf(msg, args...), colorReset)
  56. }
  57. func (s *stdoutLogger) Errorf(msg string, args ...interface{}) { s.outputf("ERROR", msg, args...) }
  58. func (s *stdoutLogger) Warnf(msg string, args ...interface{}) { s.outputf("WARN", msg, args...) }
  59. func (s *stdoutLogger) Infof(msg string, args ...interface{}) { s.outputf("INFO", msg, args...) }
  60. func (s *stdoutLogger) Debugf(msg string, args ...interface{}) { s.outputf("DEBUG", msg, args...) }
  61. func (s *stdoutLogger) Sub(mod string) Logger {
  62. return &stdoutLogger{mod: fmt.Sprintf("%s/%s", s.mod, mod), color: s.color, min: s.min}
  63. }
  64. // Stdout is a simple Logger implementation that outputs to stdout. The module name given is included in log lines.
  65. //
  66. // minLevel specifies the minimum log level to output. An empty string will output all logs.
  67. //
  68. // If color is true, then info, warn and error logs will be colored cyan, yellow and red respectively using ANSI color escape codes.
  69. func Stdout(module string, minLevel string, color bool) Logger {
  70. return &stdoutLogger{mod: module, color: color, min: levelToInt[strings.ToUpper(minLevel)]}
  71. }