Search in sources :

Example 1 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy 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 2 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project TranslateIt-api by zunpiau.

the class LogbackConfigProd method appender.

@Bean(initMethod = "start", destroyMethod = "stop")
public static RollingFileAppender<ILoggingEvent> appender(LoggerContext ctx, PatternLayoutEncoder encoder, @Value("${log.history}") int history, @Value("${log.fileNamePattern}") String parretn) {
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(ctx);
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setMaxHistory(history);
    rollingPolicy.setParent(appender);
    rollingPolicy.setContext(ctx);
    rollingPolicy.setCleanHistoryOnStart(true);
    rollingPolicy.setFileNamePattern(parretn);
    rollingPolicy.start();
    appender.setRollingPolicy(rollingPolicy);
    appender.setEncoder(encoder);
    return appender;
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Bean(org.springframework.context.annotation.Bean)

Example 3 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project cdap-ingest by caskdata.

the class FileTailerIT method getTimeLogger.

private Logger getTimeLogger(String file) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setName("File Tailer IT");
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(file + "%d{yyyy-MM-dd_HH-mm}");
    rollingPolicy.start();
    fileAppender.setRollingPolicy(rollingPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("%msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    // configures the logger
    Logger logger = loggerContext.getLogger(FileTailerIT.class.getName() + "time");
    logger.setLevel(Level.DEBUG);
    logger.addAppender(fileAppender);
    return logger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 4 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy 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 5 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project midpoint by Evolveum.

the class TestLoggingConfiguration method test110SetMaxHistory.

// MID-5674
@Test
public void test110SetMaxHistory() throws Exception {
    // GIVEN
    Task task = taskManager.createTaskInstance(TestLoggingConfiguration.class.getName() + ".test101EnableBasicAudit");
    OperationResult result = task.getResult();
    // Setup
    PrismObject<SystemConfigurationType> systemConfiguration = PrismTestUtil.parseObject(AbstractInitializedModelIntegrationTest.SYSTEM_CONFIGURATION_FILE);
    LoggingConfigurationType logging = systemConfiguration.asObjectable().getLogging();
    applyTestLoggingConfig(logging);
    ((FileAppenderConfigurationType) logging.getAppender().get(0)).setMaxHistory(100);
    Collection<ObjectDelta<? extends ObjectType>> deltas = cast(prismContext.deltaFor(SystemConfigurationType.class).item(SystemConfigurationType.F_LOGGING).replace(logging.asPrismContainerValue().clone()).asObjectDeltas(AbstractInitializedModelIntegrationTest.SYSTEM_CONFIGURATION_OID));
    // WHEN
    modelService.executeChanges(deltas, null, task, result);
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
    assertNotNull("No logger", logger);
    Iterator<Appender<ILoggingEvent>> appenderIterator = logger.iteratorForAppenders();
    RollingFileAppender<ILoggingEvent> fileAppender = null;
    while (appenderIterator.hasNext()) {
        Appender<ILoggingEvent> appender = appenderIterator.next();
        System.out.println("Appender: " + appender);
        if (appender instanceof RollingFileAppender) {
            fileAppender = (RollingFileAppender<ILoggingEvent>) appender;
            break;
        }
    }
    assertNotNull("No file appender", fileAppender);
    RollingPolicy rollingPolicy = fileAppender.getRollingPolicy();
    System.out.println("Rolling policy = " + rollingPolicy);
    assertTrue("Wrong type of rolling policy", rollingPolicy instanceof TimeBasedRollingPolicy);
    TimeBasedRollingPolicy<?> timeBasedRollingPolicy = (TimeBasedRollingPolicy<?>) rollingPolicy;
    assertEquals("Wrong maxHistory", 100, timeBasedRollingPolicy.getMaxHistory());
}
Also used : Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Task(com.evolveum.midpoint.task.api.Task) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) OperationResult(com.evolveum.midpoint.schema.result.OperationResult) 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) LoggerContext(ch.qos.logback.classic.LoggerContext) ObjectDelta(com.evolveum.midpoint.prism.delta.ObjectDelta) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.testng.annotations.Test)

Aggregations

TimeBasedRollingPolicy (ch.qos.logback.core.rolling.TimeBasedRollingPolicy)19 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)15 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)13 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)11 LoggerContext (ch.qos.logback.classic.LoggerContext)10 Logger (ch.qos.logback.classic.Logger)7 Test (org.junit.Test)6 RollingPolicy (ch.qos.logback.core.rolling.RollingPolicy)4 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)2 TriggeringPolicy (ch.qos.logback.core.rolling.TriggeringPolicy)2 GelfAppender (de.appelgriepsch.logback.GelfAppender)2 File (java.io.File)2 SlingRollingFileAppender (org.apache.sling.commons.log.logback.internal.util.SlingRollingFileAppender)2 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)2 LogcatAppender (ch.qos.logback.classic.android.LogcatAppender)1 Appender (ch.qos.logback.core.Appender)1 FileAppender (ch.qos.logback.core.FileAppender)1 LayoutWrappingEncoder (ch.qos.logback.core.encoder.LayoutWrappingEncoder)1 FixedWindowRollingPolicy (ch.qos.logback.core.rolling.FixedWindowRollingPolicy)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1