Skip to content

Commit

Permalink
chore: tweak logger
Browse files Browse the repository at this point in the history
  • Loading branch information
boojack committed Aug 28, 2024
1 parent de980fb commit d11bd30
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 14 deletions.
5 changes: 3 additions & 2 deletions server/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package profile

import (
"fmt"
"log/slog"
"os"
"path/filepath"
"runtime"
Expand Down Expand Up @@ -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
}
}
Expand All @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions server/runner/version/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()))
}
}

Expand Down
6 changes: 3 additions & 3 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
5 changes: 3 additions & 2 deletions store/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand Down Expand Up @@ -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") {
Expand Down
10 changes: 5 additions & 5 deletions test/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package teststore

import (
"context"
"fmt"
"log/slog"
"testing"

// sqlite driver.
Expand All @@ -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
}
Expand All @@ -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" {
Expand All @@ -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)
}
}
Expand Down

0 comments on commit d11bd30

Please sign in to comment.