Search in sources :

Example 16 with TimeBasedRollingPolicy

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

the class FileTailerMetricsProcessor method initAppender.

/**
 * Initializes the file appender.
 *
 * @param path the path to the directory for the metrics file
 * @param fileName the name of the metrics file
 * @return the initialized file appender
 */
private RollingFileAppender initAppender(String path, String fileName) {
    LOG.debug("Starting initialize rolling file appender");
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(path + "/" + fileName);
    fileAppender.setAppend(true);
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.setFileNamePattern(path + "/" + fileName + ".%d");
    rollingPolicy.start();
    fileAppender.setRollingPolicy(rollingPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("%msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    return fileAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 17 with TimeBasedRollingPolicy

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

the class CoffeeNetLoggingAutoConfiguration method rollingFileAppender.

private RollingFileAppender<ILoggingEvent> rollingFileAppender(LoggerContext loggerContext) {
    RollingFileAppender<ILoggingEvent> rfAppender = new RollingFileAppender<>();
    rfAppender.setName(COFFEENET_FILE_APPENDER);
    rfAppender.setContext(loggerContext);
    rfAppender.setFile(coffeeNetLoggingFileProperties.getFile());
    // TimeBasedRollingPolicy
    TimeBasedRollingPolicy rollingPolicy = new TimeBasedRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(rfAppender);
    rollingPolicy.setFileNamePattern(coffeeNetLoggingFileProperties.getFileNamePattern());
    rollingPolicy.setMaxHistory(coffeeNetLoggingFileProperties.getMaxHistory());
    rollingPolicy.start();
    rfAppender.setRollingPolicy(rollingPolicy);
    // PatternLayoutEncoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern(coffeeNetLoggingFileProperties.getPattern());
    encoder.start();
    rfAppender.setEncoder(encoder);
    rfAppender.start();
    return rfAppender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 18 with TimeBasedRollingPolicy

use of ch.qos.logback.core.rolling.TimeBasedRollingPolicy in project dubbo by alibaba.

the class LogbackContainer method doInitializer.

/**
 * Initializer logback
 *
 * @param file
 * @param level
 * @param maxHistory
 */
private void doInitializer(String file, String level, int maxHistory) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    Logger rootLogger = loggerContext.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.detachAndStopAllAppenders();
    // appender
    RollingFileAppender<ILoggingEvent> fileAppender = new RollingFileAppender<ILoggingEvent>();
    fileAppender.setContext(loggerContext);
    fileAppender.setName("application");
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    // policy
    TimeBasedRollingPolicy<ILoggingEvent> policy = new TimeBasedRollingPolicy<ILoggingEvent>();
    policy.setContext(loggerContext);
    policy.setMaxHistory(maxHistory);
    policy.setFileNamePattern(file + ".%d{yyyy-MM-dd}");
    policy.setParent(fileAppender);
    policy.start();
    fileAppender.setRollingPolicy(policy);
    // encoder
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%date [%thread] %-5level %logger (%file:%line\\) - %msg%n");
    encoder.start();
    fileAppender.setEncoder(encoder);
    fileAppender.start();
    rootLogger.addAppender(fileAppender);
    rootLogger.setLevel(Level.toLevel(level));
    rootLogger.setAdditive(false);
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) Logger(ch.qos.logback.classic.Logger) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy)

Example 19 with TimeBasedRollingPolicy

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

the class LogbackHelperTest method createRollingPolicy_time.

@Test
public void createRollingPolicy_time() {
    props.set("sonar.log.rollingPolicy", "time:yyyy-MM");
    props.set("sonar.log.maxFiles", "20");
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    RollingFileAppender appender = (RollingFileAppender) policy.createAppender("SONAR_FILE");
    // max 5 monthly files
    TimeBasedRollingPolicy triggeringPolicy = (TimeBasedRollingPolicy) appender.getTriggeringPolicy();
    assertThat(triggeringPolicy.getMaxHistory()).isEqualTo(20);
    assertThat(triggeringPolicy.getFileNamePattern()).endsWith("sonar.%d{yyyy-MM}.log");
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.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)5 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