beego_logs.go
3.1 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
package log
import (
"bytes"
"encoding/json"
"fmt"
"github.com/beego/beego/v2/core/logs"
_ "github.com/beego/beego/v2/core/logs/es"
"gitlab.fjmaimaimai.com/allied-creation/allied-creation-cooperation/pkg/constant"
"io"
"strconv"
)
type LoggerConfig struct {
Level int `json:"level,omitempty"`
Filename string `json:"filename,omitempty"`
MaxSize int `json:"maxsize,omitempty"`
MaxBackups int `json:"max_backups,omitempty"`
MaxAge int `json:"max_age,omitempty"`
Compress bool `json:"compress,omitempty"`
}
type BeegoLog struct {
serviceName string
beeLogger *logs.BeeLogger
}
func (logger *BeegoLog) AddHook(write io.Writer) {
panic("implement me")
}
func (logger *BeegoLog) SetServiceName(serviceName string) {
logger.serviceName = serviceName
}
func (logger *BeegoLog) SetLevel(level string) {
ilv, err := strconv.Atoi(level)
if err != nil {
ilv = logs.LevelDebug
}
logger.beeLogger.SetLevel(ilv)
}
func (logger *BeegoLog) Trace(msg string, appends ...map[string]interface{}) {
logs.Trace(msg, appends)
}
func (logger *BeegoLog) Debug(msg string, appends ...map[string]interface{}) {
appendsJson, _ := json.Marshal(appends)
if appendsJson == nil {
logs.Debug(msg, make(map[string]interface{}, 0))
} else {
var out bytes.Buffer
err := json.Indent(&out, appendsJson, "", " ")
if err != nil {
fmt.Println(msg)
return
}
logs.Debug(msg, out.String())
}
}
func (logger *BeegoLog) Info(msg string, appends ...map[string]interface{}) {
appendsJson, _ := json.Marshal(appends)
if appendsJson == nil {
logs.Info(msg, make(map[string]interface{}, 0))
} else {
var out bytes.Buffer
err := json.Indent(&out, appendsJson, "", " ")
if err != nil {
fmt.Println(msg)
return
}
logs.Info(msg, out.String())
}
}
func (logger *BeegoLog) Warn(msg string, appends ...map[string]interface{}) {
logs.Warn(msg, appends)
}
func (logger *BeegoLog) Error(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
func (logger *BeegoLog) Fatal(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
func (logger *BeegoLog) Panic(msg string, appends ...map[string]interface{}) {
logs.Error(msg, appends)
}
func NewBeegoLog(conf LoggerConfig) *BeegoLog {
logger := logs.GetBeeLogger()
logger.SetLevel(conf.Level)
if constant.LOG_TYPE == "file" { // 日志存储到文件
confByte, _ := json.Marshal(conf)
err := logger.SetLogger(logs.AdapterFile, string(confByte))
if err != nil {
fmt.Println(err.Error())
return nil
}
} else if constant.LOG_TYPE == "es" { // 日志存储到ES
err := logger.SetLogger(logs.AdapterEs, `{"dsn":"http://192.168.100.222:9200/","level":7}`)
if err != nil {
fmt.Println(err.Error())
return nil
}
} else { // 日志输出到控制台
err := logger.SetLogger(logs.AdapterConsole, `{"level":7,"color":true}`)
if err != nil {
fmt.Println(err.Error())
return nil
}
}
logger.SetPrefix(constant.LOG_PREFIX)
logger.EnableFuncCallDepth(true)
logger.SetLogFuncCallDepth(5)
logger.SetPrefix(constant.LOG_PREFIX)
return &BeegoLog{
beeLogger: logger,
}
}