logger.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. package logger
  2. import (
  3. "fmt"
  4. "os"
  5. terminal "github.com/mudler/go-isterminal"
  6. "github.com/ipfs/go-log"
  7. "github.com/pterm/pterm"
  8. )
  9. var _ log.StandardLogger = &Logger{}
  10. type Logger struct {
  11. level log.LogLevel
  12. }
  13. func New(lvl log.LogLevel) *Logger {
  14. if !terminal.IsTerminal(os.Stdout) {
  15. pterm.DisableColor()
  16. }
  17. if lvl == log.LevelDebug {
  18. pterm.EnableDebugMessages()
  19. }
  20. return &Logger{level: lvl}
  21. }
  22. func joinMsg(args ...interface{}) (message string) {
  23. for _, m := range args {
  24. message += " " + fmt.Sprintf("%v", m)
  25. }
  26. return
  27. }
  28. func (l Logger) enabled(lvl log.LogLevel) bool {
  29. return lvl >= l.level
  30. }
  31. func (l Logger) Debug(args ...interface{}) {
  32. if l.enabled(log.LevelDebug) {
  33. pterm.Debug.Println(joinMsg(args...))
  34. }
  35. }
  36. func (l Logger) Debugf(f string, args ...interface{}) {
  37. if l.enabled(log.LevelDebug) {
  38. pterm.Debug.Printfln(f, args...)
  39. }
  40. }
  41. func (l Logger) Error(args ...interface{}) {
  42. if l.enabled(log.LevelError) {
  43. pterm.Error.Println(pterm.LightRed(joinMsg(args...)))
  44. }
  45. }
  46. func (l Logger) Errorf(f string, args ...interface{}) {
  47. if l.enabled(log.LevelError) {
  48. pterm.Error.Printfln(pterm.LightRed(f), args...)
  49. }
  50. }
  51. func (l Logger) Fatal(args ...interface{}) {
  52. if l.enabled(log.LevelFatal) {
  53. pterm.Fatal.Println(pterm.Red(joinMsg(args...)))
  54. }
  55. }
  56. func (l Logger) Fatalf(f string, args ...interface{}) {
  57. if l.enabled(log.LevelFatal) {
  58. pterm.Fatal.Printfln(pterm.Red(f), args...)
  59. }
  60. }
  61. func (l Logger) Info(args ...interface{}) {
  62. if l.enabled(log.LevelInfo) {
  63. pterm.Info.Println(pterm.LightBlue(joinMsg(args...)))
  64. }
  65. }
  66. func (l Logger) Infof(f string, args ...interface{}) {
  67. if l.enabled(log.LevelInfo) {
  68. pterm.Info.Printfln(pterm.LightBlue(f), args...)
  69. }
  70. }
  71. func (l Logger) Panic(args ...interface{}) {
  72. l.Fatal(args...)
  73. }
  74. func (l Logger) Panicf(f string, args ...interface{}) {
  75. l.Fatalf(f, args...)
  76. }
  77. func (l Logger) Warn(args ...interface{}) {
  78. if l.enabled(log.LevelWarn) {
  79. pterm.Warning.Println(pterm.LightYellow(joinMsg(args...)))
  80. }
  81. }
  82. func (l Logger) Warnf(f string, args ...interface{}) {
  83. if l.enabled(log.LevelWarn) {
  84. pterm.Warning.Printfln(pterm.LightYellow(f), args...)
  85. }
  86. }
  87. func (l Logger) Warning(args ...interface{}) {
  88. l.Warn(args...)
  89. }
  90. func (l Logger) Warningf(f string, args ...interface{}) {
  91. l.Warnf(f, args...)
  92. }