-
-
Notifications
You must be signed in to change notification settings - Fork 759
/
hclog.go
96 lines (82 loc) · 2.94 KB
/
hclog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// Copyright (C) 2019 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/>.
package logger
import (
"io"
"log"
"github.com/hashicorp/go-hclog"
"github.com/rs/zerolog"
)
// HCLogAdapter is an adapter for hclog.Logger
type HCLogAdapter struct {
hclog.Logger
}
// Log emits a message and key/value pairs at a provided log level
func (l *HCLogAdapter) Log(level hclog.Level, msg string, args ...any) {
// Workaround to avoid logging plugin arguments that may contain sensitive data.
// Check everytime we update go-plugin library.
if msg == "starting plugin" {
return
}
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)
}
// Trace emits a message and key/value pairs at the TRACE level
func (l *HCLogAdapter) Trace(msg string, args ...any) {
l.Log(hclog.Debug, msg, args...)
}
// Debug emits a message and key/value pairs at the DEBUG level
func (l *HCLogAdapter) Debug(msg string, args ...any) {
l.Log(hclog.Debug, msg, args...)
}
// Info emits a message and key/value pairs at the INFO level
func (l *HCLogAdapter) Info(msg string, args ...any) {
l.Log(hclog.Info, msg, args...)
}
// Warn emits a message and key/value pairs at the WARN level
func (l *HCLogAdapter) Warn(msg string, args ...any) {
l.Log(hclog.Warn, msg, args...)
}
// Error emits a message and key/value pairs at the ERROR level
func (l *HCLogAdapter) Error(msg string, args ...any) {
l.Log(hclog.Error, msg, args...)
}
// With creates a sub-logger
func (l *HCLogAdapter) With(args ...any) hclog.Logger {
return &HCLogAdapter{Logger: l.Logger.With(args...)}
}
// Named creates a logger that will prepend the name string on the front of all messages
func (l *HCLogAdapter) Named(name string) hclog.Logger {
return &HCLogAdapter{Logger: l.Logger.Named(name)}
}
// StandardLogger returns a value that conforms to the stdlib log.Logger interface
func (l *HCLogAdapter) StandardLogger(_ *hclog.StandardLoggerOptions) *log.Logger {
return log.New(&StdLoggerWrapper{Sender: l.Name()}, "", 0)
}
// StandardWriter returns a value that conforms to io.Writer, which can be passed into log.SetOutput()
func (l *HCLogAdapter) StandardWriter(_ *hclog.StandardLoggerOptions) io.Writer {
return &StdLoggerWrapper{Sender: l.Name()}
}