/
emergency.go
60 lines (51 loc) · 1.4 KB
/
emergency.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
package logs
import (
"fmt"
"os"
"path/filepath"
"go.uber.org/zap"
)
var (
back_emc = new(zap.SugaredLogger)
)
func Infow(msg string, keysAndValues ...interface{}) {
back_emc.Infow(msg, keysAndValues...)
}
func init() {
//GetDetails log dir from environment
cfg := zap.NewProductionConfig()
// 生成日志文件路径
logpath := os.Getenv("HBIGDATA_HOME")
filename := filepath.Join(logpath, "temp", "log", "emergency.log")
// 判断日志所在目录,是否存在
_, err := os.Stat(filepath.Join(logpath, "temp", "log"))
if err != nil {
if os.IsNotExist(err) {
// 创建日志目录
err := os.MkdirAll(filepath.Join(logpath, "temp", "log"), os.ModeDir)
if err != nil {
fmt.Errorf("%s", "文件不存在,创建日志文件失败")
// 日志文件无法创建
// 使用console作为日志输出
} else {
cfg.OutputPaths = []string{filename}
cfg.ErrorOutputPaths = []string{filename}
}
}
// 如果日志文件存在,但是无法获取Stat信息
// 将日志输出到console上
} else {
cfg.OutputPaths = []string{filename}
cfg.ErrorOutputPaths = []string{filename}
}
cfg.EncoderConfig.EncodeTime = iso8601TimeEncoder
cfg.DisableStacktrace = true
cfg.DisableCaller = true
cfg.Level.UnmarshalText([]byte("debug"))
logger, err := cfg.Build()
if err != nil {
fmt.Errorf("%v", err)
return
}
back_emc = logger.WithOptions(zap.AddCallerSkip(1)).Sugar()
}