/
logging.go
79 lines (68 loc) · 1.7 KB
/
logging.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
package component
import (
"context"
"time"
"github.com/davecgh/go-spew/spew"
"go.uber.org/zap"
platformConf "github.com/kiga-hub/arc/conf"
"github.com/kiga-hub/arc/logging"
"github.com/kiga-hub/arc/logging/conf"
"github.com/kiga-hub/arc/micro"
microConf "github.com/kiga-hub/arc/micro/conf"
)
// LoggingComponent is Component for logging
type LoggingComponent struct {
micro.EmptyComponent
zlog *zap.Logger
enable bool
}
// Name of the component
func (c *LoggingComponent) Name() string {
return "Logger"
}
// PreInit called before Init()
func (c *LoggingComponent) PreInit(ctx context.Context) error {
_ = ctx
// load config
conf.SetDefaultLogConfig()
c.enable = true
return nil
}
// SetDynamicConfig called when get dynamic config for the first time
func (c *LoggingComponent) SetDynamicConfig(config *platformConf.NodeConfig) error {
c.enable = config.APM != nil && config.APM.EnableLog
return nil
}
// Init the component
func (c *LoggingComponent) Init(server *micro.Server) error {
// init
var err error
// setup logger
basicConf := microConf.GetBasicConfig()
// spew.Dump(basicConf)
logConf := conf.GetLogConfig()
if !c.enable {
logConf.GraylogAddr = ""
logConf.LokiAddr = ""
}
// spew.Dump(logConf)
c.zlog, err = logging.CreateLogger(basicConf, logConf)
if err != nil {
return err
}
logger := c.zlog.Sugar()
//logger.Info("Using config file: ", viper.ConfigFileUsed())
server.RegisterElement(µ.LoggingElementKey, logger)
if basicConf.IsDevMode {
spew.Dump(logConf)
}
return nil
}
// PostStop called after Stop()
func (c *LoggingComponent) PostStop(ctx context.Context) error {
_ = ctx
// post stop
err := c.zlog.Sync()
time.Sleep(time.Second * 3)
return err
}