Search in sources :

Example 1 with RollingFileAppender

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

the class LogbackHelperTest method createRollingPolicy_defaults.

@Test
public void createRollingPolicy_defaults() {
    LoggerContext ctx = underTest.getRootContext();
    LogbackHelper.RollingPolicy policy = underTest.createRollingPolicy(ctx, props, "sonar");
    FileAppender appender = policy.createAppender("SONAR_FILE");
    assertThat(appender).isInstanceOf(RollingFileAppender.class);
    // max 5 daily files
    RollingFileAppender fileAppender = (RollingFileAppender) appender;
    TimeBasedRollingPolicy triggeringPolicy = (TimeBasedRollingPolicy) fileAppender.getTriggeringPolicy();
    assertThat(triggeringPolicy.getMaxHistory()).isEqualTo(7);
    assertThat(triggeringPolicy.getFileNamePattern()).endsWith("sonar.%d{yyyy-MM-dd}.log");
}
Also used : FileAppender(ch.qos.logback.core.FileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) LoggerContext(ch.qos.logback.classic.LoggerContext) TimeBasedRollingPolicy(ch.qos.logback.core.rolling.TimeBasedRollingPolicy) Test(org.junit.Test)

Example 2 with RollingFileAppender

use of ch.qos.logback.core.rolling.RollingFileAppender in project spring-boot by spring-projects.

the class DefaultLogbackConfiguration method fileAppender.

private Appender<ILoggingEvent> fileAppender(LogbackConfigurator config, String logFile) {
    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    String logPattern = this.patterns.getProperty("file", FILE_LOG_PATTERN);
    encoder.setPattern(OptionHelper.substVars(logPattern, config.getContext()));
    appender.setEncoder(encoder);
    config.start(encoder);
    appender.setFile(logFile);
    setRollingPolicy(appender, config, logFile);
    setMaxFileSize(appender, config);
    config.appender("FILE", appender);
    return appender;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent)

Example 3 with RollingFileAppender

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

the class FileTailerMetricsProcessor method run.

@Override
public void run() {
    RollingFileAppender appender = null;
    ch.qos.logback.classic.Logger logger = initLogger("metricsLogger");
    try {
        createDirs(stateDirPath);
        createFile(stateDirPath + "/" + metricsFileName);
        appender = initAppender(stateDirPath.getAbsolutePath(), metricsFileName);
        writeMetricsHeader(logger, appender);
        while (isRunning()) {
            for (long i = 0; i < metricsSleepInterval; i += SLEEP_INTERVAL) {
                Thread.sleep(SLEEP_INTERVAL);
                if (!isRunning()) {
                    throw new InterruptedException();
                }
            }
            String currentDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            writeMetrics(logger, appender, currentDate);
            resetMetrics();
        }
    } catch (InterruptedException e) {
        LOG.debug("Metric Processor was interrupted");
    } finally {
        if (appender != null) {
            String currentDate = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS").format(new Date());
            writeMetrics(logger, appender, currentDate);
            appender.stop();
        }
    }
    LOG.info("Metrics Processor stopped");
}
Also used : RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SimpleDateFormat(java.text.SimpleDateFormat) Date(java.util.Date)

Example 4 with RollingFileAppender

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

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

the class TailerLogUtils method getSizeLogger.

public static Logger getSizeLogger(String file, String fileSize) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();
    RollingFileAppender fileAppender = new RollingFileAppender();
    fileAppender.setContext(loggerContext);
    fileAppender.setFile(file);
    fileAppender.setAppend(true);
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setFileNamePattern(file + "%i");
    rollingPolicy.setParent(fileAppender);
    rollingPolicy.start();
    rollingPolicy.setMaxIndex(100);
    fileAppender.setRollingPolicy(rollingPolicy);
    SizeBasedTriggeringPolicy triggeringPolicy = new SizeBasedTriggeringPolicy();
    triggeringPolicy.setContext(loggerContext);
    triggeringPolicy.setMaxFileSize(fileSize);
    triggeringPolicy.start();
    fileAppender.setTriggeringPolicy(triggeringPolicy);
    PatternLayoutEncoder layoutEncoder = new PatternLayoutEncoder();
    layoutEncoder.setContext(loggerContext);
    layoutEncoder.setPattern("[%d  %-5p %c{1}] %msg%n");
    layoutEncoder.start();
    fileAppender.setEncoder(layoutEncoder);
    fileAppender.start();
    // configures  logger
    Logger rootLogger = loggerContext.getLogger(BaseTailerTest.class.getName() + "size");
    rootLogger.setLevel(Level.DEBUG);
    rootLogger.addAppender(fileAppender);
    return rootLogger;
}
Also used : PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) FixedWindowRollingPolicy(ch.qos.logback.core.rolling.FixedWindowRollingPolicy) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy) Logger(ch.qos.logback.classic.Logger) LoggerContext(ch.qos.logback.classic.LoggerContext)

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