private void configureRootWithLogbackWritingToFile(LoggerContext ctx) { Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME); String appLogPattern = helper.buildLogPattern(APP_ROOT_LOGGER_CONFIG); FileAppender<ILoggingEvent> fileAppender = helper.newFileAppender(ctx, appSettings.getProps(), APP_ROOT_LOGGER_CONFIG, appLogPattern); rootLogger.addAppender(fileAppender); rootLogger.addAppender(createAppConsoleAppender(ctx, appLogPattern)); }
/** * 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); rootLogger.addAppender(createAppConsoleAppender(ctx, helper.buildLogPattern(APP_ROOT_LOGGER_CONFIG))); // 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); }
/** * Setup one or more specified loggers to be non additive and to print to System.out which will be caught by the Main * Process and written to sonar.log. */ private void configureDirectToConsoleLoggers(LoggerContext context, String... loggerNames) { RootLoggerConfig config = newRootLoggerConfigBuilder() .setProcessId(ProcessId.APP) .setThreadIdFieldPattern("") .build(); String logPattern = helper.buildLogPattern(config); ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(context, "CONSOLE", logPattern); for (String loggerName : loggerNames) { Logger consoleLogger = context.getLogger(loggerName); consoleLogger.setAdditive(false); consoleLogger.addAppender(consoleAppender); } }
@Test public void buildLogPattern_puts_process_key_as_process_id() { String pattern = underTest.buildLogPattern(newRootLoggerConfigBuilder() .setProcessId(ProcessId.ELASTICSEARCH) .build()); assertThat(pattern).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level es[][%logger{20}] %msg%n"); }
@Test public void buildLogPattern_does_not_put_threadIdFieldPattern_from_RootLoggerConfig_is_null() { String pattern = underTest.buildLogPattern( newRootLoggerConfigBuilder() .setProcessId(ProcessId.COMPUTE_ENGINE) .build()); assertThat(pattern).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level ce[][%logger{20}] %msg%n"); }
private void configureRootLogger(Props props) { RootLoggerConfig config = newRootLoggerConfigBuilder() .setProcessId(processId) .setThreadIdFieldPattern(threadIdFieldPattern) .build(); String logPattern = helper.buildLogPattern(config); helper.configureGlobalFileLog(props, config, logPattern); helper.configureForSubprocessGobbler(props, logPattern); }
@Test public void buildLogPattern_does_not_put_threadIdFieldPattern_from_RootLoggerConfig_is_empty() { String pattern = underTest.buildLogPattern( newRootLoggerConfigBuilder() .setProcessId(ProcessId.WEB_SERVER) .setThreadIdFieldPattern("") .build()); assertThat(pattern).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level web[][%logger{20}] %msg%n"); }
@Test public void buildLogPattern_puts_threadIdFieldPattern_from_RootLoggerConfig_non_null() { String threadIdFieldPattern = RandomStringUtils.randomAlphabetic(5); String pattern = underTest.buildLogPattern( newRootLoggerConfigBuilder() .setProcessId(ProcessId.APP) .setThreadIdFieldPattern(threadIdFieldPattern) .build()); assertThat(pattern).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level app[" + threadIdFieldPattern + "][%logger{20}] %msg%n"); }
private void configureRootWithLogbackWritingToFile(LoggerContext ctx) { Logger rootLogger = ctx.getLogger(ROOT_LOGGER_NAME); String appLogPattern = helper.buildLogPattern(APP_ROOT_LOGGER_CONFIG); FileAppender<ILoggingEvent> fileAppender = helper.newFileAppender(ctx, appSettings.getProps(), APP_ROOT_LOGGER_CONFIG, appLogPattern); rootLogger.addAppender(fileAppender); rootLogger.addAppender(createAppConsoleAppender(ctx, appLogPattern)); }
/** * 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); rootLogger.addAppender(createAppConsoleAppender(ctx, helper.buildLogPattern(APP_ROOT_LOGGER_CONFIG))); // 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); }
/** * Setup one or more specified loggers to be non additive and to print to System.out which will be caught by the Main * Process and written to sonar.log. */ private void configureDirectToConsoleLoggers(LoggerContext context, String... loggerNames) { RootLoggerConfig config = newRootLoggerConfigBuilder() .setProcessId(ProcessId.APP) .setThreadIdFieldPattern("") .build(); String logPattern = helper.buildLogPattern(config); ConsoleAppender<ILoggingEvent> consoleAppender = helper.newConsoleAppender(context, "CONSOLE", logPattern); for (String loggerName : loggerNames) { Logger consoleLogger = context.getLogger(loggerName); consoleLogger.setAdditive(false); consoleLogger.addAppender(consoleAppender); } }
private void configureRootLogger(Props props) { RootLoggerConfig config = newRootLoggerConfigBuilder() .setProcessId(processId) .setThreadIdFieldPattern(threadIdFieldPattern) .build(); String logPattern = helper.buildLogPattern(config); helper.configureGlobalFileLog(props, config, logPattern); helper.configureForSubprocessGobbler(props, logPattern); }