2020-05-19 13:48:05 +02:00
|
|
|
package entity
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Error represents an error message log.
|
|
|
|
type Error struct {
|
2020-07-01 17:16:24 +02:00
|
|
|
ID uint `gorm:"primary_key" json:"ID" yaml:"ID"`
|
|
|
|
ErrorTime time.Time `sql:"index" json:"Time" yaml:"Time"`
|
2020-09-13 17:51:43 +02:00
|
|
|
ErrorLevel string `gorm:"type:VARBINARY(32)" json:"Level" yaml:"Level"`
|
|
|
|
ErrorMessage string `gorm:"type:VARBINARY(2048)" json:"Message" yaml:"Message"`
|
2020-05-19 13:48:05 +02:00
|
|
|
}
|
|
|
|
|
2020-07-01 17:16:24 +02:00
|
|
|
type Errors []Error
|
|
|
|
|
2020-05-19 13:48:05 +02:00
|
|
|
// SaveErrorMessages subscribes to error logs and stored them in the errors table.
|
|
|
|
func SaveErrorMessages() {
|
|
|
|
s := event.Subscribe("log.*")
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
event.Unsubscribe(s)
|
|
|
|
}()
|
|
|
|
|
|
|
|
for msg := range s.Receiver {
|
|
|
|
level, ok := msg.Fields["level"]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
logLevel, err := logrus.ParseLevel(level.(string))
|
|
|
|
|
|
|
|
if err != nil || logLevel >= logrus.InfoLevel {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
newError := Error{ErrorLevel: logLevel.String()}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
if val, ok := msg.Fields["message"]; ok {
|
2020-05-19 13:48:05 +02:00
|
|
|
newError.ErrorMessage = val.(string)
|
|
|
|
}
|
|
|
|
|
|
|
|
if val, ok := msg.Fields["time"]; ok {
|
|
|
|
newError.ErrorTime = val.(time.Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
Db().Create(&newError)
|
|
|
|
}
|
|
|
|
}
|