From f1a6c1930935e3f8bce0286e098e2c236074bfeb Mon Sep 17 00:00:00 2001 From: Gani Georgiev Date: Mon, 26 Feb 2024 16:39:35 +0200 Subject: [PATCH] fixed logs printer dev tests --- CHANGELOG.md | 2 ++ core/base.go | 35 ++++++++++++++++++++++------------- core/base_backup.go | 7 ++++++- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c20c3bce..a94977b3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -33,6 +33,8 @@ - Updated the `cron.Start()` to start the ticker at the `00` second of the cron interval ([#4394](https://github.com/pocketbase/pocketbase/discussions/4394)). _Note that the cron format has only minute granularity and there is still no guarantee that the sheduled job will be always executed at the `00` second._ +- Fixed cron auto backups not taking in consideration the latest settings change ([#4431](https://github.com/pocketbase/pocketbase/discussions/4431)). + - Upgraded to `aws-sdk-go-v2` and added special handling for GCS to workaround the previous [GCS headers signature issue](https://github.com/pocketbase/pocketbase/issues/2231) that we had with v2. _This should also fix the SVG/JSON zero response when using Cloudflare R2 ([#4287](https://github.com/pocketbase/pocketbase/issues/4287#issuecomment-1925168142), [#2068](https://github.com/pocketbase/pocketbase/discussions/2068), [#2952](https://github.com/pocketbase/pocketbase/discussions/2952))._ _If you are using S3 for uploaded files or backups, please verify that you have a green check in the Admin UI for your S3 configuration (I've tested the new version with GCS, MinIO, Cloudflare R2 and Wasabi)._ diff --git a/core/base.go b/core/base.go index 9a246d10..5785b1f3 100644 --- a/core/base.go +++ b/core/base.go @@ -599,10 +599,10 @@ func (app *BaseApp) RefreshSettings() error { return err } - // reload handler level (if initialized and not in dev mode) - if !app.IsDev() && app.Logger() != nil { + // reload handler level (if initialized) + if app.Logger() != nil { if h, ok := app.Logger().Handler().(*logger.BatchHandler); ok { - h.SetLevel(slog.Level(app.settings.Logs.MinLevel)) + h.SetLevel(app.getLoggerMinLevel()) } } @@ -1184,25 +1184,34 @@ func (app *BaseApp) registerDefaultHooks() { } } -func (app *BaseApp) initLogger() error { - duration := 3 * time.Second - ticker := time.NewTicker(duration) - done := make(chan bool) +// getLoggerMinLevel returns the logger min level based on the +// app configurations (dev mode, settings, etc.). - // Apply the min level only if it is not in develop - // to allow printing the logs to the console. - // - // DB logs are still filtered but the checks for the min level are done - // in the BatchOptions.BeforeAddFunc instead of the slog.Handler.Enabled() method. +// If not in dev mode - returns the level from the app settings. +// +// If the app is in dev mode it returns -9999 level allowing to print +// practically all logs to the terminal. +// In this case DB logs are still filtered but the checks for the min level are done +// in the BatchOptions.BeforeAddFunc instead of the slog.Handler.Enabled() method. +func (app *BaseApp) getLoggerMinLevel() slog.Level { var minLevel slog.Level + if app.IsDev() { minLevel = -9999 } else if app.Settings() != nil { minLevel = slog.Level(app.Settings().Logs.MinLevel) } + return minLevel +} + +func (app *BaseApp) initLogger() error { + duration := 3 * time.Second + ticker := time.NewTicker(duration) + done := make(chan bool) + handler := logger.NewBatchHandler(logger.BatchOptions{ - Level: minLevel, + Level: app.getLoggerMinLevel(), BatchSize: 200, BeforeAddFunc: func(ctx context.Context, log *logger.Log) bool { if app.IsDev() { diff --git a/core/base_backup.go b/core/base_backup.go index 860de370..76820756 100644 --- a/core/base_backup.go +++ b/core/base_backup.go @@ -251,7 +251,12 @@ func (app *BaseApp) initAutobackupHooks() error { // make sure that app.Settings() is always up to date // // @todo remove with the refactoring as core.App and daos.Dao will be one. - app.RefreshSettings() + if err := app.RefreshSettings(); err != nil { + app.Logger().Debug( + "[Backup cron] Failed to get the latest app settings", + slog.String("error", err.Error()), + ) + } rawSchedule := app.Settings().Backups.Cron if rawSchedule == "" || !isServe || !app.IsBootstrapped() {