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();
}
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;
} 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);
}
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.
@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);
}