From c3a25b5791301367f867fb193a645b2fee368175 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Eriksson?= Date: Mon, 16 Dec 2024 16:08:58 +0100 Subject: [PATCH] support log level configuration in encore.app --- cli/daemon/apps/apps.go | 4 ++++ cli/daemon/run/runtime_config2.go | 11 ++++++++--- pkg/appfile/appfile.go | 4 ++++ pkg/rtconfgen/convert.go | 7 ++++++- runtimes/go/appruntime/apisdk/api/server.go | 1 + runtimes/go/appruntime/apisdk/cors/cors.go | 6 +++--- 6 files changed, 26 insertions(+), 7 deletions(-) diff --git a/cli/daemon/apps/apps.go b/cli/daemon/apps/apps.go index 9aac9d522e..8169a9f66f 100644 --- a/cli/daemon/apps/apps.go +++ b/cli/daemon/apps/apps.go @@ -383,6 +383,10 @@ func (i *Instance) Lang() appfile.Lang { return appFile.Lang } +func (i *Instance) AppFile() (*appfile.File, error) { + return appfile.ParseFile(filepath.Join(i.root, appfile.Name)) +} + func (i *Instance) BuildSettings() (appfile.Build, error) { appFile, err := appfile.ParseFile(filepath.Join(i.root, appfile.Name)) if err != nil { diff --git a/cli/daemon/run/runtime_config2.go b/cli/daemon/run/runtime_config2.go index bbd4da66a1..1f3dd580d6 100644 --- a/cli/daemon/run/runtime_config2.go +++ b/cli/daemon/run/runtime_config2.go @@ -49,6 +49,7 @@ type RuntimeConfigGenerator struct { PlatformID() string PlatformOrLocalID() string GlobalCORS() (appfile.CORS, error) + AppFile() (*appfile.File, error) BuildSettings() (appfile.Build, error) } @@ -140,13 +141,17 @@ func (g *RuntimeConfigGenerator) initialize() error { }) } - buildSettings, err := g.app.BuildSettings() + appFile, err := g.app.AppFile() if err != nil { return errors.Wrap(err, "failed to get app's build settings") } for _, svc := range g.md.Svcs { - cfg := &runtimev1.HostedService{Name: svc.Name} - if buildSettings.WorkerPooling { + cfg := &runtimev1.HostedService{ + Name: svc.Name, + LogConfig: ptrOrNil(appFile.LogLevel), + } + + if appFile.Build.WorkerPooling { n := int32(0) cfg.WorkerThreads = &n } diff --git a/pkg/appfile/appfile.go b/pkg/appfile/appfile.go index fa802f9e4f..7d812019cc 100644 --- a/pkg/appfile/appfile.go +++ b/pkg/appfile/appfile.go @@ -60,6 +60,10 @@ type File struct { // // Deprecated: Use build.docker.base_image instead. DockerBaseImage string `json:"docker_base_image,omitempty"` + + // LogLevel is the minimum log level for the app. + // If empty it defaults to "trace". + LogLevel string `json:"log_level,omitempty"` } type Build struct { diff --git a/pkg/rtconfgen/convert.go b/pkg/rtconfgen/convert.go index 72aa7e3cdb..e01969da8e 100644 --- a/pkg/rtconfgen/convert.go +++ b/pkg/rtconfgen/convert.go @@ -136,14 +136,19 @@ func (c *legacyConverter) Convert() (*config.Runtime, error) { } // Use the most verbose logging requested. - currLevel := zerolog.TraceLevel + currLevel := zerolog.PanicLevel + foundLevel := false for _, svc := range deployment.HostedServices { if svc.LogConfig != nil { if level, err := zerolog.ParseLevel(*svc.LogConfig); err == nil && level < currLevel { currLevel = level + foundLevel = true } } } + if !foundLevel { + currLevel = zerolog.TraceLevel + } cfg.LogConfig = currLevel.String() } diff --git a/runtimes/go/appruntime/apisdk/api/server.go b/runtimes/go/appruntime/apisdk/api/server.go index f2faec1fb5..0c8264c9e8 100644 --- a/runtimes/go/appruntime/apisdk/api/server.go +++ b/runtimes/go/appruntime/apisdk/api/server.go @@ -203,6 +203,7 @@ func NewServer(static *config.Static, runtime *config.Runtime, rt *reqtrack.Requ static.CORSAllowHeaders, static.CORSExposeHeaders, baseHandler, + rootLogger, ) } diff --git a/runtimes/go/appruntime/apisdk/cors/cors.go b/runtimes/go/appruntime/apisdk/cors/cors.go index 4ae885a479..aba8c97177 100644 --- a/runtimes/go/appruntime/apisdk/cors/cors.go +++ b/runtimes/go/appruntime/apisdk/cors/cors.go @@ -9,15 +9,15 @@ import ( "strings" "github.com/rs/cors" - "github.com/rs/zerolog/log" + "github.com/rs/zerolog" "encore.dev/appruntime/exported/config" ) -func Wrap(cfg *config.CORS, staticAllowedHeaders, staticExposedHeaders []string, handler http.Handler) http.Handler { +func Wrap(cfg *config.CORS, staticAllowedHeaders, staticExposedHeaders []string, handler http.Handler, logger zerolog.Logger) http.Handler { c := cors.New(Options(cfg, staticAllowedHeaders, staticExposedHeaders)) if cfg.Debug { - logger := log.With().Str("subsystem", "cors").Logger() + logger := logger.With().Str("subsystem", "cors").Logger() logger.Debug().Msg("CORS system running in debug mode. All requests will be logged.") c.Log = &logger }