-
Notifications
You must be signed in to change notification settings - Fork 0
/
provider.go
120 lines (104 loc) · 2.83 KB
/
provider.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
package log
import (
"io"
"strings"
"github.com/y19941115mx/ygo/framework"
"github.com/y19941115mx/ygo/framework/contract"
"github.com/y19941115mx/ygo/framework/provider/log/formatter"
services "github.com/y19941115mx/ygo/framework/provider/log/service"
)
// YgoLogServiceProvider 服务提供者
type YgoLogServiceProvider struct {
framework.ServiceProvider
Driver string // Driver
// 日志级别
Level contract.LogLevel
// 日志输出格式方法
Formatter contract.Formatter
// 日志context上下文信息获取函数
CtxFielder contract.CtxFielder
// 日志输出信息
Output io.Writer
}
// Register 注册一个服务实例
func (l *YgoLogServiceProvider) Register(c framework.Container) framework.NewInstance {
if l.Driver == "" {
tcs, err := c.Make(contract.ConfigKey)
if err != nil {
// 默认使用console
return services.NewYgoConsoleLog
}
cs := tcs.(contract.Config)
l.Driver = strings.ToLower(cs.GetString("log.Driver"))
}
// 根据driver的配置项确定
switch l.Driver {
case "single":
return services.NewYgoSingleLog
case "rotate":
return services.NewYgoRotateLog
case "console":
return services.NewYgoConsoleLog
case "custom":
return services.NewYgoCustomLog
default:
return services.NewYgoConsoleLog
}
}
// Boot 启动的时候注入
func (l *YgoLogServiceProvider) Boot(c framework.Container) error {
return nil
}
// IsDefer 是否延迟加载
func (l *YgoLogServiceProvider) IsDefer() bool {
return true
}
// Params 定义要传递给实例化方法的参数
func (l *YgoLogServiceProvider) Params(c framework.Container) []interface{} {
// 获取configService
configService := c.MustMake(contract.ConfigKey).(contract.Config)
// 设置参数formatter
if l.Formatter == nil {
l.Formatter = formatter.TextFormatter
if configService.IsExist("log.formatter") {
v := configService.GetString("log.formatter")
if v == "json" {
l.Formatter = formatter.JsonFormatter
} else if v == "text" {
l.Formatter = formatter.TextFormatter
}
}
}
if l.Level == contract.UnknownLevel {
l.Level = contract.InfoLevel
if configService.IsExist("log.level") {
l.Level = logLevel(configService.GetString("log.level"))
}
}
// 定义5个参数
return []interface{}{c, l.Level, l.CtxFielder, l.Formatter, l.Output}
}
// Name 定义对应的服务字符串凭证
func (l *YgoLogServiceProvider) Name() string {
return contract.LogKey
}
// logLevel get level from string
func logLevel(config string) contract.LogLevel {
switch strings.ToLower(config) {
case "panic":
return contract.PanicLevel
case "fatal":
return contract.FatalLevel
case "error":
return contract.ErrorLevel
case "warn":
return contract.WarnLevel
case "info":
return contract.InfoLevel
case "debug":
return contract.DebugLevel
case "trace":
return contract.TraceLevel
}
return contract.UnknownLevel
}