-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_unix.go
99 lines (81 loc) · 2.08 KB
/
log_unix.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
// +build !windows,!nacl,!plan9
package jsonlog
import (
"io"
"log"
"log/syslog"
"os"
"path"
"github.com/huangshaokun/funnel"
"github.com/rs/zerolog"
"github.com/spf13/viper"
)
var consumer []*funnel.Consumer
//newJSONLog ...
func newJSONLog(filepath, confpath string, arg ...interface{}) *zerolog.Logger {
newfilepath, filename := path.Split(filepath)
configPath, configName := path.Split(confpath)
l := newLogger(newfilepath, filename, configPath, configName, arg...)
return l
}
//logClose ...
func logClose(sg os.Signal) {
for _, v := range consumer {
v.HandleSignal(sg)
}
}
//NewLogger ...""""
func newLogger(filepath, filename, configPath, configName string, arg ...interface{}) *zerolog.Logger {
logger, err := syslog.New(syslog.LOG_ERR, path.Join(filepath, filename))
if err != nil {
log.SetFlags(log.Lshortfile | log.LstdFlags)
log.Println(err)
os.Exit(1)
}
vp := viper.New()
//配置初始化
vp.SetConfigName(configName)
vp.AddConfigPath(configPath + "/")
vp.SetDefault(funnel.LoggingDirectory, filepath)
vp.SetDefault(funnel.LoggingActiveFileName, filename)
vp.SetDefault(funnel.RotationMaxLines, 100000)
vp.SetDefault(funnel.RotationMaxFileSizeBytes, 100*1024*1024)
vp.SetDefault(funnel.Gzip, true)
//读取传入的参数
if len(arg) != 0 {
kvmap, _ := arg[0].(map[string]interface{})
for k, v := range kvmap {
vp.SetDefault(k, v)
}
}
cfg, reloadChan, outputWriter, err := funnel.GetConfig(vp, logger, path.Join(configPath, configName+".toml"))
if err != nil {
log.Println(err)
os.Exit(1)
}
lp := funnel.GetLineProcessor(cfg)
c := &funnel.Consumer{
Config: cfg,
LineProcessor: lp,
ReloadChan: reloadChan,
Logger: logger,
Writer: outputWriter,
}
dbgR, dbgW := io.Pipe()
go func() {
err = c.Start(dbgR)
if err != nil {
log.Println(err)
os.Exit(1)
}
}()
consumer = append(consumer, c)
pnewW, err := newWriter(configPath, configName+"level", arg...)
if err != nil {
log.Println(err)
os.Exit(1)
}
pnewW.run = dbgW
slog := zerolog.New(pnewW).With().Timestamp().Logger()
return &slog
}