Skip to content

Commit

Permalink
support log level configuration in encore.app
Browse files Browse the repository at this point in the history
  • Loading branch information
eandre committed Dec 16, 2024
1 parent 0ac208e commit c3a25b5
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cli/daemon/apps/apps.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 8 additions & 3 deletions cli/daemon/run/runtime_config2.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type RuntimeConfigGenerator struct {
PlatformID() string
PlatformOrLocalID() string
GlobalCORS() (appfile.CORS, error)
AppFile() (*appfile.File, error)
BuildSettings() (appfile.Build, error)
}

Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/appfile/appfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion pkg/rtconfgen/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}

Expand Down
1 change: 1 addition & 0 deletions runtimes/go/appruntime/apisdk/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ func NewServer(static *config.Static, runtime *config.Runtime, rt *reqtrack.Requ
static.CORSAllowHeaders,
static.CORSExposeHeaders,
baseHandler,
rootLogger,
)
}

Expand Down
6 changes: 3 additions & 3 deletions runtimes/go/appruntime/apisdk/cors/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c3a25b5

Please sign in to comment.