-
Notifications
You must be signed in to change notification settings - Fork 7.2k
/
hclog_adapter.go
133 lines (109 loc) · 2.86 KB
/
hclog_adapter.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"fmt"
"io"
"log"
"strings"
"github.com/hashicorp/go-hclog"
"github.com/mattermost/mattermost-server/v5/mlog"
)
type hclogAdapter struct {
wrappedLogger *mlog.Logger
extrasKey string
}
func (h *hclogAdapter) Log(level hclog.Level, msg string, args ...interface{}) {
switch level {
case hclog.Trace:
h.Trace(msg, args...)
case hclog.Debug:
h.Debug(msg, args...)
case hclog.Info:
h.Info(msg, args...)
case hclog.Warn:
h.Warn(msg, args...)
case hclog.Error:
h.Error(msg, args...)
default:
// For unknown/unexpected log level, treat it as an error so we notice and fix the code.
h.Error(msg, args...)
}
}
func (h *hclogAdapter) Trace(msg string, args ...interface{}) {
extras := strings.TrimSpace(fmt.Sprint(args...))
if extras != "" {
h.wrappedLogger.Debug(msg, mlog.String(h.extrasKey, extras))
} else {
h.wrappedLogger.Debug(msg)
}
}
func (h *hclogAdapter) Debug(msg string, args ...interface{}) {
extras := strings.TrimSpace(fmt.Sprint(args...))
if extras != "" {
h.wrappedLogger.Debug(msg, mlog.String(h.extrasKey, extras))
} else {
h.wrappedLogger.Debug(msg)
}
}
func (h *hclogAdapter) Info(msg string, args ...interface{}) {
extras := strings.TrimSpace(fmt.Sprint(args...))
if extras != "" {
h.wrappedLogger.Info(msg, mlog.String(h.extrasKey, extras))
} else {
h.wrappedLogger.Info(msg)
}
}
func (h *hclogAdapter) Warn(msg string, args ...interface{}) {
extras := strings.TrimSpace(fmt.Sprint(args...))
if extras != "" {
h.wrappedLogger.Warn(msg, mlog.String(h.extrasKey, extras))
} else {
h.wrappedLogger.Warn(msg)
}
}
func (h *hclogAdapter) Error(msg string, args ...interface{}) {
extras := strings.TrimSpace(fmt.Sprint(args...))
if extras != "" {
h.wrappedLogger.Error(msg, mlog.String(h.extrasKey, extras))
} else {
h.wrappedLogger.Error(msg)
}
}
func (h *hclogAdapter) IsTrace() bool {
return false
}
func (h *hclogAdapter) IsDebug() bool {
return true
}
func (h *hclogAdapter) IsInfo() bool {
return true
}
func (h *hclogAdapter) IsWarn() bool {
return true
}
func (h *hclogAdapter) IsError() bool {
return true
}
func (h *hclogAdapter) With(args ...interface{}) hclog.Logger {
return h
}
func (h *hclogAdapter) Named(name string) hclog.Logger {
return h
}
func (h *hclogAdapter) ResetNamed(name string) hclog.Logger {
return h
}
func (h *hclogAdapter) StandardLogger(opts *hclog.StandardLoggerOptions) *log.Logger {
return h.wrappedLogger.StdLog()
}
func (h *hclogAdapter) StandardWriter(opts *hclog.StandardLoggerOptions) io.Writer {
return h.wrappedLogger.StdLogWriter()
}
func (h *hclogAdapter) SetLevel(hclog.Level) {}
func (h *hclogAdapter) ImpliedArgs() []interface{} {
return []interface{}{}
}
func (h *hclogAdapter) Name() string {
return "MattermostPluginLogger"
}