Search in sources :

Example 6 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project coffeenet-starter by coffeenet.

the class CoffeeNetLoggingAutoConfigurationTest method integration.

@Test
public void integration() {
    final Logger logger = (Logger) LoggerFactory.getLogger(ROOT_LOGGER_NAME);
    assertThat(logger.getAppender("CONSOLE"), is(instanceOf(ConsoleAppender.class)));
    Appender<ILoggingEvent> fileAppender = logger.getAppender("COFFEENET-FILE");
    assertThat(fileAppender, is(instanceOf(RollingFileAppender.class)));
    // Check File Appender
    RollingFileAppender rollingFileAppender = (RollingFileAppender) fileAppender;
    assertThat(rollingFileAppender.getFile(), is("logs/app.log"));
    assertThat(rollingFileAppender.getName(), is("COFFEENET-FILE"));
    RollingPolicy rollingPolicy = rollingFileAppender.getRollingPolicy();
    assertThat(rollingPolicy, is(instanceOf(TimeBasedRollingPolicy.class)));
    TimeBasedRollingPolicy timeBasedRollingPolicy = (TimeBasedRollingPolicy) rollingFileAppender.getRollingPolicy();
    assertThat(timeBasedRollingPolicy.getFileNamePattern(), is("logs/app-%d{yyyy-MM-dd}.log"));
    PatternLayoutEncoder encoder = (PatternLayoutEncoder) rollingFileAppender.getEncoder();
    assertThat(encoder.getPattern(), is("%d{yyyy-MM-dd HH:mm:ss.SSS} %5p --- [%t] %-40.40logger{39} : %m%n%wEx"));
    // Check Gelf Appender
    Appender<ILoggingEvent> gelfAppender = logger.getAppender("COFFEENET-GELF");
    assertThat(gelfAppender, is(instanceOf(GelfAppender.class)));
    GelfAppender gelfAppender1 = (GelfAppender) gelfAppender;
    assertThat(gelfAppender1.getName(), is("COFFEENET-GELF"));
    assertThat(gelfAppender1.getServer(), is("localServer"));
    assertThat(gelfAppender1.getProtocol(), is("TCP"));
    assertThat(gelfAppender1.getPort(), is(1337));
    assertThat(((PatternLayout) gelfAppender1.getLayout()).getPattern(), is("%m"));
    assertThat(gelfAppender1.getAdditionalFields(), hasEntry("environment", "test"));
    assertThat(gelfAppender1.getAdditionalFields(), hasEntry("application", "CoffeeNetApplication"));
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) GelfAppender(de.appelgriepsch.logback.GelfAppender) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 7 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project dropwizard by dropwizard.

the class FileAppenderFactory method buildAppender.

protected FileAppender<E> buildAppender(LoggerContext context) {
    if (archive) {
        final RollingFileAppender<E> appender = new RollingFileAppender<>();
        appender.setFile(currentLogFilename);
        appender.setBufferSize(new FileSize(bufferSize.toBytes()));
        if (maxFileSize != null && !archivedLogFilenamePattern.contains("%d")) {
            final FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
            rollingPolicy.setContext(context);
            rollingPolicy.setMaxIndex(getArchivedFileCount());
            rollingPolicy.setFileNamePattern(getArchivedLogFilenamePattern());
            rollingPolicy.setParent(appender);
            rollingPolicy.start();
            appender.setRollingPolicy(rollingPolicy);
            final SizeBasedTriggeringPolicy<E> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
            triggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
            triggeringPolicy.setContext(context);
            triggeringPolicy.start();
            appender.setTriggeringPolicy(triggeringPolicy);
            return appender;
        } else {
            final TimeBasedFileNamingAndTriggeringPolicy<E> triggeringPolicy;
            if (maxFileSize == null) {
                triggeringPolicy = new DefaultTimeBasedFileNamingAndTriggeringPolicy<>();
            } else {
                final SizeAndTimeBasedFNATP<E> maxFileSizeTriggeringPolicy = new SizeAndTimeBasedFNATP<>();
                maxFileSizeTriggeringPolicy.setMaxFileSize(new FileSize(maxFileSize.toBytes()));
                triggeringPolicy = maxFileSizeTriggeringPolicy;
            }
            triggeringPolicy.setContext(context);
            final TimeBasedRollingPolicy<E> rollingPolicy = new TimeBasedRollingPolicy<>();
            rollingPolicy.setContext(context);
            rollingPolicy.setFileNamePattern(archivedLogFilenamePattern);
            rollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(triggeringPolicy);
            triggeringPolicy.setTimeBasedRollingPolicy(rollingPolicy);
            rollingPolicy.setMaxHistory(archivedFileCount);
            appender.setRollingPolicy(rollingPolicy);
            appender.setTriggeringPolicy(triggeringPolicy);
            rollingPolicy.setParent(appender);
            rollingPolicy.start();
            return appender;
        }
    }
    final FileAppender<E> appender = new FileAppender<>();
    appender.setFile(currentLogFilename);
    appender.setBufferSize(new FileSize(bufferSize.toBytes()));
    return appender;
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) 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) SizeAndTimeBasedFNATP(ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) FileSize(ch.qos.logback.core.util.FileSize)

