Refactor log handling to use in-memory Logger

This commit is contained in:
Sergey Krashevich
2023-12-05 17:44:32 +03:00
parent ab47d5718f
commit b60000ac34
2 changed files with 15 additions and 77 deletions
+4 -12
View File
@@ -270,22 +270,14 @@ func restartHandler(w http.ResponseWriter, r *http.Request) {
func logHandler(w http.ResponseWriter, r *http.Request) {
switch r.Method {
case "GET":
logFilePath := app.GetLogFilepath()
// Send current state of the log file immediately
data, err := os.ReadFile(logFilePath)
if err != nil {
http.Error(w, "Error reading log file", http.StatusInternalServerError)
return
}
data := app.LogCollector.Bytes()
Response(w, data, "text/plain")
case "DELETE":
err := os.Truncate(app.GetLogFilepath(), 0)
if err != nil {
http.Error(w, "Error truncating log file", http.StatusServiceUnavailable)
return
}
Response(w, "Log file deleted", "text/plain")
app.LogCollector.Reset()
Response(w, "Log truncated", "text/plain")
default:
http.Error(w, "Method not allowed", http.StatusBadRequest)
}