ch.qos.logback.core.rolling.TriggeringPolicy Java Examples

The following examples show how to use ch.qos.logback.core.rolling.TriggeringPolicy. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AppenderUtilsTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateRollingFileAppenderDailyRolling()
{
    final RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);

    assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());

    RollingPolicy rollingPolicy = appender.getRollingPolicy();
    final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
    assertTrue("Unexpected rolling policy", condition2);
    rollingPolicy = ((RollingPolicyDecorator)rollingPolicy).getDecorated();
    final boolean condition1 = rollingPolicy instanceof TimeBasedRollingPolicy;
    assertTrue("Unexpected decorated rolling policy", condition1);
    assertEquals("Unexpected max history",
                        (long) MAX_HISTORY,
                        (long) ((TimeBasedRollingPolicy) rollingPolicy).getMaxHistory());

    assertEquals("Unexpected file name pattern",
                        _testLogFileName + ".%d{yyyy-MM-dd}.%i.gz",
                        ((TimeBasedRollingPolicy) rollingPolicy).getFileNamePattern());

    assertEquals("Unexpected compression mode", CompressionMode.GZ, rollingPolicy.getCompressionMode());

    TriggeringPolicy triggeringPolicy = ((TimeBasedRollingPolicy) rollingPolicy).getTimeBasedFileNamingAndTriggeringPolicy();
    final boolean condition = triggeringPolicy instanceof AppenderUtils.DailyTriggeringPolicy;
    assertTrue("Unexpected triggering policy", condition);
    assertEquals("Unexpected triggering policy",
                        String.valueOf(MAX_FILE_SIZE) + " MB",
                        ((AppenderUtils.DailyTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());
    assertEquals("Unexpected layout", LAYOUT, ((PatternLayoutEncoder) appender.getEncoder()).getPattern());
}
 
Example #2
Source File: AppenderUtilsTest.java    From qpid-broker-j with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateRollingFileAppenderNonDailyRolling()
{
    when(_settings.isRollDaily()).thenReturn(Boolean.FALSE);
    when(_settings.isCompressOldFiles()).thenReturn(Boolean.FALSE);

    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    AppenderUtils.configureRollingFileAppender(_settings, new LoggerContext(), appender);

    assertEquals("Unexpected appender file name", _testLogFileName, appender.getFile());

    RollingPolicy rollingPolicy = appender.getRollingPolicy();
    final boolean condition2 = rollingPolicy instanceof RollingPolicyDecorator;
    assertTrue("Unexpected rolling policy", condition2);
    rollingPolicy = ((RollingPolicyDecorator)rollingPolicy).getDecorated();
    final boolean condition1 = rollingPolicy instanceof AppenderUtils.SimpleRollingPolicy;
    assertTrue("Unexpected decorated rolling policy", condition1);
    assertEquals("Unexpected max history",
                        (long) MAX_HISTORY,
                        (long) ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getMaxIndex());
    assertEquals("Unexpected file name pattern",
                        _testLogFileName + ".%i",
                        ((AppenderUtils.SimpleRollingPolicy) rollingPolicy).getFileNamePattern());
    assertEquals("Unexpected compression mode", CompressionMode.NONE, rollingPolicy.getCompressionMode());

    TriggeringPolicy triggeringPolicy = appender.getTriggeringPolicy();
    assertEquals("Unexpected triggering policy",
                        String.valueOf(MAX_FILE_SIZE) + " MB",
                        ((AppenderUtils.SizeTriggeringPolicy) triggeringPolicy).getMaxFileSize().toString());

    Encoder encoder = appender.getEncoder();
    final boolean condition = encoder instanceof PatternLayoutEncoder;
    assertTrue("Unexpected encoder", condition);
    assertEquals("Unexpected layout pattern", LAYOUT, ((PatternLayoutEncoder)encoder).getPattern());
}
 
Example #3
Source File: NsLog.java    From ns4_frame with Apache License 2.0 4 votes vote down vote up
private static void renameLoggingFile() {
     LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
     context.stop();
     context.reset();
     JoranConfigurator configurator = new JoranConfigurator();
     configurator.setContext(context);
     try {
         URL url = getResource(LogConstants.AUTOCONFIG_FILE, Thread.currentThread().getContextClassLoader());
         if (url == null) {
             throw new RuntimeException("init Log error,can't find logback.xml");
         }
         
         
         System.out.println("初始化File:"+url.getFile());
         
         
         configurator.doConfigure(url.getFile());
     } catch (JoranException e) {
     	
     	//读取文件读不到 读取流
     	try {
	configurator.doConfigure(getResourceAsStream(LogConstants.AUTOCONFIG_FILE, Thread.currentThread().getContextClassLoader()));
} catch (JoranException e1) {
	throw new RuntimeException(e.getMessage(), e1);
}
     	
     }

     String configFile = System.getProperty("configfile");
     if(configFile == null || "".equals(configFile)) {
     	configFile = "";
     	//System.out.println("ns_log.configfile is null");
     }else {
     	configFile = configFile.indexOf(".") != -1 ? configFile.substring(0, configFile.lastIndexOf(".")) : configFile;
     	configFile += "_";
     	//System.out.println("ns_log.configfile is configFile");
     }
     
     for (ch.qos.logback.classic.Logger logger : context.getLoggerList()) {
         Iterator<Appender<ILoggingEvent>> index = logger.iteratorForAppenders();
         while (index.hasNext()) {
             Appender<ILoggingEvent> appender = index.next();
             if (appender instanceof NSLogRollingFileAppender) {
                 NSLogRollingFileAppender fileAppender = ((NSLogRollingFileAppender) appender);
                 fileAppender.stop();
                 String fileName = fileAppender.getFile().replace("configfile_", configFile);
                 fileAppender.setFile(fileName);
                 //System.out.println("ns_log.filenNme:" + fileName);

                 TriggeringPolicy triggeringPolicy = fileAppender.getTriggeringPolicy();
                 if (triggeringPolicy instanceof TimeBasedRollingPolicy) {
                     TimeBasedRollingPolicy timeBasedRollingPolicy = ((TimeBasedRollingPolicy) triggeringPolicy);
                     String fileNamePattern = timeBasedRollingPolicy.getFileNamePattern().replace("configfile_", configFile);
                     timeBasedRollingPolicy.setFileNamePattern(fileNamePattern);
                     //System.out.println("ns_log.fileNamePattern:" + fileNamePattern);
                     timeBasedRollingPolicy.stop();
                     timeBasedRollingPolicy.start();
                 }
                 fileAppender.realStart();
             }
         }
     }
     context.start();
 }
 
Example #4
Source File: AppenderUtils.java    From qpid-broker-j with Apache License 2.0 4 votes vote down vote up
public static void configureRollingFileAppender(FileLoggerSettings fileLoggerSettings,
                                                Context loggerContext,
                                                RollingFileAppender<ILoggingEvent> appender)
{
    String fileName = fileLoggerSettings.getFileName();
    File file = new File(fileName);
    if (file.getParentFile() != null)
    {
        file.getParentFile().mkdirs();
    }
    validateLogFilePermissions(file);
    validateMaxFileSize(fileLoggerSettings.getMaxFileSize());

    appender.setFile(fileName);
    appender.setAppend(true);
    appender.setContext(loggerContext);

    TriggeringPolicy triggeringPolicy;
    RollingPolicyBase rollingPolicy;
    final String maxFileSizeAsString = String.valueOf(fileLoggerSettings.getMaxFileSize()) + "MB";
    if(fileLoggerSettings.isRollDaily())
    {
        DailyTriggeringPolicy dailyTriggeringPolicy = new DailyTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
        dailyTriggeringPolicy.setContext(loggerContext);
        TimeBasedRollingPolicy<ILoggingEvent> timeBasedRollingPolicy = new TimeBasedRollingPolicy<>();
        timeBasedRollingPolicy.setMaxHistory(fileLoggerSettings.getMaxHistory());
        timeBasedRollingPolicy.setTimeBasedFileNamingAndTriggeringPolicy(dailyTriggeringPolicy);
        timeBasedRollingPolicy.setFileNamePattern(fileName + ".%d{yyyy-MM-dd}.%i" + (fileLoggerSettings.isCompressOldFiles()
                ? ".gz"
                : ""));
        rollingPolicy = timeBasedRollingPolicy;
        triggeringPolicy = dailyTriggeringPolicy;
    }
    else
    {
        SizeTriggeringPolicy sizeTriggeringPolicy = new SizeTriggeringPolicy(fileLoggerSettings.isRollOnRestart(), maxFileSizeAsString);
        sizeTriggeringPolicy.setContext(loggerContext);
        SimpleRollingPolicy simpleRollingPolicy = new SimpleRollingPolicy(fileLoggerSettings.getMaxHistory());
        simpleRollingPolicy.setFileNamePattern(fileName + ".%i" + (fileLoggerSettings.isCompressOldFiles() ? ".gz" : ""));
        rollingPolicy = simpleRollingPolicy;
        triggeringPolicy = sizeTriggeringPolicy;
    }

    rollingPolicy.setContext(loggerContext);
    RollingPolicyDecorator decorator = new RollingPolicyDecorator(rollingPolicy, fileLoggerSettings.getRolloverListener(), fileLoggerSettings.getExecutorService());
    decorator.setParent(appender);
    appender.setRollingPolicy(decorator);
    appender.setTriggeringPolicy(triggeringPolicy);
    decorator.start();
    triggeringPolicy.start();

    final PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setPattern(fileLoggerSettings.getLayout());
    encoder.setContext(loggerContext);
    encoder.start();
    appender.setEncoder(encoder);
}