new_output.go
5.77 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package common
import (
"github.com/gin-gonic/gin"
"reflect"
)
/**
@author wangsong
输出工具,输出json 和输出日志(链式调用)
一、输出json
1 输出json支持自定义传struct 和默认的输出格式
common.NResponse().OutPut(ctx) //输出正确
common.NResponse("错了",100).SetLogFilePre("sku").OutPut(ctx) //输出错误,并存日志到文件 ******_sku.txt
2.输出 自定义 struct
common.NResponse(&struct).SetLogFilePre("sku").OutPut(ctx)
二、单独输出日志
common.NResponse().SetLogContent("进来了").OutPutLog()
尚未完善的地方
1.输出日志渠道 文件,数据库,oss之类
2.可以多次设置输出对象的 code 和 errMsg,目前只有第一次设置
*/
//接口
type NewOutPut interface {
GetErrorCode() int
GetErrorMsg() string
}
//输出类
type NewResponse struct {
logTool
outPutData NewOutPut
ctx *gin.Context //gin http
}
//日志类
type logTool struct {
openLog bool //开启记录log
logPath string //文件前缀
openParamLog bool //记录请求参数
logContent string //日志内容,会与ErrorMesg一起记录
loghandleFunc func(string)
//logger.LogInterface
//logConfig map[string]string
//logDrive logger.LogInterface
}
/*//日志类
type logTool struct {
openLog bool //开启记录log
LogfilePre string //文件前缀
openParamLog bool //记录请求参数
logContent string //日志内容,会与ErrorMesg一起记录
loghandleFunc func(logger.LogInterface,string)
}*/
//内部默认输出结构
type defaultOutPutDate struct {
ErrCode int `json:"errcode"`
ErrMsg string `json:"errmsg"`
Data interface{} `json:"data"`
}
//入口
func NResponse( opt...interface{}) *NewResponse {
newResponse:=&NewResponse{}
newResponse.createoutPutData(opt)
newResponse.logPath="logs"
return newResponse
}
//设置输出对象
func (NR *NewResponse) SetoutPutData(newOutPut NewOutPut) *NewResponse{
NR.outPutData=newOutPut
return NR
}
//设置日志内容
func (NR *NewResponse) SetLogContent(logStr string) *NewResponse{
NR.logContent=logStr
NR.openLog=true
return NR
}
//设置日志路径
/*func (NR *NewResponse) SetLogPath(filePre string) *NewResponse{
NR.logConfig["log_chan_size"]="10"
NR.logConfig["log_path"] = filePre
log ,_:=logger.InitLogger("file",NR.logConfig)
log.Init()
NR.logDrive=log
return NR
}*/
/**
开启日志记录参数
ctx 可以不传,传必须是ctx gin.Context
*/
func (NR *NewResponse) OpenParamLog() *NewResponse{
NR.openLog=true
NR.openParamLog=true
return NR
}
//设置 http ctx
func (NR *NewResponse) SetCtx(ctx *gin.Context) *NewResponse{
NR.ctx=ctx
return NR
}
//开启日志
func (NR *NewResponse) OpenLog() *NewResponse{
NR.openLog=true
return NR
}
func (NR *NewResponse) SetLogHandel(a func(string)) *NewResponse {
NR.loghandleFunc=a
return NR
}
//输出到前端
/**
ctx gin.Context 只处理一个
*/
func (NR *NewResponse) OutPut(ctx *gin.Context) {
NR.SetCtx(ctx)
if(reflect.ValueOf(NR.loghandleFunc).IsZero()!=true){
NR.loghandleFunc(NR.getLogErrorStr())//日志处理
}
if(reflect.ValueOf(NR.outPutData).IsZero()==true){//没有输出对象,就用默认的
NR.outPutData=&defaultOutPutDate{0,"ok",nil}
}
NR.ctx.JSONP(200,NR.outPutData)
}
/**
正确输出
*/
func (NR *NewResponse) SucessOutPut(ctx *gin.Context) {
NR.SetCtx(ctx)
NR.loghandleFunc(NR.getLogErrorStr())//日志处理//日志处理
NR.outPutData=&defaultOutPutDate{0,"ok",nil}
NR.ctx.JSONP(200,NR.outPutData)
}
/**
错误输出
*/
func (NR *NewResponse) ErrorOutPut(ctx *gin.Context,opt...interface{}) {
NR.SetCtx(ctx)
NR.createoutPutData(opt)
NR.loghandleFunc(NR.getLogErrorStr())//日志处理
NR.ctx.JSONP(200,NR.outPutData)
}
//输出日志
func (NR *NewResponse) OutPutLog(ctx ...*gin.Context) {
NR.setjudgeCtx(ctx)
NR.loghandleFunc(NR.getLogErrorStr())//日志处理
}
//获取输出日志的Str
func (NR *NewResponse) getLogErrorStr() string{
if(NR.openLog){
errMsg:=""
formDataStr1:=""
if(NR.outPutData!=nil){
if(NR.outPutData.GetErrorCode()!=0){
errMsg="errMsg:"
errMsg+=NR.outPutData.GetErrorMsg()
}
}
if(NR.openParamLog){
if(reflect.ValueOf(NR.ctx).IsZero()!=true){
formDataStr1="请求过来的参数:"
str:=GetRequestParam(NR.ctx)
formDataStr1+=str
}
}
logstr:=errMsg+formDataStr1+NR.logContent
return logstr
}
return ""
}
func (L *defaultOutPutDate)GetErrorMsg() string {
return L.ErrMsg
}
func (L *defaultOutPutDate)GetErrorCode() int {
return L.ErrCode
}
/**
创建 输出对象
如果 第一个参数是结构,输出对象就是此结构(就是自定义输出对象)
否则输出对象就是 defaultOutPutDate;第一个参数 是msg 第二个是code ,第三个是data
*/
func (NR *NewResponse )createoutPutData(opt []interface{}) {
if(len(opt)>0){
errMsg:="ok"
errCode:=0
var data interface{}
//第一个参数是结构,输出对象就是此结构
valueR:=reflect.ValueOf(opt[0])
if(valueR.Kind()==reflect.Ptr ){
if(valueR.Elem().Kind()==reflect.Struct){
if newOutPut,ok:=opt[0].(NewOutPut);ok{
NR.outPutData=newOutPut
}else{
panic("If it is a structure, it must inherit the interface newoutput")
}
return
}
}
//否则就是默认的结构 defaultOutPutDate 第一个参数 是msg 第二个是code ,第三个是data
errMsg=opt[0].(string)
errCode=100010
if(len(opt)>=2){
errCode=opt[1].(int)
}
if(len(opt)>=3){
data=opt[2]
}
NR.outPutData=&defaultOutPutDate{errCode,errMsg,data}
}
}
/**
NR.ctx 没有赋值就取opt[0]
如果opt有参数,必须是*gin.Context,否则pannic
*/
func (NR *NewResponse ) setjudgeCtx(opt []*gin.Context) {
if(len(opt)>0){
if(reflect.ValueOf(NR.ctx).IsZero()){
NR.SetCtx(opt[0])
}else{
panic("NewResponse.ctx No value ,Should be*gin.Context")
}
}
}