-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from OTOT-dev/log
feat: 增加日志收集
- Loading branch information
Showing
9 changed files
with
242 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ docs | |
|
||
# other | ||
**/*.DS_Store | ||
log/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package common | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"github.com/sirupsen/logrus" | ||
"gopkg.in/natefinch/lumberjack.v2" | ||
"path" | ||
) | ||
|
||
var Log = logrus.New() | ||
|
||
type LumberHook struct { | ||
logger *lumberjack.Logger | ||
} | ||
|
||
func (hook LumberHook) Levels() []logrus.Level { | ||
return logrus.AllLevels | ||
} | ||
|
||
func (hook LumberHook) Fire(entry *logrus.Entry) (err error) { | ||
|
||
fields := entry.Data | ||
var writeLine string | ||
if len(fields) != 0 { | ||
writeLine = fmt.Sprintf("[GIN] %s | %s | %d | %s | %s | %f \n", | ||
fields["time"], | ||
fields["method"], | ||
fields["status"], | ||
fields["path"], | ||
fields["clientIp"], | ||
fields["timeSub"], | ||
) | ||
} else { | ||
writeLine = entry.Message + "\n" | ||
} | ||
|
||
_, err = hook.logger.Write([]byte(writeLine)) | ||
return | ||
} | ||
|
||
type MyFormatter struct{} | ||
|
||
func (f MyFormatter) Format(entry *logrus.Entry) ([]byte, error) { | ||
|
||
// 设置buffer 缓冲区 | ||
var b *bytes.Buffer | ||
if entry.Buffer == nil { | ||
b = &bytes.Buffer{} | ||
} else { | ||
b = entry.Buffer | ||
} | ||
|
||
// 设置格式 | ||
_, err := fmt.Fprintf(b, "%s\n", entry.Message) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return b.Bytes(), nil | ||
} | ||
|
||
func InitLog(logPath, appName string) { | ||
|
||
// 设置日志输出格式 | ||
Log.SetFormatter(&MyFormatter{}) | ||
|
||
logFileName := path.Join(logPath, appName) | ||
// 使用滚动压缩方式记录日志 | ||
lumberLog := &lumberjack.Logger{ | ||
Filename: logFileName, //日志文件位置 | ||
MaxSize: 1, // 单文件最大容量,单位是MB | ||
MaxBackups: 3, // 最大保留过期文件个数 | ||
MaxAge: 1, // 保留过期文件的最大时间间隔,单位是天 | ||
Compress: true, // 是否需要压缩滚动日志, 使用的 gzip 压缩 | ||
} | ||
|
||
fileHook := LumberHook{lumberLog} | ||
|
||
Log.AddHook(&fileHook) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
package main | ||
|
||
import ( | ||
"auth-server/common" | ||
"auth-server/config" | ||
"auth-server/router" | ||
) | ||
|
||
func main() { | ||
//初始化日志 | ||
common.InitLog(config.LogPath, config.ServerName) | ||
router.InitRouter() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
package middleware | ||
|
||
import ( | ||
"auth-server/common" | ||
"auth-server/config" | ||
"bytes" | ||
"fmt" | ||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
var log = common.Log | ||
|
||
const ( | ||
statusColor200 = 42 | ||
statusColor404 = 43 | ||
statusColor500 = 41 | ||
statusColor400 = 40 | ||
|
||
methodColorGET = 44 | ||
methodColorPOST = 45 | ||
methodColorPATCH = 46 | ||
methodColorDELTE = 47 | ||
) | ||
|
||
func LogMiddleware() gin.HandlerFunc { | ||
return func(c *gin.Context) { | ||
start := time.Now() | ||
path := c.Request.URL.Path | ||
raw := c.Request.URL.RawQuery | ||
params := "" | ||
if config.DebugMode && c.ContentType() == "application/json" { | ||
ByteBody, _ := io.ReadAll(c.Request.Body) | ||
c.Request.Body = io.NopCloser(bytes.NewBuffer(ByteBody)) | ||
params = string(ByteBody) | ||
} | ||
|
||
// Process request | ||
c.Next() | ||
|
||
// Stop timer | ||
end := time.Now() | ||
timeSub := end.Sub(start).Seconds() | ||
clientIP := c.ClientIP() | ||
method := c.Request.Method | ||
statusCode := c.Writer.Status() | ||
|
||
if raw != "" { | ||
path = path + "?" + raw | ||
} | ||
|
||
var statusColor string | ||
switch statusCode { | ||
case http.StatusOK: | ||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor200, statusCode) | ||
case http.StatusNotFound: | ||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor404, statusCode) | ||
case http.StatusBadRequest: | ||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor400, statusCode) | ||
case http.StatusInternalServerError: | ||
statusColor = fmt.Sprintf("\033[%dm %d \033[0m", statusColor500, statusCode) | ||
} | ||
|
||
var methodColor string | ||
switch method { | ||
case http.MethodGet: | ||
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorGET, method) | ||
case http.MethodDelete: | ||
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorDELTE, method) | ||
case http.MethodPost: | ||
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorPOST, method) | ||
case http.MethodPatch: | ||
methodColor = fmt.Sprintf("\033[%dm %s \033[0m", methodColorPATCH, method) | ||
} | ||
// 写入到文件中不包含相关到颜色编码,所以需要额外到字段来存储原始信息 | ||
fields := logrus.Fields{ | ||
"time": start.Format("2006-01-02 15:04:06"), | ||
"method": method, | ||
"status": statusCode, | ||
"path": path, | ||
"clientIp": clientIP, | ||
"timeSub": timeSub, | ||
} | ||
if config.DebugMode { | ||
log.WithFields(fields).Infof("[GIN] %s |%s| %s| %s | %s | %fs \n %s", | ||
start.Format("2006-01-02 15:04:06"), | ||
statusColor, | ||
clientIP, | ||
methodColor, | ||
path, | ||
timeSub, | ||
params, | ||
) | ||
} else { | ||
log.WithFields(fields).Infof("[GIN] %s |%s| %s| %s | %s | %fs", | ||
start.Format("2006-01-02 15:04:06"), | ||
statusColor, | ||
clientIP, | ||
methodColor, | ||
path, | ||
timeSub, | ||
) | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.