From 7ecc0c6153674aa0eb5d5f43ec0343920fdad730 Mon Sep 17 00:00:00 2001 From: Jenny Zhu Date: Wed, 2 Oct 2024 23:18:21 -0400 Subject: [PATCH] OU-525: Reduce excess logs --- README.md | 4 ++++ cmd/plugin-backend.go | 3 +++ pkg/server/server.go | 18 +++++++++++++++--- 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a00750fb2..9a399e49f 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,10 @@ to `http://localhost:3100`. You can disable this by re-running the console with Navigate to to see the running plugin. +### Local Development Troubleshooting +1. Disable cache. Select 'disable cache' in your browser's DevTools > Network > 'disable cache'. Or use private/incognito mode in your browser. +2. Enable higher log verbosity by setting `-log-level=trace` when starting the plugin backend. For more options to set log level see [logrus documentation](https://github.com/sirupsen/logrus?tab=readme-ov-file#level-logging). + ### Running tests #### Unit tests diff --git a/cmd/plugin-backend.go b/cmd/plugin-backend.go index 551efd087..8e8463636 100644 --- a/cmd/plugin-backend.go +++ b/cmd/plugin-backend.go @@ -18,6 +18,7 @@ var ( staticPathArg = flag.String("static-path", "", "static files path to serve frontend (default: './web/dist')") configPathArg = flag.String("config-path", "", "config files path (default: './config')") pluginConfigArg = flag.String("plugin-config-path", "", "plugin yaml configuration") + logLevelArg = flag.String("log-level", "error", "verbosity of logs\noptions: ['panic', 'fatal', 'error', 'warn', 'info', 'debug', 'trace']\n'trace' level will log all incoming requests\n(default 'error')") log = logrus.WithField("module", "main") ) @@ -31,6 +32,7 @@ func main() { staticPath := mergeEnvValue("LOGGING_VIEW_PLUGIN_STATIC_PATH", *staticPathArg, "./web/dist") configPath := mergeEnvValue("LOGGING_VIEW_PLUGIN_MANIFEST_CONFIG_PATH", *configPathArg, "./config") pluginConfigPath := mergeEnvValue("LOGGING_VIEW_PLUGIN_CONFIG_PATH", *pluginConfigArg, "/etc/plugin/config.yaml") + logLevel := mergeEnvValue("LOGGING_VIEW_PLUGIN_LOG_LEVEL", *logLevelArg, "error") featuresList := strings.Fields(strings.Join(strings.Split(strings.ToLower(features), ","), " ")) @@ -49,6 +51,7 @@ func main() { StaticPath: staticPath, ConfigPath: configPath, PluginConfigPath: pluginConfigPath, + LogLevel: logLevel, }) } diff --git a/pkg/server/server.go b/pkg/server/server.go index 2a133d727..f5d73c68c 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -25,6 +25,7 @@ type Config struct { StaticPath string ConfigPath string PluginConfigPath string + LogLevel string } type PluginConfig struct { @@ -51,26 +52,37 @@ func Start(cfg *Config) { router := setupRoutes(cfg) router.Use(corsHeaderMiddleware(cfg)) - loggedRouter := handlers.LoggingHandler(slog.Logger.Out, router) - // clients must use TLS 1.2 or higher tlsConfig := &tls.Config{ MinVersion: tls.VersionTLS12, } + logrusLevel, err := logrus.ParseLevel(cfg.LogLevel) + if err != nil { + logrus.WithError(err).Fatal("unable to set the log level") + logrusLevel = logrus.ErrorLevel + } + httpServer := &http.Server{ - Handler: loggedRouter, + Handler: router, Addr: fmt.Sprintf(":%d", cfg.Port), TLSConfig: tlsConfig, ReadTimeout: 30 * time.Second, WriteTimeout: 30 * time.Second, } + if logrusLevel == logrus.TraceLevel { + loggedRouter := handlers.LoggingHandler(slog.Logger.Out, router) + httpServer.Handler = loggedRouter + } + if cfg.CertFile != "" && cfg.PrivateKeyFile != "" { slog.Infof("listening on https://:%d", cfg.Port) + logrus.SetLevel(logrusLevel) panic(httpServer.ListenAndServeTLS(cfg.CertFile, cfg.PrivateKeyFile)) } else { slog.Infof("listening on http://:%d", cfg.Port) + logrus.SetLevel(logrusLevel) panic(httpServer.ListenAndServe()) } }