From 37c03b26859c6ad3b56442b7f5a0e039526f5eb5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Tue, 8 Mar 2016 14:11:40 +0100 Subject: [PATCH] SONAR-7134 make ServerProcessLogging support other process than web --- .../server/app/ServerProcessLogging.java | 23 ++++++++++++------- .../server/app/ServerProcessLoggingTest.java | 9 +++++--- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java index af607b7c60f..fbbd8b49f6d 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java +++ b/server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java @@ -34,17 +34,23 @@ import org.sonar.server.computation.log.CeLogging; import org.sonar.server.platform.ServerLogging; /** - * Configure logback for web server process. Logs must be written to console, which is + * Configure logback for given server sub process (Web Server or CE). Logs must be written to console, which is * forwarded to file logs/sonar.log by the app master process. */ -class ServerProcessLogging { +public class ServerProcessLogging { - private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level web[%logger{20}] %msg%n"; - public static final String LOG_LEVEL_PROPERTY = "sonar.log.level"; + private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level XXXX[%logger{20}] %msg%n"; private final LogbackHelper helper = new LogbackHelper(); + private final String processName; + private final String logLevelProperty; - LoggerContext configure(Props props) { + public ServerProcessLogging(String processName, String logLevelProperty) { + this.processName = processName; + this.logLevelProperty = logLevelProperty; + } + + public LoggerContext configure(Props props) { LoggerContext ctx = helper.getRootContext(); ctx.reset(); @@ -59,14 +65,15 @@ class ServerProcessLogging { } private void configureAppender(LoggerContext ctx, Props props) { - ConsoleAppender consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", LOG_FORMAT, new CeLogDenyFilter()); + String logFormat = LOG_FORMAT.replace("XXXX", processName); + ConsoleAppender consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", logFormat, new CeLogDenyFilter()); ctx.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(consoleAppender); ctx.getLogger(Logger.ROOT_LOGGER_NAME).addAppender(CeLogging.createAppenderConfiguration(ctx, props)); } private void configureLevels(Props props) { - String levelCode = props.value(LOG_LEVEL_PROPERTY, "INFO"); + String levelCode = props.value(logLevelProperty, "INFO"); LoggerLevel level; if ("TRACE".equals(levelCode)) { level = LoggerLevel.TRACE; @@ -75,7 +82,7 @@ class ServerProcessLogging { } else if ("INFO".equals(levelCode)) { level = LoggerLevel.INFO; } else { - throw MessageException.of(String.format("Unsupported log level: %s. Please check property %s", levelCode, LOG_LEVEL_PROPERTY)); + throw MessageException.of(String.format("Unsupported log level: %s. Please check property %s", levelCode, logLevelProperty)); } ServerLogging.configureLevels(helper, level); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java index 9bf70f38516..7fce78e110b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java @@ -40,11 +40,14 @@ import static org.assertj.core.api.Assertions.assertThat; public class ServerProcessLoggingTest { + private static final String LOG_LEVEL_PROPERTY = "log.level"; + private static final String PROCESS_NAME = "pr1"; + @Rule public TemporaryFolder temp = new TemporaryFolder(); Props props = new Props(new Properties()); - ServerProcessLogging underTest = new ServerProcessLogging(); + ServerProcessLogging underTest = new ServerProcessLogging(PROCESS_NAME, LOG_LEVEL_PROPERTY); /** * Path to data dir must be set for Compute Engine logging. @@ -76,14 +79,14 @@ public class ServerProcessLoggingTest { @Test public void enable_debug_logs() { - props.set("sonar.log.level", "DEBUG"); + props.set(LOG_LEVEL_PROPERTY, "DEBUG"); LoggerContext ctx = underTest.configure(props); assertThat(ctx.getLogger(Logger.ROOT_LOGGER_NAME).getLevel()).isEqualTo(Level.DEBUG); } @Test public void enable_trace_logs() { - props.set("sonar.log.level", "TRACE"); + props.set(LOG_LEVEL_PROPERTY, "TRACE"); LoggerContext ctx = underTest.configure(props); assertThat(ctx.getLogger(Logger.ROOT_LOGGER_NAME).getLevel()).isEqualTo(Level.TRACE); } -- 2.39.5