-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathnocd.go
77 lines (68 loc) · 1.52 KB
/
nocd.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
/*
* Copyright (c) 2017 - 2020, 奶爸<[email protected]>
* All rights reserved.
*/
package nocd
import (
"runtime"
"time"
"github.com/evalphobia/logrus_sentry"
log "github.com/sirupsen/logrus"
"gopkg.in/ini.v1"
)
//mLog of sentry logger
var mLog *log.Logger
//Conf of NoCD config
var Conf *ini.File
//Debug debuggable
var Debug bool
//Loc system time location
var Loc *time.Location
//InitSysConfig system: load common config
func InitSysConfig(file string) {
var err error
if mLog == nil {
mLog = log.New()
}
Conf, err = ini.Load(file)
if err != nil {
mLog.Panicln(err)
}
// initial sentry dsn
if Conf.Section("third_party").Key("sentry_dsn").String() != "" {
hook, err := logrus_sentry.NewSentryHook(Conf.Section("third_party").Key("sentry_dsn").String(), []log.Level{
log.PanicLevel,
log.FatalLevel,
log.ErrorLevel,
})
if err == nil {
mLog.Hooks.Add(hook)
} else {
mLog.Panicln(err)
}
}
// set timezone
Loc, err = time.LoadLocation(Conf.Section("nocd").Key("loc").String())
if err != nil {
panic(err)
}
// set debuggable
Debug, err = Conf.Section("nocd").Key("debug").Bool()
if err != nil {
panic(err)
}
if Debug {
mLog.SetLevel(log.DebugLevel)
} else {
mLog.SetLevel(log.InfoLevel)
}
}
//Logger 带行号文件名方法名的Logger
func Logger() *log.Entry {
logger := log.NewEntry(mLog)
if pc, file, line, ok := runtime.Caller(1); ok {
fName := runtime.FuncForPC(pc).Name()
return logger.WithField("file", file).WithField("line", line).WithField("func", fName)
}
return logger
}