Skip to content

Commit c4cec1e

Browse files
author
phpdragon
committed
集成日志打印
1 parent 2655cf3 commit c4cec1e

File tree

6 files changed

+38
-33
lines changed

6 files changed

+38
-33
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
.idea
1+
.idea
2+
*.log

gateway_proxy.go

+10-6
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"./consts"
55
ctl "./controllers"
66
"./core"
7+
logger "./core/log"
78
eureka "./eureka-client"
89
"./logic"
910
"./utils"
@@ -20,7 +21,7 @@ import (
2021
)
2122

2223
var (
23-
gFaviconIco,_ = ioutil.ReadFile("favicon.ico")
24+
gFaviconIco, _ = ioutil.ReadFile("favicon.ico")
2425
// g_mqaddr = flag.String("mqaddr", "amqp://root:[email protected]:5672/", "mq server addr")
2526
gMySQLConnect = flag.String("mysqlUrl", "root:root1234@tcp(127.0.0.1:3306)/db_gateway_proxy?charset=utf8", "myssql host")
2627
// g_redisaddr = flag.String("redisaddr", "127.0.0.1:6379", "redis mq server addr")
@@ -106,12 +107,12 @@ func writeJsonResponse(rw http.ResponseWriter, req *http.Request, response inter
106107
rw.Header().Set(consts.ACCESS_CONTROL_ALLOW_CREDENTIALS, "true")
107108
}
108109

109-
var err interface{}
110+
var err error
110111
var dataBody []byte
111112
if isJson {
112113
dataBody, err = utils.ToJSONStringByte(response)
113114
if err != nil {
114-
log.Println(err)
115+
logger.Info(err.Error())
115116
rw.WriteHeader(http.StatusInternalServerError)
116117
return
117118
}
@@ -121,7 +122,7 @@ func writeJsonResponse(rw http.ResponseWriter, req *http.Request, response inter
121122

122123
_, err = rw.Write(dataBody)
123124
if err != nil {
124-
log.Println(err)
125+
logger.Info(err.Error())
125126
rw.WriteHeader(http.StatusInternalServerError)
126127
return
127128
}
@@ -159,6 +160,9 @@ func main() {
159160
}) // start client, register、heartbeat、refresh
160161
client.Start()
161162

163+
//监听日志级别设置
164+
http.HandleFunc("/handle/level", logger.GetAtomicLevel().ServeHTTP)
165+
162166
// http server
163167
http.HandleFunc(statusPageURL, func(writer http.ResponseWriter, request *http.Request) {
164168
writeJsonResponse(writer, request, ctl.ActuatorStatus(port, appName), true)
@@ -169,7 +173,7 @@ func main() {
169173
http.HandleFunc("/favicon.ico", func(writer http.ResponseWriter, request *http.Request) {
170174
_, err := writer.Write(gFaviconIco)
171175
if err != nil {
172-
log.Println(err)
176+
logger.Info(err.Error())
173177
writer.WriteHeader(http.StatusInternalServerError)
174178
return
175179
}
@@ -190,7 +194,7 @@ func main() {
190194
func indexHandler(rw http.ResponseWriter, req *http.Request, client *eureka.EurekaClient) {
191195
response, err := logic.HandleHttpRequest(req, client)
192196
if nil != err {
193-
log.Println(err)
197+
logger.Info(err.Error())
194198
response = core.BuildFail(core.SYSTEM_ERROR, err.Error())
195199
}
196200

logic/HttpReqLogic.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ package logic
33
import (
44
"../consts"
55
"../core"
6+
log "../core/log"
67
eureka "../eureka-client"
78
"../models"
89
"../utils"
910
"encoding/json"
1011
"errors"
11-
"fmt"
1212
"io/ioutil"
13-
"log"
1413
"net/http"
1514
"strings"
1615
)
@@ -25,7 +24,7 @@ func HandleHttpRequest(req *http.Request, client *eureka.EurekaClient) (interfac
2524

2625
routeMap, err := models.QueryAllActiveRoutes()
2726
if nil != err {
28-
log.Println(err)
27+
log.Info(err.Error())
2928
return nil, err
3029
}
3130
if nil == routeMap {
@@ -34,7 +33,7 @@ func HandleHttpRequest(req *http.Request, client *eureka.EurekaClient) (interfac
3433

3534
route, ok := routeMap[req.URL.Path]
3635
if !ok {
37-
log.Println(err)
36+
log.Info(err.Error())
3837
return nil, errors.New("请开发人员配置转发设置")
3938
}
4039

@@ -58,10 +57,12 @@ func callRemoteService(httpUrl string, req []byte) (interface{}, error) {
5857
func getPostParams(rw http.ResponseWriter, req *http.Request) (core.ApiRequest, error) {
5958
body, err := ioutil.ReadAll(req.Body)
6059
if nil != err {
60+
log.Info(err.Error())
6161
return core.ApiRequest{}, err
6262
}
6363
err = req.Body.Close()
6464
if nil != err {
65+
log.Info(err.Error())
6566
return core.ApiRequest{}, err
6667
}
6768

@@ -73,7 +74,7 @@ func getPostParams(rw http.ResponseWriter, req *http.Request) (core.ApiRequest,
7374
err := json.Unmarshal([]byte(body), &requestData)
7475
//解析失败会报错,如json字符串格式不对,缺"号,缺}等。
7576
if err != nil {
76-
fmt.Println(err)
77+
log.Info(err.Error())
7778
return requestData, nil
7879
}
7980
} else if strings.Contains(contentType, consts.APPLICATION_X_WWW_FORM_URLENCODED) {

utils/HttpUtil.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ func HttpPostByte(url string, postData []byte) ([]byte, error) {
1313
request.Header.Set("Connection", "keep-alive")
1414
request.Header.Set("Content-type", "application/json;charset=UTF-8")
1515
request.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
16-
response, error := client.Do(request)
17-
if error != nil || response.StatusCode != 200 {
18-
return nil, error
16+
response, err := client.Do(request)
17+
if err != nil || response.StatusCode != 200 {
18+
return nil, err
1919
}
2020

2121
return ioutil.ReadAll(response.Body)
@@ -28,28 +28,28 @@ func HttpPost(url string, postData string) (string, error) {
2828
request.Header.Set("Connection", "keep-alive")
2929
request.Header.Set("Content-type", "application/json;charset=UTF-8")
3030
request.Header.Set("user-agent", "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.143 Safari/537.36")
31-
response, error := client.Do(request)
32-
if error != nil || response.StatusCode != 200 {
31+
response, err := client.Do(request)
32+
if err != nil || response.StatusCode != 200 {
3333
return "", nil
3434
}
3535

36-
body, error := ioutil.ReadAll(response.Body)
37-
return string(body), error
36+
body, err := ioutil.ReadAll(response.Body)
37+
return string(body), err
3838
}
3939

4040
func HttpGet(url string) (string, error) {
4141
// 提交get请求
4242
client := &http.Client{}
4343
request, _ := http.NewRequest("GET", url, nil)
4444
request.Header.Set("Connection", "keep-alive")
45-
response, error := client.Do(request)
46-
if error != nil || response.StatusCode != 200 {
45+
response, err := client.Do(request)
46+
if err != nil || response.StatusCode != 200 {
4747
return "", nil
4848
}
4949

50-
body, error := ioutil.ReadAll(response.Body)
51-
if error != nil || response.StatusCode != 200 {
52-
return "", error
50+
body, err := ioutil.ReadAll(response.Body)
51+
if err != nil || response.StatusCode != 200 {
52+
return "", err
5353
}
54-
return string(body), error
54+
return string(body), err
5555
}

utils/SimpleUtil.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
package utils
22

33
import (
4+
log "../core/log"
45
"bytes"
56
"encoding/json"
6-
"log"
77
"os"
88
"regexp"
99
)
@@ -13,7 +13,7 @@ func dumJsonStr(jsonStr string) {
1313
err := json.Indent(&out, []byte(jsonStr), "", "\t")
1414

1515
if err != nil {
16-
log.Fatalln(err)
16+
log.Info(err.Error())
1717
}
1818

1919
_, _ = out.WriteTo(os.Stdout)
@@ -23,7 +23,7 @@ func jsonStr2Map(jsonStr string) (map[string]interface{}, error) {
2323
var mapResult map[string]interface{}
2424
//使用 json.Unmarshal(data []byte, v interface{})进行转换,返回 error 信息
2525
if err := json.Unmarshal([]byte(jsonStr), &mapResult); err != nil {
26-
log.Fatal(err)
26+
log.Info(err.Error())
2727
return nil, err
2828
}
2929

utils/SpringUtil.go

+4-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package utils
22

33
import (
4+
log "../core/log"
45
"encoding/json"
5-
"fmt"
6-
"log"
76
)
87

98
func ToJSONString(v interface{}) (string, error) {
109
jsonStr, err := json.Marshal(v)
1110
if err != nil {
12-
log.Fatal("Resp Json Err:", fmt.Sprint(err))
11+
log.Info("解析失败:" + err.Error())
1312
return "", nil
1413
}
1514

@@ -19,7 +18,7 @@ func ToJSONString(v interface{}) (string, error) {
1918
func ToJSONStringByte(v interface{}) ([]byte, error) {
2019
jsonStr, err := json.Marshal(v)
2120
if err != nil {
22-
log.Fatal("Resp Json Err:", fmt.Sprint(err))
21+
log.Info("解析失败:" + err.Error())
2322
return nil, nil
2423
}
2524
return jsonStr, nil
@@ -29,7 +28,7 @@ func ToJSON(jsonStr string, v interface{}) (interface{}, error) {
2928
err := json.Unmarshal([]byte(jsonStr), &v)
3029
//解析失败会报错,如json字符串格式不对,缺"号,缺}等。
3130
if err != nil {
32-
fmt.Println(err)
31+
log.Info("解析失败:" + err.Error())
3332
return nil, err
3433
}
3534
return v, nil

0 commit comments

Comments
 (0)