Files
matosbox/backend/internal/handlers/debug.go

53 lines
1008 B
Go

package handlers
import (
"net/http"
"os"
"github.com/gin-gonic/gin"
)
type debugLogsResponse struct {
Logs string `json:"logs"`
}
// @Summary Lire les logs backend
// @Tags Debug
// @Produce json
// @Success 200 {object} debugLogsResponse
// @Failure 500 {object} map[string]string
// @Router /debug/logs [get]
func (h *Handler) GetDebugLogs(c *gin.Context) {
logs, err := readLogTail()
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"erreur": "impossible de lire les logs"})
return
}
c.JSON(http.StatusOK, debugLogsResponse{Logs: logs})
}
func debugLogPath() string {
value := os.Getenv("DEBUG_LOG_PATH")
if value == "" {
return "./data/logs/backend.log"
}
return value
}
func readLogTail() (string, error) {
path := debugLogPath()
data, err := os.ReadFile(path)
if err != nil {
if os.IsNotExist(err) {
return "", nil
}
return "", err
}
const maxBytes = 20000
if len(data) > maxBytes {
data = data[len(data)-maxBytes:]
}
return string(data), nil
}