Search in sources :

Example 1 with FileNamePattern

use of ch.qos.logback.core.rolling.helper.FileNamePattern in project qpid-broker-j by apache.

the class RollingPolicyDecoratorTest method setUp.

@Before
public void setUp() throws Exception {
    _baseFolder = TestFileUtils.createTestDirectory("rollover", true);
    _testFile = createTestFile("test.2015-06-25.0.gz");
    Context mockContext = mock(Context.class);
    _delegate = mock(RollingPolicyBase.class);
    String fileNamePattern = _baseFolder.getAbsolutePath() + "/" + "test.%d{yyyy-MM-dd}.%i.gz";
    when(_delegate.getFileNamePattern()).thenReturn(fileNamePattern);
    when(_delegate.getContext()).thenReturn(mockContext);
    _listener = mock(RollingPolicyDecorator.RolloverListener.class);
    _policy = new RollingPolicyDecorator(_delegate, _listener, createMockExecutorService());
    _rolledFileRegExp = Pattern.compile(new FileNamePattern(fileNamePattern, mockContext).toRegex());
    LOGGER.debug("Rolled file reg exp: {} ", _rolledFileRegExp);
}
Also used : Context(ch.qos.logback.core.Context) FileNamePattern(ch.qos.logback.core.rolling.helper.FileNamePattern) RollingPolicyBase(ch.qos.logback.core.rolling.RollingPolicyBase) Before(org.junit.Before)

Example 2 with FileNamePattern

use of ch.qos.logback.core.rolling.helper.FileNamePattern in project aion by aionnetwork.

the class AionLoggerFactory method constructAppenders.

private static List<Appender<ILoggingEvent>> constructAppenders(boolean shouldLogToFile, String _logDirectory) {
    List<Appender<ILoggingEvent>> appenders = new ArrayList<>();
    String logDirectory = DEFAULT_LOG_DIR;
    if (_logDirectory != null && !_logDirectory.trim().isEmpty())
        logDirectory = _logDirectory;
    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(context);
    encoder.setPattern(DEFAULT_LOG_PATTERN);
    encoder.start();
    ConsoleAppender<ILoggingEvent> consoleSync = new ConsoleAppender<>();
    consoleSync.setContext(context);
    // for logger debugging
    consoleSync.setName("consoleSyncAppender");
    consoleSync.setEncoder(encoder);
    consoleSync.start();
    AsyncAppender consoleAsync = new AsyncAppender();
    consoleAsync.setContext(context);
    // for logger debugging
    consoleAsync.setName("consoleAsyncAppender");
    consoleAsync.addAppender(consoleSync);
    consoleAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    consoleAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    consoleAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    consoleAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    consoleAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    consoleAsync.start();
    appenders.add(consoleAsync);
    if (!shouldLogToFile)
        return appenders;
    RollingFileAppender<ILoggingEvent> fileSync = new RollingFileAppender<>();
    SizeBasedTriggeringPolicy<ILoggingEvent> tp = new SizeBasedTriggeringPolicy<>();
    tp.setContext(context);
    tp.start();
    SizeAndTimeBasedRollingPolicy rp = new SizeAndTimeBasedRollingPolicy();
    rp.setContext(context);
    // roll-over each day
    // notice that we don't use the OS-agnostic File.separator here since logback is converts
    // the FileNamePattern
    // to a unix-style path using ch.qos.logback.core.rolling.helper.FileFilterUtil.slashify
    FileNamePattern fnp = new FileNamePattern(logDirectory + "/%d{yyyy/MM, aux}/aion.%d{yyyy-MM-dd}.%i.log", context);
    rp.setFileNamePattern(fnp.getPattern());
    // max rollover file size = 100MB
    rp.setMaxFileSize(FileSize.valueOf("100mb"));
    rp.setParent(fileSync);
    rp.start();
    // for logger debugging
    fileSync.setName("fileSyncAppender");
    fileSync.setContext(context);
    fileSync.setTriggeringPolicy(tp);
    fileSync.setRollingPolicy(rp);
    fileSync.setFile(logDirectory + File.separator + DEFAULT_LOG_FILE_CURRENT);
    fileSync.setEncoder(encoder);
    fileSync.setAppend(true);
    fileSync.start();
    AsyncAppender fileAsync = new AsyncAppender();
    fileAsync.setContext(context);
    // for logger debugging
    fileAsync.setName("fileAsyncAppender");
    fileAsync.addAppender(fileSync);
    fileAsync.setIncludeCallerData(ASYNC_LOGGER_INCLUDE_CALLER_DATA);
    fileAsync.setNeverBlock(ASYNC_LOGGER_NEVER_BLOCK);
    fileAsync.setMaxFlushTime(ASYNC_LOGGER_MAX_FLUSH_TIME_MS);
    fileAsync.setDiscardingThreshold(ASYNC_LOGGER_DISCARDING_THRESHOLD);
    fileAsync.setQueueSize(ASYNC_LOGGER_QUEUE_SIZE);
    fileAsync.start();
    appenders.add(fileAsync);
    return appenders;
}
Also used : ConsoleAppender(ch.qos.logback.core.ConsoleAppender) Appender(ch.qos.logback.core.Appender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) AsyncAppender(ch.qos.logback.classic.AsyncAppender) PatternLayoutEncoder(ch.qos.logback.classic.encoder.PatternLayoutEncoder) ConsoleAppender(ch.qos.logback.core.ConsoleAppender) RollingFileAppender(ch.qos.logback.core.rolling.RollingFileAppender) SizeAndTimeBasedRollingPolicy(ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy) ArrayList(java.util.ArrayList) FileNamePattern(ch.qos.logback.core.rolling.helper.FileNamePattern) ILoggingEvent(ch.qos.logback.classic.spi.ILoggingEvent) AsyncAppender(ch.qos.logback.classic.AsyncAppender) SizeBasedTriggeringPolicy(ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)

