Skip to content

Commit 13a4d11

Browse files
author
woytu
committed
加入日志logrus
1 parent 1edd07f commit 13a4d11

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ require (
77
github.com/gin-gonic/gin v1.4.0
88
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 // indirect
99
github.com/mitchellh/gox v1.0.1 // indirect
10+
github.com/sirupsen/logrus v1.4.2
1011
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2
1112
)

go.sum

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+I
22
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
33
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
44
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
5+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
56
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3 h1:t8FVkw33L+wilf2QiWkw0UV77qRpcH/JHPKGpKa2E8g=
67
github.com/gin-contrib/sse v0.0.0-20190301062529-5545eab6dad3/go.mod h1:VJ0WA2NBN22VlZ2dKZQPAPnyWw5XTlK1KymzLKsr59s=
78
github.com/gin-gonic/gin v1.4.0 h1:3tMoCCfM7ppqsR0ptz/wi1impNpT7/9wQtMZ8lr1mCQ=
@@ -14,6 +15,8 @@ github.com/json-iterator/go v1.1.6 h1:MrUvLMLTMxbqFJ9kzlvat/rYZqZnW3u4wkLzWTaFwK
1415
github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCVDaaPEHmU=
1516
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02 h1:nI4Q7WQDcng8eRmi8bRP1SXlJIYLkgdgR7ZhJuzPbhs=
1617
github.com/karalabe/xgo v0.0.0-20190301120235-2d6d1848fb02/go.mod h1:iYGcTYIPUvEWhFo6aKUuLchs+AV4ssYdyuBbQJZGcBk=
18+
github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk=
19+
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
1720
github.com/mattn/go-isatty v0.0.7 h1:UvyT9uN+3r7yLEYSlJsbQGdsaB/a0DlgWP3pql6iwOc=
1821
github.com/mattn/go-isatty v0.0.7/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
1922
github.com/mitchellh/gox v1.0.1 h1:x0jD3dcHk9a9xPSDN6YEL4xL6Qz0dvNYm8yZqui5chI=
@@ -26,7 +29,11 @@ github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9
2629
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
2730
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2831
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
32+
github.com/sirupsen/logrus v1.4.2 h1:SPIRibHv4MatM3XXNO2BJeFLZwZ2LvZgfQ5+UNI2im4=
33+
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
2934
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
35+
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
36+
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
3037
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
3138
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
3239
github.com/ugorji/go v1.1.4 h1:j4s+tAvLfL3bZyefP2SEWmhBzmuIlH/eqNuPdFPgngw=
@@ -38,6 +45,8 @@ golang.org/x/net v0.0.0-20190503192946-f4e77d36d62c/go.mod h1:t9HGtf8HONx5eT2rtn
3845
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
3946
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223 h1:DH4skfRX4EBpamg7iV4ZlCpblAHI6s6TDM39bFZumv8=
4047
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
48+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc=
49+
golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
4150
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
4251
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
4352
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

key-gin.go

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import (
44
"flag"
55
"fmt"
66
"github.com/gin-gonic/gin"
7+
log "github.com/sirupsen/logrus"
78
"io"
89
"key-gin/common"
910
"key-gin/utils"
10-
"log"
1111
"net/http"
1212
"os"
1313
"strings"
@@ -106,15 +106,19 @@ func getKey(c *gin.Context) {
106106
c.JSON(http.StatusOK, common.Error(300, "请选择版本"))
107107
return
108108
}
109-
path, err := utils.ContextPath("key-gin")
109+
// 获取当前绝对路径
110+
dir, err := os.Getwd()
110111
if err != nil {
112+
log.Error(err)
111113
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
112114
return
113115
}
114-
path = utils.PathStitching(path, "pyutils")
116+
path := utils.PathStitching(dir, "pyutils")
115117
if company == "netsarang" {
116118
result, err := utils.ExecutePython(path+"/xshell_key.py", app, version)
117119
if err != nil {
120+
log.Error(err)
121+
fmt.Println(err)
118122
c.JSON(http.StatusOK, common.Error(500, "获取key系统错误"))
119123
return
120124
}
@@ -161,13 +165,13 @@ func upload(c *gin.Context) {
161165
fmt.Println(header.Filename)
162166
out, err := os.Create("./tmp/" + filename + ".png")
163167
if err != nil {
164-
log.Fatal(err)
168+
log.Error(err)
165169
}
166170
defer out.Close()
167171
// 拷贝上传的文件信息到新建的out文件中
168172
_, err = io.Copy(out, file)
169173
if err != nil {
170-
log.Fatal(err)
174+
log.Error(err)
171175
}
172176
}
173177

@@ -223,6 +227,18 @@ func Port() (port string) {
223227
//return ":" + os.Args[1]
224228
}
225229

230+
func init() {
231+
// 设置日志格式为json格式 自带的只有两种样式logrus.JSONFormatter{}和logrus.TextFormatter{}
232+
log.SetFormatter(&log.JSONFormatter{})
233+
234+
// 设置将日志输出到标准输出(默认的输出为stderr,标准错误)
235+
// 日志消息输出可以是任意的io.writer类型
236+
log.SetOutput(os.Stdout)
237+
238+
// 设置日志级别为warn以上
239+
log.SetLevel(log.DebugLevel)
240+
}
241+
226242
func main() {
227243
router := gin.Default()
228244

0 commit comments

Comments
 (0)