hclog.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright (C) 2019 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. // Workaround to avoid logging plugin arguments that may contain sensitive data.
  28. // Check everytime we update go-plugin library.
  29. if msg == "starting plugin" {
  30. return
  31. }
  32. var ev *zerolog.Event
  33. switch level {
  34. case hclog.Info:
  35. ev = logger.Info()
  36. case hclog.Warn:
  37. ev = logger.Warn()
  38. case hclog.Error:
  39. ev = logger.Error()
  40. default:
  41. ev = logger.Debug()
  42. }
  43. ev.Timestamp().Str("sender", l.Name())
  44. addKeysAndValues(ev, args...)
  45. ev.Msg(msg)
  46. }
  47. // Trace emits a message and key/value pairs at the TRACE level
  48. func (l *HCLogAdapter) Trace(msg string, args ...any) {
  49. l.Log(hclog.Debug, msg, args...)
  50. }
  51. // Debug emits a message and key/value pairs at the DEBUG level
  52. func (l *HCLogAdapter) Debug(msg string, args ...any) {
  53. l.Log(hclog.Debug, msg, args...)
  54. }
  55. // Info emits a message and key/value pairs at the INFO level
  56. func (l *HCLogAdapter) Info(msg string, args ...any) {
  57. l.Log(hclog.Info, msg, args...)
  58. }
  59. // Warn emits a message and key/value pairs at the WARN level
  60. func (l *HCLogAdapter) Warn(msg string, args ...any) {
  61. l.Log(hclog.Warn, msg, args...)
  62. }
  63. // Error emits a message and key/value pairs at the ERROR level
  64. func (l *HCLogAdapter) Error(msg string, args ...any) {
  65. l.Log(hclog.Error, msg, args...)
  66. }
  67. // With creates a sub-logger
  68. func (l *HCLogAdapter) With(args ...any) hclog.Logger {
  69. return &HCLogAdapter{Logger: l.Logger.With(args...)}
  70. }
  71. // Named creates a logger that will prepend the name string on the front of all messages
  72. func (l *HCLogAdapter) Named(name string) hclog.Logger {
  73. return &HCLogAdapter{Logger: l.Logger.Named(name)}
  74. }
  75. // StandardLogger returns a value that conforms to the stdlib log.Logger interface
  76. func (l *HCLogAdapter) StandardLogger(_ *hclog.StandardLoggerOptions) *log.Logger {
  77. return log.New(&StdLoggerWrapper{Sender: l.Name()}, "", 0)
  78. }
  79. // StandardWriter returns a value that conforms to io.Writer, which can be passed into log.SetOutput()
  80. func (l *HCLogAdapter) StandardWriter(_ *hclog.StandardLoggerOptions) io.Writer {
  81. return &StdLoggerWrapper{Sender: l.Name()}
  82. }