Example 3 with FileNamePattern

use of ch.qos.logback.core.rolling.helper.FileNamePattern in project cdap by caskdata.

the class FixedWindowRollingPolicy method start.

@Override
public void start() {
    if (fileNamePatternStr != null) {
        if (context instanceof AppenderContext) {
            AppenderContext context = (AppenderContext) this.context;
            fileNamePatternStr = fileNamePatternStr.replace("instanceId", Integer.toString(context.getInstanceId()));
        } else if (!Boolean.TRUE.equals(context.getObject(Constants.Logging.PIPELINE_VALIDATION))) {
            throw new IllegalStateException("Expected logger context instance of " + AppenderContext.class.getName() + " but got " + context.getClass().getName());
        }
        fileNamePattern = new FileNamePattern(fileNamePatternStr, this.context);
    } else {
        LOG.error(FNP_NOT_SET);
        throw new IllegalStateException(FNP_NOT_SET + CoreConstants.SEE_FNP_NOT_SET);
    }
    if (maxIndex < minIndex) {
        LOG.warn("MaxIndex {} cannot be smaller than MinIndex {}.", maxIndex, minIndex);
        maxIndex = minIndex;
    }
    if ((maxIndex - minIndex) > MAX_WINDOW_SIZE) {
        LOG.warn("Large window sizes are not allowed.");
        maxIndex = minIndex + MAX_WINDOW_SIZE;
        LOG.warn("MaxIndex reduced to " + maxIndex);
    }
    IntegerTokenConverter itc = fileNamePattern.getIntegerTokenConverter();
    if (itc == null) {
        throw new IllegalStateException("FileNamePattern [" + fileNamePattern.getPattern() + "] does not contain a valid IntegerToken");
    }
    processedIndex = maxIndex;
    super.start();
}
Also used : FileNamePattern(ch.qos.logback.core.rolling.helper.FileNamePattern) IntegerTokenConverter(ch.qos.logback.core.rolling.helper.IntegerTokenConverter) AppenderContext(co.cask.cdap.api.logging.AppenderContext)

Aggregations

FileNamePattern (ch.qos.logback.core.rolling.helper.FileNamePattern)3 AsyncAppender (ch.qos.logback.classic.AsyncAppender)1 PatternLayoutEncoder (ch.qos.logback.classic.encoder.PatternLayoutEncoder)1 ILoggingEvent (ch.qos.logback.classic.spi.ILoggingEvent)1 Appender (ch.qos.logback.core.Appender)1 ConsoleAppender (ch.qos.logback.core.ConsoleAppender)1 Context (ch.qos.logback.core.Context)1 RollingFileAppender (ch.qos.logback.core.rolling.RollingFileAppender)1 RollingPolicyBase (ch.qos.logback.core.rolling.RollingPolicyBase)1 SizeAndTimeBasedRollingPolicy (ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy)1 SizeBasedTriggeringPolicy (ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy)1 IntegerTokenConverter (ch.qos.logback.core.rolling.helper.IntegerTokenConverter)1 AppenderContext (co.cask.cdap.api.logging.AppenderContext)1 ArrayList (java.util.ArrayList)1 Before (org.junit.Before)1