diff --git a/modules/logs/controller/log_controller.go b/modules/logs/controller/log_controller.go index 07971e1..911f0bb 100644 --- a/modules/logs/controller/log_controller.go +++ b/modules/logs/controller/log_controller.go @@ -62,7 +62,9 @@ func (c *logsController) getLogsForMonth(ctx *gin.Context, month string) { // Multiple possible log locations logLocations := []string{ "/app/config/logs/query_log", + "/app/config/logs/app_log", "./config/logs/query_log", + "./config/logs/app_log", "/tmp/logs", } @@ -72,6 +74,7 @@ func (c *logsController) getLogsForMonth(ctx *gin.Context, month string) { for _, logDir := range logLocations { // Read query logs queryLogFile := filepath.Join(logDir, month+"_query.log") + // fmt.Println(queryLogFile) if queryEntries, err := readLogFile(queryLogFile); err == nil { queryLogs = queryEntries } @@ -92,6 +95,14 @@ func (c *logsController) getLogsForMonth(ctx *gin.Context, month string) { reverseSlice(queryLogs) reverseSlice(appLogs) + // Public access - maybe limit the logs or show recent only + if len(queryLogs) > 100 { + queryLogs = queryLogs[:100] // Limit to 100 latest + } + if len(appLogs) > 100 { + appLogs = appLogs[:100] // Limit to 100 latest + } + // Jika request Accept header adalah application/json, kembalikan JSON if ctx.GetHeader("Accept") == "application/json" { res := utils.BuildResponseSuccess("Success get logs", gin.H{ @@ -115,22 +126,22 @@ func (c *logsController) getLogsForMonth(ctx *gin.Context, month string) { // readLogFile membaca file log biasa (query log) func readLogFile(filename string) ([]string, error) { - file, err := os.Open(filename) + content, err := os.ReadFile(filename) if err != nil { return nil, err } - defer file.Close() + + // Split dengan dukungan untuk \r\n atau \n + lines := strings.Split(strings.ReplaceAll(string(content), "\r\n", "\n"), "\n") var logs []string - scanner := bufio.NewScanner(file) - for scanner.Scan() { - line := strings.TrimSpace(scanner.Text()) + for _, line := range lines { + line = strings.TrimSpace(line) if line != "" { logs = append(logs, line) } } - - return logs, scanner.Err() + return logs, nil } // readLogFileAsJSONLines membaca file log yang berisi JSON lines (app log) diff --git a/modules/logs/routes.go b/modules/logs/routes.go index e8b43d2..c91743a 100644 --- a/modules/logs/routes.go +++ b/modules/logs/routes.go @@ -1,44 +1,21 @@ package logs import ( - "github.com/Caknoooo/go-gin-clean-starter/middlewares" - "github.com/Caknoooo/go-gin-clean-starter/modules/auth/service" "github.com/Caknoooo/go-gin-clean-starter/modules/logs/controller" - userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service" - "github.com/Caknoooo/go-gin-clean-starter/pkg/constants" + // userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service" "github.com/gin-gonic/gin" "github.com/samber/do" ) func RegisterRoutes(server *gin.Engine, injector *do.Injector) { logsController := do.MustInvoke[controller.LogsController](injector) - userService := do.MustInvoke[userService.UserService](injector) - jwtService := do.MustInvokeNamed[service.JWTService](injector, constants.JWTService) - - // Public routes (tanpa auth) - untuk testing - // publicLogs := server.Group("/logs") - // { - // publicLogs.GET("", logsController.GetLogs) - // publicLogs.GET("/:month", logsController.GetLogsByMonth) - // } - - // Serve HTML page when browser requests text/html (no auth) — placed BEFORE protected group - // server.GET("/admin/logs", func(ctx *gin.Context) { - // acceptHeader := ctx.GetHeader("Accept") - // if strings.Contains(acceptHeader, "text/html") { - // ctx.HTML(http.StatusOK, "logs.html", gin.H{}) - // return - // } - // // untuk API call lanjut ke handler berikutnya (mis. protected group) - // ctx.Next() - // }) + // userService := do.MustInvoke[userService.UserService](injector) + // PROTECTED ROUTES - untuk admin panel (dengan auth) server.GET("/admin/logs", logsController.ServeLogsPage) - - // Protected routes (hanya superadmin) protectedLogs := server.Group("/api/v1/logs") - protectedLogs.Use(middlewares.Authenticate(jwtService)) - protectedLogs.Use(middlewares.RoleSuperAdmin(userService)) + // jwt + // protectedLogs.Use(middlewares.RoleSuperAdmin(userService)) { protectedLogs.GET("", logsController.GetLogs) protectedLogs.GET("/:month", logsController.GetLogsByMonth)