chore: tweak logger

This commit is contained in:
Steven 2024-08-28 22:53:57 +08:00
parent de980fb7d7
commit d11bd30ec6
5 changed files with 16 additions and 14 deletions

View File

@ -2,6 +2,7 @@ package profile
import (
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
@ -68,7 +69,7 @@ func (p *Profile) Validate() error {
p.Data = filepath.Join(os.Getenv("ProgramData"), "memos")
if _, err := os.Stat(p.Data); os.IsNotExist(err) {
if err := os.MkdirAll(p.Data, 0770); err != nil {
fmt.Printf("Failed to create data directory: %s, err: %+v\n", p.Data, err)
slog.Error("failed to create data directory", slog.String("data", p.Data), slog.String("error", err.Error()))
return err
}
}
@ -79,7 +80,7 @@ func (p *Profile) Validate() error {
dataDir, err := checkDataDir(p.Data)
if err != nil {
fmt.Printf("Failed to check dsn: %s, err: %+v\n", dataDir, err)
slog.Error("failed to check dsn", slog.String("data", dataDir), slog.String("error", err.Error()))
return err
}

View File

@ -5,11 +5,11 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/pkg/errors"
"golang.org/x/exp/slog"
storepb "github.com/usememos/memos/proto/gen/store"
"github.com/usememos/memos/server/profile"
@ -115,7 +115,7 @@ func (r *Runner) Check(ctx context.Context) {
ActivityId: &activity.ID,
},
}); err != nil {
fmt.Printf("failed to create inbox: %s\n", err)
slog.Error("failed to create inbox", slog.String("error", err.Error()))
}
}

View File

@ -129,15 +129,15 @@ func (s *Server) Shutdown(ctx context.Context) {
// Shutdown echo server.
if err := s.echoServer.Shutdown(ctx); err != nil {
fmt.Printf("failed to shutdown server, error: %v\n", err)
slog.Error("failed to shutdown server", slog.String("error", err.Error()))
}
// Close database connection.
if err := s.Store.Close(); err != nil {
fmt.Printf("failed to close database, error: %v\n", err)
slog.Error("failed to close database", slog.String("error", err.Error()))
}
fmt.Printf("memos stopped properly\n")
slog.Info("memos stopped properly")
}
func (s *Server) StartBackgroundRunners(ctx context.Context) {

View File

@ -72,7 +72,7 @@ func (s *Store) Migrate(ctx context.Context) error {
}
defer tx.Rollback()
fmt.Println("start migration")
slog.Info("start migration", slog.String("currentSchemaVersion", latestMigrationHistoryVersion), slog.String("targetSchemaVersion", schemaVersion))
for _, filePath := range filePaths {
fileSchemaVersion, err := s.getSchemaVersionOfMigrateScript(filePath)
if err != nil {
@ -93,7 +93,7 @@ func (s *Store) Migrate(ctx context.Context) error {
if err := tx.Commit(); err != nil {
return errors.Wrap(err, "failed to commit transaction")
}
fmt.Println("end migrate")
slog.Info("end migrate")
// Upsert the current schema version to migration_history.
if _, err = s.driver.UpsertMigrationHistory(ctx, &UpsertMigrationHistory{
@ -255,6 +255,7 @@ func (s *Store) normalizedMigrationHistoryList(ctx context.Context) error {
sort.Sort(version.SortVersion(versions))
latestVersion := versions[len(versions)-1]
latestMinorVersion := version.GetMinorVersion(latestVersion)
// If the latest version is greater than 0.22, return.
// As of 0.22, the migration history is already normalized.
if version.IsVersionGreaterThan(latestMinorVersion, "0.22") {

View File

@ -2,7 +2,7 @@ package teststore
import (
"context"
"fmt"
"log/slog"
"testing"
// sqlite driver.
@ -18,13 +18,13 @@ func NewTestingStore(ctx context.Context, t *testing.T) *store.Store {
profile := test.GetTestingProfile(t)
dbDriver, err := db.NewDBDriver(profile)
if err != nil {
fmt.Printf("failed to create db driver, error: %+v\n", err)
slog.Error("failed to create db driver", slog.String("error", err.Error()))
}
resetTestingDB(ctx, profile, dbDriver)
store := store.New(dbDriver, profile)
if err := store.Migrate(ctx); err != nil {
fmt.Printf("failed to migrate db, error: %+v\n", err)
slog.Error("failed to migrate db", slog.String("error", err.Error()))
}
return store
}
@ -48,7 +48,7 @@ func resetTestingDB(ctx context.Context, profile *profile.Profile, dbDriver stor
DROP TABLE IF EXISTS webhook;
DROP TABLE IF EXISTS reaction;`)
if err != nil {
fmt.Printf("failed to reset testing db, error: %+v\n", err)
slog.Error("failed to reset testing db", slog.String("error", err.Error()))
panic(err)
}
} else if profile.Driver == "postgres" {
@ -69,7 +69,7 @@ func resetTestingDB(ctx context.Context, profile *profile.Profile, dbDriver stor
DROP TABLE IF EXISTS webhook CASCADE;
DROP TABLE IF EXISTS reaction CASCADE;`)
if err != nil {
fmt.Printf("failed to reset testing db, error: %+v\n", err)
slog.Error("failed to reset testing db", slog.String("error", err.Error()))
panic(err)
}
}