Newer
Older
//@author Ajit Jagtap
//@version Thu Jul 05 2018 06:40:54 GMT+0530 (IST)
// Package loggermdl will help you log error
package loggermdl
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"
"corelab.mkcl.org/MKCLOS/coredevelopmentplatform/corepkgv2/errormdl"
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
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
lumberjack "gopkg.in/natefinch/lumberjack.v2"
)
var logger *zap.Logger
var sugar *zap.SugaredLogger
// Init Init Logger
// maxBackupFileSize, megabytes
// maxAgeForBackupFile, days
func Init(fileName string, maxBackupCnt, maxBackupFileSize, maxAgeForBackupFileInDays int, loglevel zapcore.Level) {
os.MkdirAll(filepath.Dir(fileName), os.ModePerm)
w := zapcore.AddSync(&lumberjack.Logger{
Filename: fileName,
MaxSize: maxBackupFileSize, // megabytes
MaxBackups: maxBackupCnt,
MaxAge: maxAgeForBackupFileInDays, // days
})
// zap.AddStacktrace(
rawJSON := []byte(`{
"level": "debug",
"encoding": "json",
"outputPaths": ["stdout", "/tmp/logs"],
"errorOutputPaths": ["stderr"],
"initialFields": {"foo": "bar"},
"disableCaller":false,
"encoderConfig": {
"messageKey": "m",
"callerKey": "c",
"callerEncode": 0,
"timeKey": "t",
"levelKey": "l",
"levelEncoder": "lowercase"
}
}`)
var cfg zap.Config
json.Unmarshal(rawJSON, &cfg)
core := zapcore.NewCore(
//enc, //
zapcore.NewJSONEncoder(zap.NewDevelopmentEncoderConfig()),
w,
loglevel,
)
logger = zap.New(core, zap.AddCaller(), zap.AddCallerSkip(1))
defer logger.Sync()
sugar = logger.Sugar()
}
// GetCallers will return callers chain
func GetCallers(skip int) string {
_, file, line, ok := runtime.Caller(skip)
if errormdl.CheckBool(!ok) {
file = "<???>"
line = 1
} else {
slash := strings.LastIndex(file, "/")
if slash >= 0 {
file = file[slash+1:]
}
}
return fmt.Sprintf("%s:%d", file, line)
}