forked from shiguanghuxian/etcd-manage
/
logger.go
67 lines (61 loc) · 1.79 KB
/
logger.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
package logger
import (
"net/url"
"os"
"runtime"
"strings"
"time"
"github.com/shiguanghuxian/etcd-manage/program/common"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// 日志对象
var (
Log *zap.SugaredLogger
)
// InitLogger 日志初始化,用于记录操作日志
func InitLogger(logPath string, isDebug bool) (*zap.SugaredLogger, error) {
infoLogPath := ""
// errorLogPath := ""
if logPath == "" {
logRoot := common.GetRootDir() + "logs" + string(os.PathSeparator)
if isExt, _ := common.PathExists(logRoot); isExt == false {
os.MkdirAll(logRoot, os.ModePerm)
}
infoLogPath = logRoot + time.Now().Format("20060102") + ".log"
// errorLogPath = logRoot + time.Now().Format("20060102_error") + ".log"
} else {
logPath = strings.TrimRight(logPath, string(os.PathSeparator))
infoLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102") + ".log"
// errorLogPath = logPath + string(os.PathSeparator) + time.Now().Format("20060102_error") + ".log"
}
// 兼容win根完整路径问题
zap.RegisterSink("winfile", func(u *url.URL) (zap.Sink, error) {
return os.OpenFile(u.Path[1:], os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0644)
})
cfg := &zap.Config{
Encoding: "json",
}
cfg.EncoderConfig = zap.NewProductionEncoderConfig()
atom := zap.NewAtomicLevel()
if isDebug == true {
atom.SetLevel(zapcore.DebugLevel)
cfg.OutputPaths = []string{"stdout"}
// cfg.ErrorOutputPaths = []string{"stdout"}
} else {
atom.SetLevel(zapcore.InfoLevel)
if runtime.GOOS == "windows" {
cfg.OutputPaths = []string{"winfile:///" + infoLogPath}
} else {
cfg.OutputPaths = []string{infoLogPath}
}
// cfg.ErrorOutputPaths = []string{errorLogPath}
}
cfg.Level = atom
logger, err := cfg.Build()
if err != nil {
return nil, err
}
Log = logger.Sugar()
return Log, nil
}