From 44dcee705fa9582238aa7db8b4b5b5f1a0fec2b6 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 2 Aug 2022 15:41:42 +0200 Subject: [PATCH] SONAR-17138 sonar.log rotation move inside Sonar Application process --- .../instance-administration/system-info.md | 14 --------- .../org/sonar/application/AppLogging.java | 30 ++----------------- .../org/sonar/application/AppLoggingTest.java | 24 ++++----------- 3 files changed, 7 insertions(+), 61 deletions(-) diff --git a/server/sonar-docs/src/pages/instance-administration/system-info.md b/server/sonar-docs/src/pages/instance-administration/system-info.md index a7386de183b..68a1ba3eaf4 100644 --- a/server/sonar-docs/src/pages/instance-administration/system-info.md +++ b/server/sonar-docs/src/pages/instance-administration/system-info.md @@ -45,20 +45,6 @@ To control log rolling, use the `sonar.log.rollingPolicy` `sonar.log.maxFiles` is the maximum number of files to keep. This property is ignored if `sonar.log.rollingPolicy=none`. -#### **Wrapper Config** - -If Sonarqube was started using the SonarQube wrapper (for example, by using the provided start and stop scripts), the log rotation of the main Process (sonar.log) needs to be defined in the `wrapper.conf`. -By Default, the wrapper will rotate the `sonar.log` file each day if there is new content to be logged. - -The log rotation in the wrapper can be fine-tuned with the following properties: - -* **`wrapper.logfile.maxsize=value[m for mb, k for kb]`** -* **`wrapper.logfile.maxfiles=value`** -* **`wrapper.logfile.rollmode=DATE|SIZE`** - -`wrapper.logfile.maxsize` and `wrapper.logfile.maxfiles` are only considered if `wrapper.logfile.rollmode` is set to `SIZE`. -For `wrapper.logfile.rollmode=DATE` to work properly, the file defined with the property `wrapper.logfile` needs to include a "YYYYMMDD" Token. - ### UI Access to Logs and Log Levels The System Info page gives you the ability to download your instance's current log files (log files rotate on a regular basis), and to tune the log level via controls at the top of the page. Changes made here are temporary, and last only until the next time the instance is restarted, at which point the level will be reset to the more permanent value set in _$SONARQUBE-HOME/conf/sonar.properties_. Regardless, if you change your log level _from_ `INFO`, but sure to change it back as soon as is practical; log files can get very large very quickly at lower log levels. diff --git a/server/sonar-main/src/main/java/org/sonar/application/AppLogging.java b/server/sonar-main/src/main/java/org/sonar/application/AppLogging.java index 87e26824b25..930f0cae918 100644 --- a/server/sonar-main/src/main/java/org/sonar/application/AppLogging.java +++ b/server/sonar-main/src/main/java/org/sonar/application/AppLogging.java @@ -146,11 +146,8 @@ public class AppLogging { helper.enableJulChangePropagation(ctx); configureConsole(ctx); - if (helper.isAllLogsToConsoleEnabled(appSettings.getProps()) || !appSettings.getProps().valueAsBoolean("sonar.wrapped", false)) { - configureWithLogbackWritingToFile(ctx); - } else { - configureWithWrapperWritingToFile(ctx); - } + configureWithLogbackWritingToFile(ctx); + helper.apply( LogLevelConfig.newBuilder(helper.getRootLoggerName()) .rootLevelFor(ProcessId.APP) @@ -200,29 +197,6 @@ public class AppLogging { startupLogger.addAppender(helper.newConsoleAppender(ctx, GOBBLER_PLAIN_CONSOLE, encoder)); } - /** - * SQ has been started by the wrapper (ie. with sonar.sh) therefor, APP's System.out (and System.err) are written to - * sonar.log by the wrapper. - */ - private void configureWithWrapperWritingToFile(LoggerContext ctx) { - // configure all logs (ie. root logger) to be written to console with formatting - // in practice, this will be only APP's own logs as logs from sub processes are written to LOGGER_GOBBLER and - // LOGGER_GOBBLER is configured below to be detached from root - // logs are written to the console because we want them to be in sonar.log and the wrapper will write any log - // from APP's System.out and System.err to sonar.log - Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME); - Encoder encoder = helper.createEncoder(appSettings.getProps(), rootLoggerConfig, ctx); - rootLogger.addAppender(createAppConsoleAppender(ctx, encoder)); - - // in regular configuration, sub processes are not copying their logs to their System.out, so, the only logs to be - // expected in LOGGER_GOBBLER are those before logback is setup in subprocesses or when JVM crashes - // so, they must be printed to App's System.out as is (as they are already formatted) and the wrapper will write - // them to sonar.log - // logger is configured to be non additive as we don't want these logs written to sonar.log and duplicated in the - // console with an incorrect formatting - configureGobbler(ctx); - } - /** * Configure the logger to which logs from sub processes are written to * (called {@link StreamGobbler#LOGGER_GOBBLER}) by {@link StreamGobbler}, diff --git a/server/sonar-main/src/test/java/org/sonar/application/AppLoggingTest.java b/server/sonar-main/src/test/java/org/sonar/application/AppLoggingTest.java index 5b9a9a57d25..46584e8b7f0 100644 --- a/server/sonar-main/src/test/java/org/sonar/application/AppLoggingTest.java +++ b/server/sonar-main/src/test/java/org/sonar/application/AppLoggingTest.java @@ -73,35 +73,25 @@ public class AppLoggingTest { new LogbackHelper().resetFromXml("/logback-test.xml"); } - @Test - public void no_writing_to_sonar_log_file_when_running_from_sonar_script() { - emulateRunFromSonarScript(); - - LoggerContext ctx = underTest.configure(); - - ctx.getLoggerList().forEach(AppLoggingTest::verifyNoFileAppender); - } - @Test public void root_logger_only_writes_to_console_with_formatting_when_running_from_sonar_script() { - emulateRunFromSonarScript(); - LoggerContext ctx = underTest.configure(); Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME); - ConsoleAppender consoleAppender = (ConsoleAppender) rootLogger.getAppender("APP_CONSOLE"); + var consoleAppender = (ConsoleAppender) rootLogger.getAppender("APP_CONSOLE"); verifyAppFormattedLogEncoder(consoleAppender.getEncoder()); - assertThat(rootLogger.iteratorForAppenders()).toIterable().hasSize(1); + var rollingFileAppender = rootLogger.getAppender("file_sonar"); + assertThat(rollingFileAppender).isNotNull(); + assertThat(rootLogger.iteratorForAppenders()).toIterable().hasSize(2); } @Test public void gobbler_logger_writes_to_console_without_formatting_when_running_from_sonar_script() { - emulateRunFromSonarScript(); - LoggerContext ctx = underTest.configure(); Logger gobblerLogger = ctx.getLogger(LOGGER_GOBBLER); verifyGobblerConsoleAppender(gobblerLogger); + assertThat(gobblerLogger.iteratorForAppenders()).toIterable().hasSize(1); } @@ -266,10 +256,6 @@ public class AppLoggingTest { assertThat(((LayoutWrappingEncoder)encoder).getLayout()).isInstanceOf(LogbackJsonLayout.class); } - private void emulateRunFromSonarScript() { - settings.getProps().set("sonar.wrapped", "true"); - } - private void emulateRunFromCommandLine(boolean withAllLogsPrintedToConsole) { if (withAllLogsPrintedToConsole) { settings.getProps().set("sonar.log.console", "true"); -- 2.39.5