generated from mattermost/mattermost-plugin-starter-template
/
plugin_logger.go
149 lines (126 loc) · 3.22 KB
/
plugin_logger.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
// Copyright (c) 2019-present Mattermost, Inc. All Rights Reserved.
// See License for license information.
package utils
import (
"fmt"
"strings"
"time"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
pluginapi "github.com/mattermost/mattermost-plugin-api"
"github.com/mattermost/mattermost-server/v6/model"
)
const pluginCallerKey = "plugin_caller"
type LogConfig struct {
BotUserID string
ChannelID string
Level zapcore.Level
IncludeJSON bool
}
type LogConfigGetter interface {
GetLogConfig() LogConfig
}
// plugin implements zapcore.Core interface to serve as a logging "backend" for
// SugaredLogger, using the plugin API log methods.
type plugin struct {
zapcore.LevelEnabler
poster *pluginapi.PostService
logger *pluginapi.LogService
confGetter LogConfigGetter
fields map[string]zapcore.Field
}
func NewPluginLogger(mmapi *pluginapi.Client, confGetter LogConfigGetter) Logger {
options := zap.AddCaller()
return &logger{
SugaredLogger: zap.New(&plugin{
poster: &mmapi.Post,
logger: &mmapi.Log,
LevelEnabler: zapcore.DebugLevel,
confGetter: confGetter,
}, options).Sugar(),
}
}
func (p *plugin) With(fields []zapcore.Field) zapcore.Core {
return p.with(fields)
}
func (p *plugin) with(fields []zapcore.Field) *plugin {
ff := map[string]zapcore.Field{}
for k, v := range p.fields {
ff[k] = v
}
for _, f := range fields {
ff[f.Key] = f
}
c := *p
c.fields = ff
return &c
}
func (p *plugin) Check(e zapcore.Entry, ce *zapcore.CheckedEntry) *zapcore.CheckedEntry {
return ce.AddCore(e, p)
}
func (p *plugin) Sync() error {
return nil
}
func (p *plugin) Write(e zapcore.Entry, fields []zapcore.Field) error {
caller := strings.TrimPrefix(e.Caller.FullPath(), "github.com/mattermost/mattermost-plugin-apps/")
if e.Caller.Defined {
fields = append(fields, zapcore.Field{
Key: pluginCallerKey,
Type: zapcore.StringType,
String: caller,
})
}
p = p.with(fields)
w := p.logger.Error
switch e.Level {
case zapcore.DebugLevel:
w = p.logger.Debug
case zapcore.InfoLevel:
w = p.logger.Info
case zapcore.WarnLevel:
w = p.logger.Warn
}
pairs := []interface{}{}
for k, f := range p.fields {
switch {
case f.Integer != 0:
pairs = append(pairs, k, f.Integer)
case f.String != "":
pairs = append(pairs, k, f.String)
default:
pairs = append(pairs, k, f.Interface)
}
}
w(e.Message, pairs...)
if p.confGetter == nil {
return nil
}
logconf := p.confGetter.GetLogConfig()
if logconf.ChannelID == "" || !logconf.Level.Enabled(e.Level) {
return nil
}
message := fmt.Sprintf("%s %s (%s): %s\n", e.Time.Format(time.StampMilli), e.Level.CapitalString(), caller, e.Message)
if logconf.IncludeJSON {
ccJSON := map[string]any{}
for i := 0; i < len(pairs); i += 2 {
key := pairs[i].(string)
// The caller is logged in message
if key != pluginCallerKey {
ccJSON[key] = pairs[i+1]
}
}
if len(ccJSON) > 0 {
message += JSONBlock(ccJSON)
}
}
logPost := &model.Post{
ChannelId: logconf.ChannelID,
UserId: logconf.BotUserID,
Message: message,
}
if err := p.poster.CreatePost(logPost); err != nil {
// Log directly to avoid loop
p.logger.Error("failed to post log message", "err", err.Error())
}
return nil
}