1
0

lego.go 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright (C) 2019-2023 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. package logger
  15. import "fmt"
  16. const (
  17. legoLogSender = "lego"
  18. )
  19. // LegoAdapter is an adapter for lego.StdLogger
  20. type LegoAdapter struct {
  21. LogToConsole bool
  22. }
  23. // Fatal emits a log at Error level
  24. func (l *LegoAdapter) Fatal(args ...any) {
  25. if l.LogToConsole {
  26. ErrorToConsole(fmt.Sprint(args...))
  27. return
  28. }
  29. Log(LevelError, legoLogSender, "", fmt.Sprint(args...))
  30. }
  31. // Fatalln is the same as Fatal
  32. func (l *LegoAdapter) Fatalln(args ...any) {
  33. l.Fatal(args...)
  34. }
  35. // Fatalf emits a log at Error level
  36. func (l *LegoAdapter) Fatalf(format string, args ...any) {
  37. if l.LogToConsole {
  38. ErrorToConsole(format, args...)
  39. return
  40. }
  41. Log(LevelError, legoLogSender, "", format, args...)
  42. }
  43. // Print emits a log at Info level
  44. func (l *LegoAdapter) Print(args ...any) {
  45. if l.LogToConsole {
  46. InfoToConsole(fmt.Sprint(args...))
  47. return
  48. }
  49. Log(LevelInfo, legoLogSender, "", fmt.Sprint(args...))
  50. }
  51. // Println is the same as Print
  52. func (l *LegoAdapter) Println(args ...any) {
  53. l.Print(args...)
  54. }
  55. // Printf emits a log at Info level
  56. func (l *LegoAdapter) Printf(format string, args ...any) {
  57. if l.LogToConsole {
  58. InfoToConsole(format, args...)
  59. return
  60. }
  61. Log(LevelInfo, legoLogSender, "", format, args...)
  62. }