logrus.go
3.9 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package log
import (
"fmt"
"github.com/olivere/elastic/v7"
"github.com/sirupsen/logrus"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/constant"
"gopkg.in/sohlich/elogrus.v7"
"io"
"os"
)
type ServerLog struct {
serviceName string
logrus *logrus.Logger
}
func (logger *ServerLog) AddHook(write io.Writer) {
panic("implement me")
}
func (logger *ServerLog) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *ServerLog) SetLevel(level string) {
switch level {
case "trace":
logger.logrus.Level = logrus.TraceLevel
case "debug":
logger.logrus.Level = logrus.DebugLevel
case "info":
logger.logrus.Level = logrus.InfoLevel
case "warn":
logger.logrus.Level = logrus.WarnLevel
case "error":
logger.logrus.Level = logrus.ErrorLevel
case "fatal":
logger.logrus.Level = logrus.FatalLevel
case "panic":
logger.logrus.Level = logrus.PanicLevel
default:
logger.logrus.Level = logrus.DebugLevel
}
}
func (logger *ServerLog) Trace(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Trace(msg)
}
func (logger *ServerLog) Debug(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Debug(msg)
}
func (logger *ServerLog) Info(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Info(msg)
}
func (logger *ServerLog) Warn(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Warn(msg)
}
func (logger *ServerLog) Error(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Error(msg)
}
func (logger *ServerLog) Fatal(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Fatal(msg)
}
func (logger *ServerLog) Panic(msg string, appends ...map[string]interface{}) {
contextLogger := logger.logrus.WithFields(logrus.Fields{"serviceName": logger.serviceName})
for _, append := range appends {
contextLogger = contextLogger.WithFields(append)
}
contextLogger.Panic(msg)
}
func NewServerLog() *ServerLog {
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{
TimestampFormat: "2006-01-02 15:04:05",
PrettyPrint: true,
}
if constant.LOG_TYPE == "file" {
file, err := os.OpenFile("logrus.log", os.O_CREATE|os.O_WRONLY, 0666)
if err == nil {
_ = file.Truncate(1024 * 1024 * 2)
logger.Out = file
} else {
logger.Info("Failed to log to file, using default stderr")
}
} else if constant.LOG_TYPE == "es" {
url := fmt.Sprintf("http://%s:%s", "es.chenzhiying.asia", "80")
client, err := elastic.NewClient(elastic.SetURL(url), elastic.SetSniff(false))
if err != nil {
//log.Panic(err)
panic(err)
}
hook, err := elogrus.NewAsyncElasticHook(client, "es.chenzhiying.asia", logrus.DebugLevel, "boss-appointment")
if err != nil {
//log.Panic(err)
panic(err)
}
logger.Hooks.Add(hook)
} else {
logger.Out = os.Stdout
}
return &ServerLog{
logrus: logger,
}
}