2022-07-17 18:16:00 +00:00
|
|
|
// Copyright (C) 2019-2022 Nicola Murino
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published
|
|
|
|
// by the Free Software Foundation, version 3.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
2021-07-11 13:26:51 +00:00
|
|
|
package logger
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/hashicorp/go-hclog"
|
2022-01-05 10:37:45 +00:00
|
|
|
"github.com/rs/zerolog"
|
2021-07-11 13:26:51 +00:00
|
|
|
)
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// HCLogAdapter is an adapter for hclog.Logger
|
2021-07-11 13:26:51 +00:00
|
|
|
type HCLogAdapter struct {
|
|
|
|
hclog.Logger
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Log emits a message and key/value pairs at a provided log level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Log(level hclog.Level, msg string, args ...any) {
|
2022-01-05 10:37:45 +00:00
|
|
|
var ev *zerolog.Event
|
|
|
|
switch level {
|
|
|
|
case hclog.Info:
|
|
|
|
ev = logger.Info()
|
|
|
|
case hclog.Warn:
|
|
|
|
ev = logger.Warn()
|
|
|
|
case hclog.Error:
|
|
|
|
ev = logger.Error()
|
|
|
|
default:
|
|
|
|
ev = logger.Debug()
|
|
|
|
}
|
|
|
|
ev.Timestamp().Str("sender", l.Name())
|
|
|
|
addKeysAndValues(ev, args...)
|
|
|
|
ev.Msg(msg)
|
2021-07-11 13:26:51 +00:00
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Trace emits a message and key/value pairs at the TRACE level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Trace(msg string, args ...any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
l.Log(hclog.Debug, msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Debug emits a message and key/value pairs at the DEBUG level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Debug(msg string, args ...any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
l.Log(hclog.Debug, msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Info emits a message and key/value pairs at the INFO level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Info(msg string, args ...any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
l.Log(hclog.Info, msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Warn emits a message and key/value pairs at the WARN level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Warn(msg string, args ...any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
l.Log(hclog.Warn, msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Error emits a message and key/value pairs at the ERROR level
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) Error(msg string, args ...any) {
|
2021-07-11 13:26:51 +00:00
|
|
|
l.Log(hclog.Error, msg, args...)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// With creates a sub-logger
|
2022-05-19 17:49:51 +00:00
|
|
|
func (l *HCLogAdapter) With(args ...any) hclog.Logger {
|
2021-07-11 13:26:51 +00:00
|
|
|
return &HCLogAdapter{Logger: l.Logger.With(args...)}
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// Named creates a logger that will prepend the name string on the front of all messages
|
2021-07-11 13:26:51 +00:00
|
|
|
func (l *HCLogAdapter) Named(name string) hclog.Logger {
|
|
|
|
return &HCLogAdapter{Logger: l.Logger.Named(name)}
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// StandardLogger returns a value that conforms to the stdlib log.Logger interface
|
2021-07-11 13:26:51 +00:00
|
|
|
func (l *HCLogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
|
|
|
|
return log.New(&StdLoggerWrapper{Sender: l.Name()}, "", 0)
|
|
|
|
}
|
|
|
|
|
2021-11-27 16:04:13 +00:00
|
|
|
// StandardWriter returns a value that conforms to io.Writer, which can be passed into log.SetOutput()
|
2021-07-11 13:26:51 +00:00
|
|
|
func (l *HCLogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
|
|
|
|
return &StdLoggerWrapper{Sender: l.Name()}
|
|
|
|
}
|