Search in sources :

Example 41 with FileAppender

use of ch.qos.logback.core.FileAppender in project gocd by gocd.

the class DefaultPluginLoggingServiceTest method assertPluginLogFile.

private void assertPluginLogFile(String pluginId, String expectedPluginLogFileName) {
    SystemEnvironment systemEnvironment = mock(SystemEnvironment.class);
    DefaultPluginLoggingService loggingService = new DefaultPluginLoggingService(systemEnvironment);
    loggingService.debug(pluginId, "some-logger-name", "message");
    ch.qos.logback.classic.Logger logger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger("plugin." + pluginId);
    ArrayList<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    logger.iteratorForAppenders().forEachRemaining(new Consumer<Appender<ILoggingEvent>>() {

        @Override
        public void accept(Appender<ILoggingEvent> iLoggingEventAppender) {
            appenders.add(iLoggingEventAppender);
        }
    });
    String loggingDirectory = loggingService.getCurrentLogDirectory();
    assertThat(appenders.size()).isEqualTo(1);
    assertThat(new File(((FileAppender) appenders.get(0)).rawFileProperty())).isEqualTo(new File(loggingDirectory, expectedPluginLogFileName));
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) ArrayList(java.util.ArrayList) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) SystemEnvironment(com.thoughtworks.go.util.SystemEnvironment) File(java.io.File)

Example 42 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class WebServerProcessLoggingTest method log_for_cluster_changes_layout_in_file_and_console.

@Test
public void log_for_cluster_changes_layout_in_file_and_console() {
    props.set("sonar.cluster.enabled", "true");
    props.set("sonar.cluster.node.name", "my-node");
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    FileAppender fileAppender = (FileAppender) root.getAppender("file_web");
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) fileAppender.getEncoder();
    assertThat(encoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level my-node web[%X{HTTP_REQUEST_ID}][%logger{20}] %msg%n");
    Logger startup = ctx.getLogger("startup");
    ConsoleAppender<ILoggingEvent> consoleAppender = (ConsoleAppender<ILoggingEvent>) startup.getAppender("CONSOLE");
    PatternLayoutEncoder patternEncoder = (PatternLayoutEncoder) consoleAppender.getEncoder();
    assertThat(patternEncoder.getPattern()).isEqualTo("%d{yyyy.MM.dd HH:mm:ss} %-5level my-node app[][%logger{20}] %msg%n");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(org.sonar.process.logging.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) Test(org.junit.Test)

Example 43 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class LogbackHelperTest method createRollingPolicy_size.

@Test
public void createRollingPolicy_size() throws Exception {
    props.set("sonar.log.rollingPolicy", "size:1MB");
    props.set("sonar.log.maxFiles", "20");
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    Appender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    // max 20 files of 1Mb
    RollingFileAppender fileAppender = (RollingFileAppender) appender;
    FixedWindowRollingPolicy rollingPolicy = (FixedWindowRollingPolicy) fileAppender.getRollingPolicy();
    assertThat(rollingPolicy.getMaxIndex()).isEqualTo(20);
    assertThat(rollingPolicy.getFileNamePattern()).endsWith("sonar.%i.log");
    SizeBasedTriggeringPolicy triggeringPolicy = (SizeBasedTriggeringPolicy) fileAppender.getTriggeringPolicy();
    FileSize maxFileSize = (FileSize) FieldUtils.readField(triggeringPolicy, "maxFileSize", true);
    assertThat(maxFileSize.getSize()).isEqualTo(1024L * 1024);
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) FileAppender(ch.qos.logback.core.FileAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Example 44 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class CeProcessLoggingTest method log_to_ce_file.

@Test
public void log_to_ce_file() {
    LoggerContext ctx = underTest.configure(props);
    Logger root = ctx.getLogger(Logger.ROOT_LOGGER_NAME);
    Appender<ILoggingEvent> appender = root.getAppender("file_ce");
    assertThat(appender).isInstanceOf(FileAppender.class);
    FileAppender fileAppender = (FileAppender) appender;
    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");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) PatternLayoutEncoder(org.sonar.process.logging.PatternLayoutEncoder) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) File(java.io.File) Test(org.junit.Test)

Example 45 with FileAppender

use of ch.qos.logback.core.FileAppender in project sonarqube by SonarSource.

the class AppLoggingTest method verifySonarLogFileAppender.

private void verifySonarLogFileAppender(Appender<ILoggingEvent> appender) {
    assertThat(appender).isInstanceOf(FileAppender.class);
    FileAppender fileAppender = (FileAppender) appender;
    assertThat(fileAppender.getFile()).isEqualTo(new File(logDir, "sonar.log").getAbsolutePath());
    verifyAppFormattedLogEncoder(fileAppender.getEncoder());
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) File(java.io.File)

Aggregations

FileAppender (ch.qos.logback.core.FileAppender)46 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)33 LoggerContext (ch.qos.logback.classic.LoggerContext)27 File (java.io.File)23 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)18 Logger (ch.qos.logback.classic.Logger)17 Appender (ch.qos.logback.core.Appender)14 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)12 Test (org.junit.Test)9 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)6 Path (java.nio.file.Path)6 OutputStreamAppender (ch.qos.logback.core.OutputStreamAppender)5 FileSize (ch.qos.logback.core.util.FileSize)5 Test (org.junit.jupiter.api.Test)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)4 IOException (java.io.IOException)4 Field (java.lang.reflect.Field)4 SyslogAppender (ch.qos.logback.classic.net.SyslogAppender)3 SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)3