From e80a6753a54631f7380452c6189c8698d76b3913 Mon Sep 17 00:00:00 2001 From: ari melody Date: Thu, 6 Feb 2025 12:32:51 +0000 Subject: [PATCH] create log class, edit fatal-but-not-really logs --- admin/releasehttp.go | 8 +++--- api/api.go | 6 ++--- log/log.go | 58 ++++++++++++++++++++++++++++++++++++++++++++ view/music.go | 2 +- 4 files changed, 66 insertions(+), 8 deletions(-) create mode 100644 log/log.go diff --git a/admin/releasehttp.go b/admin/releasehttp.go index be5052b..7ef4d37 100644 --- a/admin/releasehttp.go +++ b/admin/releasehttp.go @@ -22,7 +22,7 @@ func serveRelease(app *model.AppState) http.Handler { http.NotFound(w, r) return } - fmt.Printf("FATAL: Failed to pull full release data for %s: %s\n", releaseID, err) + fmt.Printf("WARN: Failed to pull full release data for %s: %s\n", releaseID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -86,7 +86,7 @@ func serveAddCredit(app *model.AppState, release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { artists, err := controller.GetArtistsNotOnRelease(app.DB, release.ID) if err != nil { - fmt.Printf("FATAL: Failed to pull artists not on %s: %s\n", release.ID, err) + fmt.Printf("WARN: Failed to pull artists not on %s: %s\n", release.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -113,7 +113,7 @@ func serveNewCredit(app *model.AppState) http.Handler { artistID := strings.Split(r.URL.Path, "/")[3] artist, err := controller.GetArtist(app.DB, artistID) if err != nil { - fmt.Printf("FATAL: Failed to pull artists %s: %s\n", artistID, err) + fmt.Printf("WARN: Failed to pull artists %s: %s\n", artistID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -166,7 +166,7 @@ func serveAddTrack(app *model.AppState, release *model.Release) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { tracks, err := controller.GetTracksNotOnRelease(app.DB, release.ID) if err != nil { - fmt.Printf("FATAL: Failed to pull tracks not on %s: %s\n", release.ID, err) + fmt.Printf("WARN: Failed to pull tracks not on %s: %s\n", release.ID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } diff --git a/api/api.go b/api/api.go index 50b1c63..9a2c340 100644 --- a/api/api.go +++ b/api/api.go @@ -27,7 +27,7 @@ func Handler(app *model.AppState) http.Handler { http.NotFound(w, r) return } - fmt.Printf("FATAL: Error while retrieving artist %s: %s\n", artistID, err) + fmt.Printf("WARN: Error while retrieving artist %s: %s\n", artistID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -69,7 +69,7 @@ func Handler(app *model.AppState) http.Handler { http.NotFound(w, r) return } - fmt.Printf("FATAL: Error while retrieving release %s: %s\n", releaseID, err) + fmt.Printf("WARN: Error while retrieving release %s: %s\n", releaseID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } @@ -111,7 +111,7 @@ func Handler(app *model.AppState) http.Handler { http.NotFound(w, r) return } - fmt.Printf("FATAL: Error while retrieving track %s: %s\n", trackID, err) + fmt.Printf("WARN: Error while retrieving track %s: %s\n", trackID, err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return } diff --git a/log/log.go b/log/log.go new file mode 100644 index 0000000..734cc9f --- /dev/null +++ b/log/log.go @@ -0,0 +1,58 @@ +package log + +import ( + "fmt" + "os" + "time" + + "github.com/jmoiron/sqlx" +) + +type ( + Logger struct { + DB *sqlx.DB + } + + Log struct { + ID string `json:"id" db:"id"` + Type string `json:"type" db:"type"` + Content string `json:"content" db:"content"` + CreatedAt time.Time `json:"created_at" db:"created_at"` + } +) + +const ( + TYPE_ACCOUNT = "account" +) + +func (self *Logger) Info(logType string, format string, args ...any) { + fmt.Printf(fmt.Sprintf("[%s] INFO: %s", logType, format), args...) + // TODO: push logs to DB +} + +func (self *Logger) Warn(logType string, format string, args ...any) { + fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] WARN: %s", logType, format), args...) + // TODO: push logs to DB +} + +func (self *Logger) Fatal(logType string, format string, args ...any) { + fmt.Fprintf(os.Stderr, fmt.Sprintf("[%s] FATAL: %s", logType, format), args...) + // we won't need to push fatal logs to DB, as these usually precede a panic or crash +} + +func (self *Logger) Fetch(id string) *Log { + // TODO: log fetch + return nil +} + +func (self *Logger) Search(typeFilters []string, content string, offset int, limit int) []Log { + // TODO: log search + return []Log{} +} + +func (self *Logger) Delete(id string) error { + // TODO: log deletion + // consider: logging the deletion of logs? + // or just not deleting logs at all + return nil +} diff --git a/view/music.go b/view/music.go index 89e428c..2d40ef0 100644 --- a/view/music.go +++ b/view/music.go @@ -37,7 +37,7 @@ func ServeCatalog(app *model.AppState) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { releases, err := controller.GetAllReleases(app.DB, true, 0, true) if err != nil { - fmt.Printf("FATAL: Failed to pull releases for catalog: %s\n", err) + fmt.Printf("WARN: Failed to pull releases for catalog: %s\n", err) http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError) return }