2020-10-30 04:17:04 +03:00
|
|
|
package admin
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/owncast/owncast/logging"
|
|
|
|
"github.com/sirupsen/logrus"
|
2020-11-15 05:39:53 +03:00
|
|
|
log "github.com/sirupsen/logrus"
|
2020-10-30 04:17:04 +03:00
|
|
|
)
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// GetLogs will return all logs.
|
2020-10-30 04:17:04 +03:00
|
|
|
func GetLogs(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logs := logging.Logger.AllEntries()
|
|
|
|
response := make([]logsResponse, 0)
|
|
|
|
|
|
|
|
for i := 0; i < len(logs); i++ {
|
|
|
|
response = append(response, fromEntry(logs[i]))
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-07-09 21:16:44 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-10-30 04:17:04 +03:00
|
|
|
}
|
|
|
|
|
2020-11-13 02:14:59 +03:00
|
|
|
// GetWarnings will return only warning and error logs.
|
2020-10-30 04:17:04 +03:00
|
|
|
func GetWarnings(w http.ResponseWriter, r *http.Request) {
|
|
|
|
logs := logging.Logger.WarningEntries()
|
|
|
|
response := make([]logsResponse, 0)
|
|
|
|
|
|
|
|
for i := 0; i < len(logs); i++ {
|
2020-12-06 03:55:19 +03:00
|
|
|
logEntry := logs[i]
|
|
|
|
if logEntry != nil {
|
|
|
|
response = append(response, fromEntry(logEntry))
|
|
|
|
}
|
2020-10-30 04:17:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json")
|
2021-07-09 21:16:44 +03:00
|
|
|
if err := json.NewEncoder(w).Encode(response); err != nil {
|
2020-11-15 05:39:53 +03:00
|
|
|
log.Errorln(err)
|
|
|
|
}
|
2020-10-30 04:17:04 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type logsResponse struct {
|
2023-05-30 20:31:43 +03:00
|
|
|
Time time.Time `json:"time"`
|
2020-10-30 04:17:04 +03:00
|
|
|
Message string `json:"message"`
|
|
|
|
Level string `json:"level"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func fromEntry(e *logrus.Entry) logsResponse {
|
|
|
|
return logsResponse{
|
|
|
|
Message: e.Message,
|
|
|
|
Level: e.Level.String(),
|
|
|
|
Time: e.Time,
|
|
|
|
}
|
|
|
|
}
|