hclog.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 (
  16. "io"
  17. "log"
  18. "github.com/hashicorp/go-hclog"
  19. "github.com/rs/zerolog"
  20. )
  21. // HCLogAdapter is an adapter for hclog.Logger
  22. type HCLogAdapter struct {
  23. hclog.Logger
  24. }
  25. // Log emits a message and key/value pairs at a provided log level
  26. func (l *HCLogAdapter) Log(level hclog.Level, msg string, args ...any) {
  27. var ev *zerolog.Event
  28. switch level {
  29. case hclog.Info:
  30. ev = logger.Info()
  31. case hclog.Warn:
  32. ev = logger.Warn()
  33. case hclog.Error:
  34. ev = logger.Error()
  35. default:
  36. ev = logger.Debug()
  37. }
  38. ev.Timestamp().Str("sender", l.Name())
  39. addKeysAndValues(ev, args...)
  40. ev.Msg(msg)
  41. }
  42. // Trace emits a message and key/value pairs at the TRACE level
  43. func (l *HCLogAdapter) Trace(msg string, args ...any) {
  44. l.Log(hclog.Debug, msg, args...)
  45. }
  46. // Debug emits a message and key/value pairs at the DEBUG level
  47. func (l *HCLogAdapter) Debug(msg string, args ...any) {
  48. l.Log(hclog.Debug, msg, args...)
  49. }
  50. // Info emits a message and key/value pairs at the INFO level
  51. func (l *HCLogAdapter) Info(msg string, args ...any) {
  52. l.Log(hclog.Info, msg, args...)
  53. }
  54. // Warn emits a message and key/value pairs at the WARN level
  55. func (l *HCLogAdapter) Warn(msg string, args ...any) {
  56. l.Log(hclog.Warn, msg, args...)
  57. }
  58. // Error emits a message and key/value pairs at the ERROR level
  59. func (l *HCLogAdapter) Error(msg string, args ...any) {
  60. l.Log(hclog.Error, msg, args...)
  61. }
  62. // With creates a sub-logger
  63. func (l *HCLogAdapter) With(args ...any) hclog.Logger {
  64. return &HCLogAdapter{Logger: l.Logger.With(args...)}
  65. }
  66. // Named creates a logger that will prepend the name string on the front of all messages
  67. func (l *HCLogAdapter) Named(name string) hclog.Logger {
  68. return &HCLogAdapter{Logger: l.Logger.Named(name)}
  69. }
  70. // StandardLogger returns a value that conforms to the stdlib log.Logger interface
  71. func (l *HCLogAdapter) StandardLogger(_ *hclog.StandardLoggerOptions) *log.Logger {
  72. return log.New(&StdLoggerWrapper{Sender: l.Name()}, "", 0)
  73. }
  74. // StandardWriter returns a value that conforms to io.Writer, which can be passed into log.SetOutput()
  75. func (l *HCLogAdapter) StandardWriter(_ *hclog.StandardLoggerOptions) io.Writer {
  76. return &StdLoggerWrapper{Sender: l.Name()}
  77. }