Расширенная информация о запросах в логе

This commit is contained in:
Anatoly Prohacky 2023-04-25 21:48:32 +10:00
parent d596cab3fd
commit deb168b2e1

View File

@ -8,6 +8,7 @@ import (
"strconv"
"github.com/gin-gonic/gin"
"github.com/google/uuid"
"github.com/xuri/excelize/v2"
"gitstore.ru/tolikproh/sirius/internal/model"
)
@ -19,13 +20,16 @@ var (
)
func (s *Handler) GinConvert(c *gin.Context) {
s.log.Debug("start convert")
sessionId := uuid.New().String()
s.log.Printf("start convert. session id: %s; user agent: %s; remote addr: %s; file size [byte]: %d",
sessionId, c.Request.UserAgent(), c.Request.RemoteAddr, c.Request.ContentLength)
c.Request.Body = http.MaxBytesReader(c.Writer, c.Request.Body, s.cfg.Srv.MaxSizeFile<<20)
file, fileHeader, err := c.Request.FormFile("file")
if err != nil {
s.log.Errorf("error code: 001. %s", err.Error())
s.log.Errorf("session id: %s. error code: 001. %s", sessionId, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
@ -42,7 +46,7 @@ func (s *Handler) GinConvert(c *gin.Context) {
// Validate File Type
if _, ex := TEXT_TYPES[fileType]; !ex {
s.log.Errorf("error code: 002. formated data not text/plain")
s.log.Errorf("session id: %s. error code: 002. formated data not text/plain", sessionId)
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.New("formated data not text/plain").Error(),
})
@ -50,7 +54,7 @@ func (s *Handler) GinConvert(c *gin.Context) {
}
if !json.Valid(buffer) {
s.log.Errorf("error code: 003. formated data not json")
s.log.Errorf("session id: %s. error code: 003. formated data not json", sessionId)
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.New("formated data not json").Error(),
})
@ -61,7 +65,7 @@ func (s *Handler) GinConvert(c *gin.Context) {
err = json.Unmarshal(buffer, &sirius)
if err != nil {
s.log.Errorf("error code: 004. %s", err.Error())
s.log.Errorf("session id: %s. error code: 004. %s", sessionId, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.New("Error unmarshal file").Error(),
})
@ -70,7 +74,7 @@ func (s *Handler) GinConvert(c *gin.Context) {
buff, err := SaveToExel(sirius.NewBolid().ZoneInfo())
if err != nil {
s.log.Errorf("error code: 005. %s", err.Error())
s.log.Errorf("session id: %s. error code: 005. %s", sessionId, err.Error())
c.JSON(http.StatusBadRequest, gin.H{
"error": errors.New("Error create file").Error(),
})
@ -79,7 +83,7 @@ func (s *Handler) GinConvert(c *gin.Context) {
c.Data(200, "application/vnd.ms-excel", buff.Bytes())
s.log.Printf("converted: ok. filename: %s; file size: %d bytes", fileHeader.Filename, fileHeader.Size)
s.log.Printf("session id: %s.converted: ok. filename: %s; file size: %d bytes", sessionId, fileHeader.Filename, fileHeader.Size)
}