]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7134 make ServerProcessLogging support other process than web
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Tue, 8 Mar 2016 13:11:40 +0000 (14:11 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Mon, 21 Mar 2016 15:44:03 +0000 (16:44 +0100)
server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java
server/sonar-server/src/test/java/org/sonar/server/app/ServerProcessLoggingTest.java

index af607b7c60fa32310dcd9f407ff34f8f5dfb91e0..fbbd8b49f6d088ae3f69662ea5baf0b854bd8691 100644 (file)
@@ -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<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", LOG_FORMAT, new CeLogDenyFilter<ILoggingEvent>());
+    String logFormat = LOG_FORMAT.replace("XXXX", processName);
+    ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", logFormat, new CeLogDenyFilter<ILoggingEvent>());
     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);
   }
index 9bf70f385161ce2f1c5ff4c10de9bec995ffa29a..7fce78e110ba03aefb3361f0ea60c594ddcab6e6 100644 (file)
@@ -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);
   }