/
logService.go
66 lines (56 loc) · 1.7 KB
/
logService.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
package log
import (
"encoding/json"
"github.com/drep-project/drepcli/app"
"gopkg.in/urfave/cli.v1"
"path"
)
type LogService struct {
config *LogConfig
}
func (logService *LogService) Name() string {
return "log"
}
func (logService *LogService) Api() []app.API {
return []app.API{}
}
func (logService *LogService) Flags() []cli.Flag {
return []cli.Flag{LogDirFlag, LogLevelFlag, VmoduleFlag, BacktraceAtFlag}
}
func (logService *LogService) Init(executeContext *app.ExecuteContext) error {
phase := executeContext.GetConfig(logService.Name())
logService.config = &LogConfig{}
err := json.Unmarshal(phase, logService.config)
if err != nil {
return err
}
logService.setLogConfig(executeContext.CliContext, executeContext.CommonConfig.HomeDir)
return SetUp(logService.config)
}
func (logService *LogService) Start(executeContext *app.ExecuteContext) error {
return nil
}
func (logService *LogService) Stop(executeContext *app.ExecuteContext) error {
return nil
}
// setLogConfig creates an log configuration from the set command line flags,
func (logService *LogService) setLogConfig(ctx *cli.Context, homeDir string) {
logService.config = &LogConfig{}
if ctx.GlobalIsSet(LogLevelFlag.Name) {
logService.config.LogLevel = ctx.GlobalInt(LogLevelFlag.Name)
} else {
logService.config.LogLevel = 3
}
if ctx.GlobalIsSet(VmoduleFlag.Name) {
logService.config.Vmodule = ctx.GlobalString(VmoduleFlag.Name)
}
if ctx.GlobalIsSet(BacktraceAtFlag.Name) {
logService.config.BacktraceAt = ctx.GlobalString(BacktraceAtFlag.Name)
}
//logdir
if ctx.GlobalIsSet(LogDirFlag.Name) {
logService.config.DataDir = ctx.GlobalString(LogDirFlag.Name)
} else {
logService.config.DataDir = path.Join(homeDir, "log")
}
}