/
plugins.go
53 lines (46 loc) · 1.35 KB
/
plugins.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
package portercontext
import (
"encoding/json"
"github.com/hashicorp/go-hclog"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// makePluginLogger creates a logger suitable for plugins to communicate with the hashicorp
// go-plugin framework, using hclog to talk over stderr
func (c *Context) makePluginLogger(pluginKey string, cfg LogConfiguration) zapcore.Core {
pluginLogger := zapToHclog{
hclog.New(&hclog.LoggerOptions{
Name: pluginKey,
Output: c.Err,
Level: hclog.Debug,
JSONFormat: true,
}),
}
enc := zap.NewProductionEncoderConfig()
jsonEncoder := zapcore.NewJSONEncoder(enc)
return zapcore.NewCore(jsonEncoder, zapcore.AddSync(pluginLogger), cfg.LogLevel)
}
// Accepts zap log commands and translates them to a format that hclog understands
// so that the plugin doesn't write any log messages that would cause the go plugin
// framework to error out (i.e. printing directly to stderr)
type zapToHclog struct {
logger hclog.Logger
}
func (z zapToHclog) Write(p []byte) (n int, err error) {
var entry map[string]interface{}
if err := json.Unmarshal(p, &entry); err != nil {
return 0, err
}
msg := entry["msg"].(string)
switch entry["level"].(string) {
case "error":
z.logger.Error(msg)
case "warn":
z.logger.Warn(msg)
case "debug":
z.logger.Debug(msg)
default:
z.logger.Info(msg)
}
return len(p), nil
}