Example 8 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method hasMaxFileSize.

@Test
public void hasMaxFileSize() throws Exception {
    FileAppenderFactory fileAppenderFactory = new FileAppenderFactory();
    fileAppenderFactory.setCurrentLogFilename(folder.newFile("logfile.log").toString());
    fileAppenderFactory.setArchive(true);
    fileAppenderFactory.setMaxFileSize(Size.kilobytes(1));
    fileAppenderFactory.setArchivedLogFilenamePattern(folder.newFile("example-%d-%i.log.gz").toString());
    RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext());
    assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeAndTimeBasedFNATP.class);
    final Field maxFileSizeField = SizeAndTimeBasedFNATP.class.getDeclaredField("maxFileSize");
    maxFileSizeField.setAccessible(true);
    final FileSize maxFileSize = (FileSize) maxFileSizeField.get(appender.getTriggeringPolicy());
    assertThat(maxFileSize.getSize()).isEqualTo(1024L);
}
Also used : Field(java.lang.reflect.Field) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Example 9 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project dropwizard by dropwizard.

the class FileAppenderFactoryTest method hasMaxFileSizeFixedWindow.

@Test
public void hasMaxFileSizeFixedWindow() throws Exception {
    FileAppenderFactory fileAppenderFactory = new FileAppenderFactory();
    fileAppenderFactory.setCurrentLogFilename(folder.newFile("logfile.log").toString());
    fileAppenderFactory.setArchive(true);
    fileAppenderFactory.setMaxFileSize(Size.kilobytes(1));
    fileAppenderFactory.setArchivedLogFilenamePattern(folder.newFile("example-%i.log.gz").toString());
    RollingFileAppender<ILoggingEvent> appender = (RollingFileAppender<ILoggingEvent>) fileAppenderFactory.buildAppender(new LoggerContext());
    assertThat(appender.getRollingPolicy()).isInstanceOf(FixedWindowRollingPolicy.class);
    assertThat(appender.getRollingPolicy().isStarted()).isTrue();
    assertThat(appender.getTriggeringPolicy()).isInstanceOf(SizeBasedTriggeringPolicy.class);
    assertThat(appender.getTriggeringPolicy().isStarted()).isTrue();
    final Field maxFileSizeField = SizeBasedTriggeringPolicy.class.getDeclaredField("maxFileSize");
    maxFileSizeField.setAccessible(true);
    final FileSize maxFileSize = (FileSize) maxFileSizeField.get(appender.getTriggeringPolicy());
    assertThat(maxFileSize.getSize()).isEqualTo(1024L);
}
Also used : Field(java.lang.reflect.Field) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) FileSize(ch.qos.logback.core.util.FileSize) Test(org.junit.Test)

Example 10 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project jmxtrans by jmxtrans.

the class TimeBasedRollingKeyOutWriter method initLogger.

@Override
protected Logger initLogger(String fileStr) throws IOException {
    RollingPolicy rollingPolicy = initRollingPolicy(fileStr, getMaxLogBackupFiles(), getMaxLogFileSize());
    RollingFileAppender appender = buildAppender(buildEncoder(), rollingPolicy);
    rollingPolicy.start();
    appender.start();
    // configure the logger for info and add the appender
    return getAndConfigureLogger(appender);
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) RollingPolicy(ch.qos.logback.core.rolling.RollingPolicy)

Aggregations

RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)19 LoggerContext (ch.qos.logback.classic.LoggerContext)11 TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)10 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)9 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)6 Test (org.junit.Test)6 Logger (ch.qos.logback.classic.Logger)5 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)4 FileAppender (ch.qos.logback.core.FileAppender)3 FileSize (ch.qos.logback.core.util.FileSize)3 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)2 Field (java.lang.reflect.Field)2 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 SizeAndTimeBasedFNATP (ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP)1 GelfAppender (de.appelgriepsch.logback.GelfAppender)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 SimpleDateFormat (java.text.SimpleDateFormat)1