Skip to content

Commit

Permalink
log lỗi ra file
Browse files Browse the repository at this point in the history
  • Loading branch information
DuyTechmaster committed Oct 12, 2019
1 parent 7106a37 commit 4e1d627
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 0 deletions.
50 changes: 50 additions & 0 deletions logging/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
## Ghi lỗi ra file log

1. Hàm bắt lỗi và ghi ra file log
```go
func LogError(c *gin.Context, message string) {
var errorInfo ErrorInfo
errorInfo.Time = time.Now()
errorInfo.Path = c.FullPath()
errorInfo.Method = c.Request.Method

var log = logrus.New()
log.Out = os.Stdout
file, err := os.OpenFile("log/logging.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err == nil {
log.Out = file
} else {
log.Println(err)
}

log.WithFields(logrus.Fields{
"Path": errorInfo.Path,
"Method": errorInfo.Method,
}).Error(message)
}
```

2. Trong mỗi route, gọi hàm **LogError** khi kiểm tra lỗi, ví dụ như trong **POST /upload-video**
```go
file, err := c.FormFile("video-file")
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
LogError(c, err.Error())
return
}

fileType := file.Header["Content-Type"][0]

if GetAllowFormat(fileType, allowedMediaType) == "" {
c.JSON(http.StatusBadRequest, "Định dạng không hợp lệ. Yêu cầu: mp4")
LogError(c, "Định dạng không hợp lệ. Yêu cầu: mp4")
return
}

err = c.SaveUploadedFile(file, path.Join("./upload", file.Filename))
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
LogError(c, err.Error())
return
}
```
48 changes: 48 additions & 0 deletions logging/controller/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package controller

import (
"os"
"time"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
)

var allowedMediaType = []string{"video/mp4"}

type ErrorInfo struct {
Time time.Time
Path string
Method string
}

func GetAllowFormat(format string, allowFormat []string) string {
var allow string
for _, item := range allowFormat {
if format == item {
allow = item
}
}
return allow
}

func LogError(c *gin.Context, message string) {
var errorInfo ErrorInfo
errorInfo.Time = time.Now()
errorInfo.Path = c.FullPath()
errorInfo.Method = c.Request.Method

var log = logrus.New()
log.Out = os.Stdout
file, err := os.OpenFile("log/logging.log", os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666)
if err == nil {
log.Out = file
} else {
log.Println(err)
}

log.WithFields(logrus.Fields{
"Path": errorInfo.Path,
"Method": errorInfo.Method,
}).Error(message)
}
36 changes: 36 additions & 0 deletions logging/controller/controller.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package controller

import (
"net/http"
"path"

"github.com/gin-gonic/gin"
)

func HomePage(c *gin.Context) {
c.HTML(http.StatusOK, "index.html", gin.H{})
}

func UploadVideo(c *gin.Context) {
file, err := c.FormFile("video-file")
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
LogError(c, err.Error())
return
}

fileType := file.Header["Content-Type"][0]

if GetAllowFormat(fileType, allowedMediaType) == "" {
c.JSON(http.StatusBadRequest, "Định dạng không hợp lệ. Yêu cầu: mp4")
LogError(c, "Định dạng không hợp lệ. Yêu cầu: mp4")
return
}

err = c.SaveUploadedFile(file, path.Join("./upload", file.Filename))
if err != nil {
c.JSON(http.StatusInternalServerError, err.Error())
LogError(c, err.Error())
return
}
}
4 changes: 4 additions & 0 deletions logging/log/logging.log
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
time="2019-10-12T09:10:53+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Error="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video Time="2019-10-12 09:10:53.860303 +0700 +07 m=+19.269711617"
time="2019-10-12T09:12:47+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video Time="2019-10-12 09:12:47.071071 +0700 +07 m=+8.262930654"
time="2019-10-12T09:14:08+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Path=/upload-video
time="2019-10-12T09:17:06+07:00" level=error msg="Định dạng không hợp lệ. Yêu cầu: mp4" Method=POST Path=/upload-video
16 changes: 16 additions & 0 deletions logging/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"gin-gonic/logging/controller"

"github.com/gin-gonic/gin"
)

func main() {
router := gin.Default()
router.LoadHTMLGlob("view/*")

router.GET("/", controller.HomePage)
router.POST("/upload-video", controller.UploadVideo)
router.Run(":8085")
}
16 changes: 16 additions & 0 deletions logging/view/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Upload file</title>
</head>
<body>
<h2>Upload video</h2>
<form action="/upload-video" method="POST" enctype="multipart/form-data">
<input type="file" name="video-file" accept="video/*">
<input type="submit">
</form>
</body>
</html>

0 comments on commit 4e1d627

Please sign in to comment.