-
Notifications
You must be signed in to change notification settings - Fork 271
/
logger.go
117 lines (100 loc) · 2.61 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
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
package sdk
import (
"encoding/json"
"io"
"log"
"os"
"strings"
"time"
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/utils"
)
var logChannel string
var defaultChannel = "AlibabaCloud"
type Logger struct {
*log.Logger
formatTemplate string
isOpen bool
lastLogMsg string
}
var defaultLoggerTemplate = `{time} {channel}: "{method} {uri} HTTP/{version}" {code} {cost} {hostname}`
var loggerParam = []string{"{time}", "{start_time}", "{ts}", "{channel}", "{pid}", "{host}", "{method}", "{uri}", "{version}", "{target}", "{hostname}", "{code}", "{error}", "{req_headers}", "{res_body}", "{res_headers}", "{cost}"}
func initLogMsg(fieldMap map[string]string) {
for _, value := range loggerParam {
fieldMap[value] = ""
}
}
func (client *Client) GetLogger() *Logger {
return client.logger
}
func (client *Client) GetLoggerMsg() string {
if client.logger == nil {
client.SetLogger("", "", os.Stdout, "")
}
return client.logger.lastLogMsg
}
func (client *Client) SetLogger(level string, channel string, out io.Writer, template string) {
if level == "" {
level = "info"
}
logChannel = "AlibabaCloud"
if channel != "" {
logChannel = channel
}
log := log.New(out, "["+strings.ToUpper(level)+"]", log.Lshortfile)
if template == "" {
template = defaultLoggerTemplate
}
client.logger = &Logger{
Logger: log,
formatTemplate: template,
isOpen: true,
}
}
func (client *Client) OpenLogger() {
if client.logger == nil {
client.SetLogger("", "", os.Stdout, "")
}
client.logger.isOpen = true
}
func (client *Client) CloseLogger() {
if client.logger != nil {
client.logger.isOpen = false
}
}
func (client *Client) SetTemplate(template string) {
if client.logger == nil {
client.SetLogger("", "", os.Stdout, "")
}
client.logger.formatTemplate = template
}
func (client *Client) GetTemplate() string {
if client.logger == nil {
client.SetLogger("", "", os.Stdout, "")
}
return client.logger.formatTemplate
}
func TransToString(object interface{}) string {
byt, err := json.Marshal(object)
if err != nil {
return ""
}
return string(byt)
}
func (client *Client) printLog(fieldMap map[string]string, err error) {
if err != nil {
fieldMap["{error}"] = err.Error()
}
fieldMap["{time}"] = time.Now().Format("2006-01-02 15:04:05")
fieldMap["{ts}"] = utils.GetTimeInFormatISO8601()
fieldMap["{channel}"] = logChannel
if client.logger != nil {
logMsg := client.logger.formatTemplate
for key, value := range fieldMap {
logMsg = strings.Replace(logMsg, key, value, -1)
}
client.logger.lastLogMsg = logMsg
if client.logger.isOpen == true {
client.logger.Output(2, logMsg)
}
}
}