Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
黄成意
/
go_sku_server
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
691491e6
authored
Dec 10, 2020
by
wang
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
解决冲突后提交
parent
97e9c5e1
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
161 additions
and
73 deletions
controller/middleware_controller.go
controller/sku_save_controller.go
doc/log.md
pkg/common/new_output.go
pkg/logger/logger.go
controller/middleware_controller.go
View file @
691491e6
...
...
@@ -53,7 +53,7 @@ func Error_Middleware() gin.HandlerFunc {
defer
func
()
{
if
err
:=
recover
();
err
!=
nil
{
errMsg
:=
fmt
.
Sprintf
(
"%s"
,
err
)
common
.
NResponse
(
errMsg
,
500
)
.
SetLogFilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
(
)
common
.
NResponse
(
errMsg
)
.
SetLogHandel
(
saveLogHandle
)
.
OutPut
(
ctx
)
}
}()
ctx
.
Next
()
...
...
controller/sku_save_controller.go
View file @
691491e6
...
...
@@ -6,6 +6,7 @@ import (
"go_sku_server/model/saveModel"
"go_sku_server/pkg/common"
"go_sku_server/pkg/e"
"go_sku_server/pkg/logger"
"go_sku_server/service"
)
...
...
@@ -35,12 +36,14 @@ sku基本数据增加和修改
Attrs int `json:"attrs" `
*/
func
SaveSku
(
ctx
*
gin
.
Context
)
{
//参数验证
var
lySaveRequest
saveModel
.
LySaveRequest
if
err
:=
ctx
.
ShouldBindBodyWith
(
&
lySaveRequest
,
binding
.
JSON
);
err
!=
nil
{
common
.
NResponse
(
err
.
Error
())
.
SetLog
FilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
(
)
common
.
NResponse
(
err
.
Error
())
.
SetLog
Handel
(
saveLogHandle
)
.
OutPut
(
ctx
)
return
}
//执行skuSave
...
...
@@ -48,11 +51,13 @@ func SaveSku(ctx *gin.Context) {
serviceErr
,
skuId
:=
LySave
.
SaveSku
(
lySaveRequest
,
ctx
)
//错误处理
if
(
serviceErr
!=
nil
){
code
:=
10001
errMsg
:=
serviceErr
.
Error
()
if
err
,
ok
:=
serviceErr
.
(
*
e
.
ApiError
);
ok
{
common
.
NResponse
(
err
.
Error
(),
err
.
Code
)
.
SetLogFilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
()
}
else
{
common
.
NResponse
(
err
.
Error
(),
err
.
Code
)
.
SetLogFilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
()
code
=
err
.
Code
errMsg
=
err
.
ErrMsg
}
common
.
NResponse
(
errMsg
,
code
)
.
SetLogHandel
(
saveLogHandle
)
.
OpenParamLog
()
.
OutPut
(
ctx
)
}
else
{
//执行成功
res
:=
saveModel
.
LySaveResponse
{
0
,
"ok"
,
skuId
}
common
.
NResponse
(
&
res
)
.
OutPut
(
ctx
)
...
...
@@ -66,26 +71,32 @@ func SkuEdit(ctx *gin.Context) {
//参数验证
var
lyeditRequest
saveModel
.
LyEditRequest
if
err
:=
ctx
.
ShouldBindBodyWith
(
&
lyeditRequest
,
binding
.
JSON
);
err
!=
nil
{
common
.
NResponse
(
err
.
Error
())
.
SetLog
FilePre
(
"editSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
(
)
common
.
NResponse
(
err
.
Error
())
.
SetLog
Handel
(
editLogHandle
)
.
OutPut
(
ctx
)
return
}
//执行skuSave
LySave
:=
service
.
LySaveService
{}
serviceErr
,
skuId
:=
LySave
.
SkuEdit
(
lyeditRequest
)
//错误处理
if
(
serviceErr
!=
nil
){
if
err
,
ok
:=
serviceErr
.
(
*
e
.
ApiError
);
ok
{
common
.
NResponse
(
err
.
Error
()
,
err
.
Code
)
.
SetLogFilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
(
)
common
.
NResponse
(
err
.
Error
()
)
.
SetLogHandel
(
editLogHandle
)
.
OutPut
(
ctx
)
}
else
{
common
.
NResponse
(
err
.
Error
()
,
err
.
Code
)
.
SetLogFilePre
(
"saveSku"
)
.
OpenRecordReuquest
(
ctx
)
.
OutPut
(
)
common
.
NResponse
(
err
.
Error
()
)
.
SetLogHandel
(
editLogHandle
)
.
OutPut
(
ctx
)
}
}
else
{
//执行成功
res
:=
saveModel
.
LySaveResponse
{
0
,
"ok"
,
skuId
}
common
.
NResponse
(
&
res
)
.
OutPut
(
ctx
)
}
return
}
func
saveLogHandle
(
errMsg
string
)
{
logger
.
Select
(
"sku_save"
)
.
Error
(
errMsg
)
}
func
editLogHandle
(
errMsg
string
)
{
logger
.
Select
(
"sku_save"
)
.
Error
(
errMsg
)
}
doc/log.md
0 → 100644
View file @
691491e6
### 日志
### 同步日志
#### 调用方法
不需要额外配置
```
json
import
(
"go_sku_server/pkg/logger"
)
logger.SyncInsert(
"sku_save"
,
"记录一下日志"
,
logger.LogLevelError)
//记录错误日志
logger.SyncInsert(
"sku_save"
,
"记录一下日志"
,
logger.LogLevelDebug)
//记录debug日志
logger.SyncInsert(
"sku_save"
,
"记录一下日志"
)
//默认记录info
```
### 异步日志
#### 配置文件
```
ini
[log_config]
1
=
sku_save,5
2
=
sku_edit,5
```
比如 sku_save,5 路径 sku_save 即文件夹是 sku 文件名类似 是save_2020-12-10.log
#### 调用示例
```
json
import
(
"go_sku_server/pkg/logger"
)
logger.Select(
"sku_save"
).Info(errMsg)
logger.Select(
"sku_save"
).Error(errMsg)
```
\ No newline at end of file
pkg/common/new_output.go
View file @
691491e6
...
...
@@ -3,13 +3,23 @@ package common
import
(
"fmt"
"github.com/gin-gonic/gin"
"go_sku_server/pkg/logger"
"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
{
...
...
@@ -17,21 +27,37 @@ type NewOutPut interface {
GetErrorMsg
()
string
}
//输出类
type
NewResponse
struct
{
LogDrive
*
logDrive
O
utPutData
NewOutPut
logTool
o
utPutData
NewOutPut
ctx
*
gin
.
Context
//gin http
}
//日志类
type
log
Drive
struct
{
type
log
Tool
struct
{
openLog
bool
//开启记录log
LogfilePre
string
//文件前缀
isRecordReuquest
bool
//记录请求参数
logContent
string
//日志内容,会与ErrorMesg一起记录
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"`
...
...
@@ -43,42 +69,42 @@ type defaultOutPutDate struct {
func
NResponse
(
opt
...
interface
{})
*
NewResponse
{
newResponse
:=&
NewResponse
{}
newResponse
.
createOutPutData
(
opt
)
log
:=&
logDrive
{}
newResponse
.
LogDrive
=
log
newResponse
.
LogDrive
.
LogfilePre
=
"defaltLog"
newResponse
.
createoutPutData
(
opt
)
newResponse
.
logPath
=
"logs"
return
newResponse
}
//设置输出对象
func
(
NR
*
NewResponse
)
Set
O
utPutData
(
newOutPut
NewOutPut
)
*
NewResponse
{
NR
.
O
utPutData
=
newOutPut
func
(
NR
*
NewResponse
)
Set
o
utPutData
(
newOutPut
NewOutPut
)
*
NewResponse
{
NR
.
o
utPutData
=
newOutPut
return
NR
}
//设置日志内容
func
(
NR
*
NewResponse
)
SetLogContent
(
logStr
string
)
*
NewResponse
{
NR
.
LogDrive
.
logContent
=
logStr
NR
.
LogDrive
.
openLog
=
true
NR
.
logContent
=
logStr
NR
.
openLog
=
true
return
NR
}
//设置日志文件前缀
func
(
NR
*
NewResponse
)
SetLogFilePre
(
filePre
string
)
*
NewResponse
{
NR
.
LogDrive
.
openLog
=
true
NR
.
LogDrive
.
LogfilePre
=
filePre
//设置日志路径
/*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
)
OpenRecordReuquest
(
ctx
...
interface
{})
*
NewResponse
{
NR
.
setjudgeCtx
(
ctx
)
NR
.
LogDrive
.
openLog
=
true
NR
.
LogDrive
.
isRecordReuquest
=
true
func
(
NR
*
NewResponse
)
OpenParamLog
()
*
NewResponse
{
NR
.
openLog
=
true
NR
.
openParamLog
=
true
return
NR
}
...
...
@@ -90,7 +116,12 @@ func (NR *NewResponse) SetCtx(ctx *gin.Context) *NewResponse{
//开启日志
func
(
NR
*
NewResponse
)
OpenLog
()
*
NewResponse
{
NR
.
LogDrive
.
openLog
=
true
NR
.
openLog
=
true
return
NR
}
func
(
NR
*
NewResponse
)
SetLogHandel
(
a
func
(
string
))
*
NewResponse
{
NR
.
loghandleFunc
=
a
return
NR
}
...
...
@@ -98,42 +129,64 @@ func (NR *NewResponse) OpenLog() *NewResponse{
/**
ctx gin.Context 只处理一个
*/
func
(
NR
*
NewResponse
)
OutPut
(
ctx
...
interface
{})
{
NR
.
setjudgeCtx
(
ctx
)
NR
.
logHandle
()
//日志处理
if
(
reflect
.
ValueOf
(
NR
.
OutPutData
)
.
IsZero
()
==
true
){
//没有输出对象,就用默认的
NR
.
OutPutData
=&
defaultOutPutDate
{
0
,
"ok"
,
nil
}
func
(
NR
*
NewResponse
)
OutPut
(
ctx
*
gin
.
Context
)
{
NR
.
SetCtx
(
ctx
)
if
(
reflect
.
ValueOf
(
NR
.
loghandleFunc
)
.
IsZero
()
!=
true
){
NR
.
loghandleFunc
(
NR
.
getLogErrorStr
())
//日志处理
}
NR
.
ctx
.
JSONP
(
200
,
NR
.
OutPutData
)
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
)
OutPutLog
()
{
NR
.
OpenLog
()
NR
.
logHandle
()
//日志处理
/**
错误输出
*/
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
)
}
//内部,日志处理(后续要改善 输出日志渠道 文件、oss、数据库,或者第三方)
func
(
NR
*
NewResponse
)
logHandle
()
{
//输出日志
func
(
NR
*
NewResponse
)
OutPutLog
(
ctx
...*
gin
.
Context
)
{
NR
.
setjudgeCtx
(
ctx
)
NR
.
loghandleFunc
(
NR
.
getLogErrorStr
())
//日志处理
}
if
(
NR
.
LogDrive
.
openLog
){
//获取输出日志的Str
func
(
NR
*
NewResponse
)
getLogErrorStr
()
string
{
if
(
NR
.
openLog
){
errMsg
:=
""
formDataStr1
:=
""
if
(
NR
.
O
utPutData
!=
nil
){
if
(
NR
.
O
utPutData
.
GetErrorCode
()
!=
0
){
if
(
NR
.
o
utPutData
!=
nil
){
if
(
NR
.
o
utPutData
.
GetErrorCode
()
!=
0
){
errMsg
=
"errMsg:"
errMsg
+=
NR
.
O
utPutData
.
GetErrorMsg
()
errMsg
+=
NR
.
o
utPutData
.
GetErrorMsg
()
}
}
if
(
NR
.
LogDrive
.
isRecordReuquest
){
if
(
NR
.
openParamLog
){
if
(
reflect
.
ValueOf
(
NR
.
ctx
)
.
IsZero
()
!=
true
){
formDataStr1
=
"请求过来的参数:"
formDataStr1
+=
GetRequestParam
(
NR
.
ctx
)
str
:=
GetRequestParam
(
NR
.
ctx
)
formDataStr1
+=
str
}
}
logstr
:=
errMsg
+
formDataStr1
+
NR
.
LogDrive
.
logContent
logger
.
Log
(
logstr
,
NR
.
LogDrive
.
LogfilePre
,
1
)
logstr
:=
errMsg
+
formDataStr1
+
NR
.
logContent
return
logstr
}
return
""
}
...
...
@@ -145,12 +198,13 @@ func (L *defaultOutPutDate)GetErrorCode() int {
return
L
.
ErrCode
}
/**
创建 输出对象
如果 第一个参数是结构,输出对象就是此结构(就是自定义输出对象)
否则输出对象就是 defaultOutPutDate;第一个参数 是msg 第二个是code ,第三个是data
*/
func
(
NR
*
NewResponse
)
create
O
utPutData
(
opt
[]
interface
{})
{
func
(
NR
*
NewResponse
)
create
o
utPutData
(
opt
[]
interface
{})
{
if
(
len
(
opt
)
>
0
){
errMsg
:=
"ok"
...
...
@@ -164,7 +218,7 @@ func (NR *NewResponse )createOutPutData(opt []interface{}) {
fmt
.
Print
(
a
)
if
(
valueR
.
Elem
()
.
Kind
()
==
reflect
.
Struct
){
if
newOutPut
,
ok
:=
opt
[
0
]
.
(
NewOutPut
);
ok
{
NR
.
O
utPutData
=
newOutPut
NR
.
o
utPutData
=
newOutPut
}
else
{
panic
(
"If it is a structure, it must inherit the interface newoutput"
)
}
...
...
@@ -181,7 +235,7 @@ func (NR *NewResponse )createOutPutData(opt []interface{}) {
if
(
len
(
opt
)
>=
3
){
data
=
opt
[
2
]
}
NR
.
O
utPutData
=&
defaultOutPutDate
{
errCode
,
errMsg
,
data
}
NR
.
o
utPutData
=&
defaultOutPutDate
{
errCode
,
errMsg
,
data
}
}
}
...
...
@@ -189,12 +243,10 @@ func (NR *NewResponse )createOutPutData(opt []interface{}) {
NR.ctx 没有赋值就取opt[0]
如果opt有参数,必须是*gin.Context,否则pannic
*/
func
(
NR
*
NewResponse
)
setjudgeCtx
(
opt
[]
interface
{})
{
func
(
NR
*
NewResponse
)
setjudgeCtx
(
opt
[]
*
gin
.
Context
)
{
if
(
len
(
opt
)
>
0
){
if
(
reflect
.
ValueOf
(
NR
.
ctx
)
.
IsZero
()){
if
ctx
,
ok
:=
opt
[
0
]
.
(
*
gin
.
Context
);
ok
{
NR
.
SetCtx
(
ctx
)
}
NR
.
SetCtx
(
opt
[
0
])
}
else
{
panic
(
"NewResponse.ctx No value ,Should be*gin.Context"
)
}
...
...
pkg/logger/logger.go
View file @
691491e6
...
...
@@ -20,11 +20,7 @@ const (
var
(
logD
map
[
string
]
logger
.
LogInterface
)
var
once
sync
.
Once
func
Loginit
()
{
once
.
Do
(
func
()
{
fmt
.
Print
(
"日志初始化开始"
)
...
...
@@ -37,16 +33,8 @@ func Loginit() {
logD
[
sliceConfig
[
0
]]
=
log
}
})
}
/**
github.com/ichunt2019/log 参数说明
log_chan_size 最高5协程处理日志
log_path 日志路径 默认为logs
open_sync 开启同步(本项目不需要同步模式,允许使用 wait方法阻塞直至日志完全存入文件,一般用于一次性处理脚本)
*/
/**
初始化 log(github.com/ichunt2019/log)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment