diff --git a/logging/Readme.md b/logging/Readme.md new file mode 100644 index 0000000..b992f20 --- /dev/null +++ b/logging/Readme.md @@ -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 +} +``` \ No newline at end of file diff --git a/logging/controller/base.go b/logging/controller/base.go new file mode 100644 index 0000000..8eba834 --- /dev/null +++ b/logging/controller/base.go @@ -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) +} diff --git a/logging/controller/controller.go b/logging/controller/controller.go new file mode 100644 index 0000000..55bb72c --- /dev/null +++ b/logging/controller/controller.go @@ -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 + } +} diff --git a/logging/log/logging.log b/logging/log/logging.log new file mode 100644 index 0000000..5140a21 --- /dev/null +++ b/logging/log/logging.log @@ -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 diff --git a/logging/main.go b/logging/main.go new file mode 100644 index 0000000..bf5d147 --- /dev/null +++ b/logging/main.go @@ -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") +} diff --git a/logging/view/index.html b/logging/view/index.html new file mode 100644 index 0000000..43868c4 --- /dev/null +++ b/logging/view/index.html @@ -0,0 +1,16 @@ + + +
+ + + +