å®éã«éç¨ããã¦ããã¨çµ¶å¯¾ã«å¿ è¦ãªã®ãã¢ã¯ã»ã¹ãã°ã§ãã APPãµã¼ãã¼ã¨ãã¦éç¨ããå ´åã¯ããã¾ã§ç´°ããã®ã¯ãããªãã®ã§ããã apacheã®ãã°ã¿ãããªãã®ãåºåãããã¨æãã¾ã
apacheã®ãã°ã®ãã©ã¼ãããã¯ä»¥ä¸ã®ãããªå½¢ã§ããæ¨æºãããªãã§ã
LogFormat "%a %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
Fiberã§ãã°ããã¡ã¤ã«ã«åºåããããã«ãlogger.Config
ã®Output
ãã£ã¼ã«ãã使ç¨ãã¦ãã¡ã¤ã«ãæå®ãããã¨ãã§ãã¾ãã
以ä¸ã«ãã°ãç¹å®ã®ãã©ã«ãã«ä¿åããæ¹æ³ã示ãã¾ãã
ã¾ããGoã®æ¨æºã©ã¤ãã©ãªã使ç¨ãã¦ãã°ãã¡ã¤ã«ãä½æãã
ãã®ãã¡ã¤ã«ãlogger.Config
ã®Output
ãã£ã¼ã«ãã«è¨å®ãã¾ãã
å®ä¾ã®ããã°ã©ã
以ä¸ã¯ããã°ãlogs
ãã©ã«ãå
ã®access.log
ãã¡ã¤ã«ã«ä¿åããä¾ã§ãã
package main import ( "github.com/gofiber/fiber/v2" "github.com/gofiber/fiber/v2/middleware/logger" "os" "path/filepath" "time" ) func main() { // Create logs directory if it doesn't exist logDir := "logs" if _, err := os.Stat(logDir); os.IsNotExist(err) { os.Mkdir(logDir, os.ModePerm) } // Open log file logFile, err := os.OpenFile(filepath.Join(logDir, "access.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } defer logFile.Close() app := fiber.New() // Define a custom log format logFormat := "${ip} - - [${time}] \"${method} ${path} ${protocol}\" ${status} ${bytesSent} \"${referer}\" \"${ua}\"\n" app.Use(logger.New(logger.Config{ Format: logFormat, TimeFormat: "02/Jan/2006:15:04:05 -0700", TimeZone: "Local", Output: logFile, })) app.Get("/", func(c *fiber.Ctx) error { return c.SendString("Hello, World!") }) app.Listen(":3000") }
説æ
ãã°ãã©ã«ãã®ä½æ:
go logDir := "logs" if _, err := os.Stat(logDir); os.IsNotExist(err) { os.Mkdir(logDir, os.ModePerm) }
logs
ã¨ãããã©ã«ããåå¨ããªãå ´åãæ°ããä½æãã¾ãããã°ãã¡ã¤ã«ã®ãªã¼ãã³:
go logFile, err := os.OpenFile(filepath.Join(logDir, "access.log"), os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) if err != nil { panic(err) } defer logFile.Close()
logs/access.log
ã¨ãããã¡ã¤ã«ãèªã¿æ¸ãã¢ã¼ãã§ãªã¼ãã³ããåå¨ããªãå ´åã¯æ°ããä½æãã¾ãããã¡ã¤ã«ã¯ã¢ããªã±ã¼ã·ã§ã³çµäºæã«éãããã¾ããLogger ããã«ã¦ã§ã¢ã®è¨å®:
go app.Use(logger.New(logger.Config{ Format: logFormat, TimeFormat: "02/Jan/2006:15:04:05 -0700", TimeZone: "Local", Output: logFile, }))
ãã°ãã©ã¼ãããã¨ãã¡ã¤ã«åºåè¨å®ãæå®ãã¾ãã
TimeFormatã®è£è¶³
TimeFormat
ãã£ã¼ã«ãã¯ããã°ã¨ã³ããªå
ã®ã¿ã¤ã ã¹ã¿ã³ãã®ãã©ã¼ããããæå®ããããã«ä½¿ç¨ããã¾ãããã®ãã©ã¼ãããã¯ãGoã®æ¨æºã©ã¤ãã©ãª time
ããã±ã¼ã¸ã§æä¾ããããã©ã¼ãããæååã«å¾ãã¾ãã
Goã®æéãã©ã¼ãããã¯ä»ã®è¨èªã¨ç°ãªããç¹å®ã®ãµã³ãã«æé Mon Jan 2 15:04:05 -0700 MST 2006
ã«åºã¥ãã¦ãã¾ãããã®ãµã³ãã«æéã®åé¨åã¯ãç°ãªãæéã³ã³ãã¼ãã³ãã表ãã¾ããå
·ä½çã«ã¯ï¼
02
= æ¥ (02)Jan
= æ (1æ)2006
= å¹´ (2006å¹´)15
= æ (15æã24æéå½¢å¼)04
= å (04å)05
= ç§ (05ç§)-0700
= ã¿ã¤ã ã¾ã¼ã³ã®ãªãã»ãã (-07:00)
Apacheã®LogFormat
ã«å¯¾å¿ããããã«ããããã以ä¸ã®ãããªãã©ã¼ããããæå®ãã¾ã:
TimeFormat: "02/Jan/2006:15:04:05 -0700"
ãã©ã¼ãããã®è©³ç´°
- 02
æ¥ (å
é ã«ã¼ããã¤ãã)
- /
ã¹ã©ãã·ã¥ (æåéãã®ã¹ã©ãã·ã¥)
- Jan
æ (ç縮形å¼)
- /
ã¹ã©ãã·ã¥
- 2006
å¹´ (4æ¡)
- :
ã³ãã³
- 15
æ (24æéå½¢å¼)
- :
ã³ãã³
- 04
å
- :
ã³ãã³
- 05
ç§
- ã¹ãã¼ã¹
-
-0700
ã¿ã¤ã ã¾ã¼ã³ãªãã»ãã (UTCããã®ãªãã»ãã)
ãã®ãã©ã¼ãããã¯ã以ä¸ã®ãããªã¿ã¤ã ã¹ã¿ã³ããçæãã¾ãï¼
23/Jun/2024:15:04:05 -0700
åºåãµã³ãã«
127.0.0.1 - - [23/Jun/2024:20:32:45 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" 127.0.0.1 - - [23/Jun/2024:20:32:45 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" 127.0.0.1 - - [23/Jun/2024:20:32:46 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" 127.0.0.1 - - [23/Jun/2024:20:33:04 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" 127.0.0.1 - - [23/Jun/2024:20:33:05 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36" 127.0.0.1 - - [23/Jun/2024:20:33:05 +0900] "GET / http" 200 13 "" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36"