-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
142 lines (120 loc) · 2.7 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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package remote_log_go
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"time"
)
type Logger struct {
appName string
storageDays int32
serviceName string
transport []interface{}
}
/**
* @description: 创建Logger类
* @param {string} appName
* @param {int32} storageDays
* @param {[]interface{}} transport
* @return {*}
*/
func NewLogger(appName string, storageDays int32, transport ...interface{}) *Logger {
hostname, _ := os.Hostname()
if appName == "" {
panic(errors.New("appname cannot be empty"))
}
RemoteLogApiUrl = os.Getenv("REMOTE_LOG_API_URL")
if RemoteLogApiUrl == "" {
panic(errors.New("invalid env REMOTE_LOG_API_URL"))
}
goPath := os.Getenv("NODE_APP_DATA")
if goPath != "" {
ErrorLogPath = fmt.Sprintf("%v/%v/remote_logs", goPath, appName)
os.MkdirAll(ErrorLogPath, os.ModePerm)
} else {
panic(errors.New("invalid env NODE_APP_DATA"))
}
return &Logger{
appName: appName,
storageDays: storageDays,
transport: transport,
serviceName: hostname,
}
}
func (l *Logger) Init() error {
params := url.Values{}
params.Add("app", l.appName)
params.Add("storageDays", fmt.Sprintf("%v", l.storageDays))
params.Add("pwd", "b3981ef7-694b-11ec-a673-00163e1357b3")
baseUrl, _ := url.Parse(RemoteLogApiUrl)
baseUrl.Path = "api/appStorageDays"
baseUrl.RawQuery = params.Encode()
res, err := http.Get(baseUrl.String())
if err != nil {
return err
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
var response Response
err = json.Unmarshal(body, &response)
if err != nil {
return err
}
if response.Code != 200 {
return errors.New(response.Message)
}
return nil
}
func (l *Logger) Debug(message string) {
l.log(Debug, message)
}
func (l *Logger) Info(message string) {
l.log(Info, message)
}
func (l *Logger) Warn(message string) {
l.log(Warn, message)
}
func (l *Logger) Error(message error) {
l.log(Error, message.Error())
}
func (l *Logger) Access(message string) {
l.log(Access, message)
}
func (l *Logger) log(level Level, message string) {
if message == "" {
return
}
now := time.Now().Format("2006-01-02 15:04:05")
logInfo := &LogInfo{
ServiceName: l.serviceName,
AppName: l.appName,
Level: string(level),
LogTime: now,
Message: message,
}
isLog := false // // 是否日志已记录,未记录的日志console打印
for _, item := range l.transport {
switch v := item.(type) {
case *HttpTransport:
if v.shouldLog(level) {
isLog = true
v.log(logInfo)
}
case *ConsoleTransport:
if v.shouldLog(level) {
isLog = true
v.log(logInfo)
}
}
}
if !isLog {
consoleTransport := NewConsoleTransport()
consoleTransport.log(logInfo)
}
}