aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java22
-rw-r--r--server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java14
-rw-r--r--server/sonar-search/src/main/java/org/sonar/search/SearchServer.java2
-rw-r--r--server/sonar-search/src/test/java/org/sonar/search/SearchLoggingTest.java45
-rw-r--r--server/sonar-server/src/main/java/org/sonar/server/app/ServerProcessLogging.java36
-rw-r--r--server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java2
-rw-r--r--server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java2
7 files changed, 73 insertions, 50 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
index 7ba6393a3d6..8b7c8bf5150 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/LogbackHelper.java
@@ -25,6 +25,7 @@ import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.classic.jul.LevelChangePropagator;
+import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.classic.spi.LoggerContextListener;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.Context;
@@ -46,6 +47,8 @@ public class LogbackHelper {
public static final String ROLLING_POLICY_PROPERTY = "sonar.log.rollingPolicy";
public static final String MAX_FILES_PROPERTY = "sonar.log.maxFiles";
+ private static final String THREAD_ID_PLACEHOLDER = "ZZZZ";
+ private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level [" + THREAD_ID_PLACEHOLDER + "][%logger{20}] %msg%n";
public LoggerContext getRootContext() {
org.slf4j.Logger logger;
@@ -73,6 +76,25 @@ public class LogbackHelper {
return propagator;
}
+ public void configureRootLogger(LoggerContext ctx, Props props, String threadIdFieldPattern, String fileName) {
+ String logFormat = LOG_FORMAT.replace(THREAD_ID_PLACEHOLDER, threadIdFieldPattern);
+ // configure appender
+ LogbackHelper.RollingPolicy rollingPolicy = createRollingPolicy(ctx, props, fileName);
+ FileAppender<ILoggingEvent> fileAppender = rollingPolicy.createAppender("file");
+ fileAppender.setContext(ctx);
+ PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
+ fileEncoder.setContext(ctx);
+ fileEncoder.setPattern(logFormat);
+ fileEncoder.start();
+ fileAppender.setEncoder(fileEncoder);
+ fileAppender.start();
+
+ // configure logger
+ Logger rootLogger = ctx.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
+ rootLogger.addAppender(fileAppender);
+ rootLogger.detachAppender("console");
+ }
+
public ConsoleAppender newConsoleAppender(Context loggerContext, String name, String pattern, Filter... filters) {
PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder();
consoleEncoder.setContext(loggerContext);
diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java b/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
index ec3ac8fc78b..174733eb097 100644
--- a/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
+++ b/server/sonar-search/src/main/java/org/sonar/search/SearchLogging.java
@@ -19,26 +19,20 @@
*/
package org.sonar.search;
-import ch.qos.logback.classic.Level;
-import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.spi.ILoggingEvent;
-import ch.qos.logback.core.ConsoleAppender;
import org.sonar.process.LogbackHelper;
+import org.sonar.process.Props;
public class SearchLogging {
- private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level es[][%logger{20}] %X %msg%n";
-
private LogbackHelper helper = new LogbackHelper();
- public LoggerContext configure() {
+ public LoggerContext configure(Props props) {
LoggerContext ctx = helper.getRootContext();
ctx.reset();
- ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(ctx, "CONSOLE", LOG_FORMAT);
- Logger rootLogger = helper.configureLogger(Logger.ROOT_LOGGER_NAME, Level.INFO);
- rootLogger.addAppender(consoleAppender);
+ helper.configureRootLogger(ctx, props, "", "es");
+
return ctx;
}
}
diff --git a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java
index d8bf14841fc..7163399b86a 100644
--- a/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java
+++ b/server/sonar-search/src/main/java/org/sonar/search/SearchServer.java
@@ -86,7 +86,7 @@ public class SearchServer implements Monitored {
public static void main(String... args) {
ProcessEntryPoint entryPoint = ProcessEntryPoint.createForArguments(args);
- new SearchLogging().configure();
+ new SearchLogging().configure(entryPoint.getProps());
SearchServer searchServer = new SearchServer(entryPoint.getProps());
entryPoint.launch(searchServer);
}
diff --git a/server/sonar-search/src/test/java/org/sonar/search/SearchLoggingTest.java b/server/sonar-search/src/test/java/org/sonar/search/SearchLoggingTest.java
index 87944620241..dde89d32d45 100644
--- a/server/sonar-search/src/test/java/org/sonar/search/SearchLoggingTest.java
+++ b/server/sonar-search/src/test/java/org/sonar/search/SearchLoggingTest.java
@@ -21,17 +21,38 @@ package org.sonar.search;
import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
+import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
+import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.Appender;
-import ch.qos.logback.core.ConsoleAppender;
+import ch.qos.logback.core.FileAppender;
+import java.io.File;
+import java.io.IOException;
+import java.util.Properties;
import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
import org.sonar.process.LogbackHelper;
+import org.sonar.process.ProcessProperties;
+import org.sonar.process.Props;
import static org.assertj.core.api.Assertions.assertThat;
public class SearchLoggingTest {
+ @Rule
+ public TemporaryFolder temp = new TemporaryFolder();
- SearchLogging underTest = new SearchLogging();
+ private File logDir;
+
+ private Props props = new Props(new Properties());
+ private SearchLogging underTest = new SearchLogging();
+
+ @Before
+ public void setUp() throws IOException {
+ logDir = temp.newFolder();
+ props.set(ProcessProperties.PATH_LOGS, logDir.getAbsolutePath());
+ }
@AfterClass
public static void resetLogback() throws Exception {
@@ -39,11 +60,25 @@ public class SearchLoggingTest {
}
@Test
- public void log_to_console() {
- LoggerContext ctx = underTest.configure();
+ public void do_not_log_to_console() {
+ LoggerContext ctx = underTest.configure(props);
Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
Appender appender = root.getAppender("CONSOLE");
- assertThat(appender).isInstanceOf(ConsoleAppender.class);
+ assertThat(appender).isNull();
+ }
+
+ @Test
+ public void log_to_es_file() {
+ LoggerContext ctx = underTest.configure(props);
+
+ Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
+ Appender<ILoggingEvent> appender = root.getAppender("file");
+ assertThat(appender).isInstanceOf(FileAppender.class);
+ FileAppender fileAppender = (FileAppender) appender;
+ assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "es.log").getAbsolutePath());
+ assertThat(fileAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
+ PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
+ assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level [][%logger{20}] %msg%n");
}
}
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 2d9976b0b64..50feea704f6 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
@@ -19,11 +19,7 @@
*/
package org.sonar.server.app;
-import ch.qos.logback.classic.Logger;
import ch.qos.logback.classic.LoggerContext;
-import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
-import ch.qos.logback.classic.spi.ILoggingEvent;
-import ch.qos.logback.core.FileAppender;
import java.util.logging.LogManager;
import org.slf4j.bridge.SLF4JBridgeHandler;
import org.sonar.api.utils.MessageException;
@@ -34,16 +30,13 @@ import org.sonar.server.platform.ServerLogging;
public abstract class ServerProcessLogging {
private static final String LOG_LEVEL_PROPERTY = "sonar.log.level";
- private static final String PROCESS_NAME_PLACEHOLDER = "XXXX";
- private static final String THREAD_ID_PLACEHOLDER = "ZZZZ";
- private static final String LOG_FORMAT = "%d{yyyy.MM.dd HH:mm:ss} %-5level " + PROCESS_NAME_PLACEHOLDER + "[" + THREAD_ID_PLACEHOLDER + "][%logger{20}] %msg%n";
private final String processName;
- private final String threadIdPattern;
+ private final String threadIdFieldPattern;
private final LogbackHelper helper = new LogbackHelper();
- protected ServerProcessLogging(String processName, String threadIdPattern) {
+ protected ServerProcessLogging(String processName, String threadIdFieldPattern) {
this.processName = processName;
- this.threadIdPattern = threadIdPattern;
+ this.threadIdFieldPattern = threadIdFieldPattern;
}
public LoggerContext configure(Props props) {
@@ -51,7 +44,7 @@ public abstract class ServerProcessLogging {
ctx.reset();
helper.enableJulChangePropagation(ctx);
- configureRootLogger(ctx, props);
+ helper.configureRootLogger(ctx, props, threadIdFieldPattern, processName);
configureLevels(props);
// Configure java.util.logging, used by Tomcat, in order to forward to slf4j
@@ -60,27 +53,6 @@ public abstract class ServerProcessLogging {
return ctx;
}
- private void configureRootLogger(LoggerContext ctx, Props props) {
- String logFormat = LOG_FORMAT
- .replace(PROCESS_NAME_PLACEHOLDER, processName)
- .replace(THREAD_ID_PLACEHOLDER, threadIdPattern);
- // configure appender
- LogbackHelper.RollingPolicy rollingPolicy = helper.createRollingPolicy(ctx, props, processName);
- FileAppender<ILoggingEvent> fileAppender = rollingPolicy.createAppender("file");
- fileAppender.setContext(ctx);
- PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
- fileEncoder.setContext(ctx);
- fileEncoder.setPattern(logFormat);
- fileEncoder.start();
- fileAppender.setEncoder(fileEncoder);
- fileAppender.start();
-
- // configure logger
- Logger rootLogger = ctx.getLogger(ch.qos.logback.classic.Logger.ROOT_LOGGER_NAME);
- rootLogger.addAppender(fileAppender);
- rootLogger.detachAppender("console");
- }
-
private void configureLevels(Props props) {
String levelCode = props.value(LOG_LEVEL_PROPERTY, "INFO");
LoggerLevel level;
diff --git a/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
index b2e39b3e5d8..fa94f15e1b1 100644
--- a/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/ce/log/CeProcessLoggingTest.java
@@ -83,7 +83,7 @@ public class CeProcessLoggingTest {
assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "ce.log").getAbsolutePath());
assertThat(fileAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
- assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level ce[%X{ceTaskUuid}][%logger{20}] %msg%n");
+ assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level [%X{ceTaskUuid}][%logger{20}] %msg%n");
}
@Test
diff --git a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
index 170ea8b6c11..9c9e60e6d34 100644
--- a/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
+++ b/server/sonar-server/src/test/java/org/sonar/server/app/WebServerProcessLoggingTest.java
@@ -83,7 +83,7 @@ public class WebServerProcessLoggingTest {
assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "web.log").getAbsolutePath());
assertThat(fileAppender.getEncoder()).isInstanceOf(PatternLayoutEncoder.class);
PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
- assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level web[%X{UID}][%logger{20}] %msg%n");
+ assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level [%X{UID}][%logger{20}] %msg%n");
}
@Test