2
0

logger.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright © 2021 Ettore Di Giacinto <[email protected]>
  2. //
  3. // This program is free software; you can redistribute it and/or modify
  4. // it under the terms of the GNU General Public License as published by
  5. // the Free Software Foundation; either version 2 of the License, or
  6. // (at your option) any later version.
  7. //
  8. // This program is distributed in the hope that it will be useful,
  9. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. // GNU General Public License for more details.
  12. //
  13. // You should have received a copy of the GNU General Public License along
  14. // with this program; if not, see <http://www.gnu.org/licenses/>.
  15. package logger
  16. import (
  17. "fmt"
  18. "os"
  19. terminal "github.com/mudler/go-isterminal"
  20. "github.com/ipfs/go-log"
  21. "github.com/pterm/pterm"
  22. )
  23. var _ log.StandardLogger = &Logger{}
  24. type Logger struct {
  25. level log.LogLevel
  26. }
  27. func New(lvl log.LogLevel) *Logger {
  28. if !terminal.IsTerminal(os.Stdout) {
  29. pterm.DisableColor()
  30. }
  31. if lvl == log.LevelDebug {
  32. pterm.EnableDebugMessages()
  33. }
  34. return &Logger{level: lvl}
  35. }
  36. func joinMsg(args ...interface{}) (message string) {
  37. for _, m := range args {
  38. message += " " + fmt.Sprintf("%v", m)
  39. }
  40. return
  41. }
  42. func (l Logger) enabled(lvl log.LogLevel) bool {
  43. return lvl >= l.level
  44. }
  45. func (l Logger) Debug(args ...interface{}) {
  46. if l.enabled(log.LevelDebug) {
  47. pterm.Debug.Println(joinMsg(args...))
  48. }
  49. }
  50. func (l Logger) Debugf(f string, args ...interface{}) {
  51. if l.enabled(log.LevelDebug) {
  52. pterm.Debug.Printfln(f, args...)
  53. }
  54. }
  55. func (l Logger) Error(args ...interface{}) {
  56. if l.enabled(log.LevelError) {
  57. pterm.Error.Println(pterm.LightRed(joinMsg(args...)))
  58. }
  59. }
  60. func (l Logger) Errorf(f string, args ...interface{}) {
  61. if l.enabled(log.LevelError) {
  62. pterm.Error.Printfln(pterm.LightRed(f), args...)
  63. }
  64. }
  65. func (l Logger) Fatal(args ...interface{}) {
  66. if l.enabled(log.LevelFatal) {
  67. pterm.Fatal.Println(pterm.Red(joinMsg(args...)))
  68. }
  69. }
  70. func (l Logger) Fatalf(f string, args ...interface{}) {
  71. if l.enabled(log.LevelFatal) {
  72. pterm.Fatal.Printfln(pterm.Red(f), args...)
  73. }
  74. }
  75. func (l Logger) Info(args ...interface{}) {
  76. if l.enabled(log.LevelInfo) {
  77. pterm.Info.Println(pterm.LightBlue(joinMsg(args...)))
  78. }
  79. }
  80. func (l Logger) Infof(f string, args ...interface{}) {
  81. if l.enabled(log.LevelInfo) {
  82. pterm.Info.Printfln(pterm.LightBlue(f), args...)
  83. }
  84. }
  85. func (l Logger) Panic(args ...interface{}) {
  86. l.Fatal(args...)
  87. }
  88. func (l Logger) Panicf(f string, args ...interface{}) {
  89. l.Fatalf(f, args...)
  90. }
  91. func (l Logger) Warn(args ...interface{}) {
  92. if l.enabled(log.LevelWarn) {
  93. pterm.Warning.Println(pterm.LightYellow(joinMsg(args...)))
  94. }
  95. }
  96. func (l Logger) Warnf(f string, args ...interface{}) {
  97. if l.enabled(log.LevelWarn) {
  98. pterm.Warning.Printfln(pterm.LightYellow(f), args...)
  99. }
  100. }
  101. func (l Logger) Warning(args ...interface{}) {
  102. l.Warn(args...)
  103. }
  104. func (l Logger) Warningf(f string, args ...interface{}) {
  105. l.Warnf(f, args...)
  106. }