diff --git a/config/config.go b/config/config.go index ca708883684..8a06be48946 100644 --- a/config/config.go +++ b/config/config.go @@ -1138,3 +1138,36 @@ type Revision struct { Revision string LastUpdated time.Time } + +// UpdateLoggerRegistryFromConfig will update the passed in registry with all log patterns in +// `cfg.LogConfig` and each resource's `LogConfiguration` field if present. +func UpdateLoggerRegistryFromConfig(registry *logging.Registry, cfg *Config, warnLogger logging.Logger) { + var combinedLogCfg []logging.LoggerPatternConfig + if cfg.LogConfig != nil { + combinedLogCfg = append(combinedLogCfg, cfg.LogConfig...) + } + + for _, serv := range cfg.Services { + if serv.LogConfiguration != nil { + resLogCfg := logging.LoggerPatternConfig{ + Pattern: "rdk.resource_manager." + serv.ResourceName().String(), + Level: serv.LogConfiguration.Level.String(), + } + combinedLogCfg = append(combinedLogCfg, resLogCfg) + } + } + for _, comp := range cfg.Components { + if comp.LogConfiguration != nil { + resLogCfg := logging.LoggerPatternConfig{ + Pattern: "rdk.resource_manager." + comp.ResourceName().String(), + Level: comp.LogConfiguration.Level.String(), + } + combinedLogCfg = append(combinedLogCfg, resLogCfg) + } + } + + if err := registry.Update(combinedLogCfg, warnLogger); err != nil { + warnLogger.Warnw("Error processing log patterns", + "error", err) + } +} diff --git a/logging/log_config.go b/logging/log_config.go index 2698f30d8f3..5156574e13f 100644 --- a/logging/log_config.go +++ b/logging/log_config.go @@ -12,34 +12,34 @@ type LoggerPatternConfig struct { } const ( - // Pattern matching on loggers. Examples describe regular expression below. + // Regular expressions for logger names (non-resource loggers). Examples + // describe the regular expression that follows. - // e.g. "foo" + // e.g. "foo". validLoggerSectionName = `[a-zA-Z0-9]+([_-]*[a-zA-Z0-9]+)*` - // e.g. "foo" or "*" + // e.g. "foo" or "*". validLoggerSectionNameWithWildcard = `(` + validLoggerSectionName + `|\*)` - // e.g. "foo.bar" - validLoggerSections = validLoggerSectionName + `(\.` + validLoggerSectionName + `)*` - // e.g. "foo.*.foo" + // e.g. "foo.*.foo". validLoggerSectionsWithWildcard = validLoggerSectionNameWithWildcard + `(\.` + validLoggerSectionNameWithWildcard + `)*` // Restricts above regex to be the entire pattern. validLoggerName = `^` + validLoggerSectionsWithWildcard + `$` - // Resource configurations. Examples describe regular expression below. + // Regular expressions for resource logger names. Examples describe the + // regular expression that follows. - // e.g. "foo-bar" + // e.g. "foo-bar". validNamespacePattern = `([\w-]+|\*)` - // e.g. "service" or "component" or "*" + // e.g. "service" or "component" or "*". validResourceTypePattern = `(service|component|\*)` - // e.g. "foo-bar" + // e.g. "foo-bar". validResourceSubTypePattern = validNamespacePattern - // e.g. "foo-bar" + // e.g. "foo-bar". validModelNamePattern = validNamespacePattern - // e.g. "service:foo" or "remote:" + // e.g. "service:foo" or "remote:". validTypeSubsectionPattern = `(` + validResourceTypePattern + `:` + validResourceSubTypePattern + `|remote:)` - // e.g. "rdk.resource_manager.rdk:component:motor/foo" + // e.g. "rdk.resource_manager.rdk:component:motor/foo". validResourcePattern = `^rdk.resource_manager.` + validNamespacePattern + `:` + validTypeSubsectionPattern + `\/` + - validModelNamePattern + `$` // e.g. "rdk.resource_manager.rdk:component:motor/foo" + validModelNamePattern + `$` ) var ( diff --git a/web/server/entrypoint.go b/web/server/entrypoint.go index cf13ffa93aa..50fe598d09f 100644 --- a/web/server/entrypoint.go +++ b/web/server/entrypoint.go @@ -209,37 +209,6 @@ func (s *robotServer) createWebOptions(cfg *config.Config) (weboptions.Options, return options, nil } -func (s *robotServer) updateLoggerRegistry(cfg *config.Config) { - var combinedLogCfg []logging.LoggerPatternConfig - if cfg.LogConfig != nil { - combinedLogCfg = append(combinedLogCfg, cfg.LogConfig...) - } - - for _, serv := range cfg.Services { - if serv.LogConfiguration != nil { - resLogCfg := logging.LoggerPatternConfig{ - Pattern: "rdk.resource_manager." + serv.ResourceName().String(), - Level: serv.LogConfiguration.Level.String(), - } - combinedLogCfg = append(combinedLogCfg, resLogCfg) - } - } - for _, comp := range cfg.Components { - if comp.LogConfiguration != nil { - resLogCfg := logging.LoggerPatternConfig{ - Pattern: "rdk.resource_manager." + comp.ResourceName().String(), - Level: comp.LogConfiguration.Level.String(), - } - combinedLogCfg = append(combinedLogCfg, resLogCfg) - } - } - - if err := s.registry.Update(combinedLogCfg, s.logger); err != nil { - s.logger.Errorw("Error processing log patterns", - "error", err) - } -} - func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err error) { ctx, cancel := context.WithCancel(ctx) @@ -337,7 +306,7 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err // Update logger registry as soon as we have fully processed config. Further // updates to the registry will be handled by the config watcher goroutine. - s.updateLoggerRegistry(processedConfig) + config.UpdateLoggerRegistryFromConfig(s.registry, processedConfig, s.logger) if processedConfig.Cloud != nil { cloudRestartCheckerActive = make(chan struct{}) @@ -442,7 +411,7 @@ func (s *robotServer) serveWeb(ctx context.Context, cfg *config.Config) (err err // Update logger registry if log patterns may have changed. if !diff.LogEqual { s.logger.Debug("Detected potential changes to log patterns; updating logger levels") - s.updateLoggerRegistry(processedConfig) + config.UpdateLoggerRegistryFromConfig(s.registry, processedConfig, s.logger) } myRobot.Reconfigure(ctx, processedConfig)