Java Code Examples for ch.qos.logback.core.rolling.FixedWindowRollingPolicy#setContext()

The following examples show how to use ch.qos.logback.core.rolling.FixedWindowRollingPolicy#setContext() . 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: BaleenFileLoggerBuilder.java    From baleen with Apache License 2.0 5 votes vote down vote up
/**
 * Create an appender that will roll over based on size
 *
 * @param context
 * @param encoder
 * @return An appender that matches the set up of the logger builder
 */
private RollingFileAppender<ILoggingEvent> createLogAppender(
    LoggerContext context, Encoder<ILoggingEvent> encoder) {
  RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
  appender.setEncoder(encoder);
  appender.setFile(file);

  FixedWindowRollingPolicy rolling = new FixedWindowRollingPolicy();
  rolling.setParent(appender);
  rolling.setContext(context);

  // Set the maximum number of logs, either to the user specified setting or default to 1
  rolling.setMinIndex(1);
  if (maxNumberLogs.isPresent() && maxNumberLogs.get() >= 0) {
    rolling.setMaxIndex(maxNumberLogs.get());
  } else {
    rolling.setMaxIndex(1);
  }
  rolling.setFileNamePattern(getFileWithPattern("%i"));

  // Configure size based rolling
  SizeBasedTriggeringPolicy<ILoggingEvent> trigger = new SizeBasedTriggeringPolicy<>();
  trigger.setMaxFileSize(getMaxFileSize());
  trigger.setContext(context);

  rolling.start();
  trigger.start();
  appender.setRollingPolicy(rolling);
  appender.setTriggeringPolicy(trigger);

  return appender;
}
 
Example 2
Source File: Log.java    From bisq with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void setup(String fileName) {
    LoggerContext loggerContext = (LoggerContext) LoggerFactory.getILoggerFactory();

    RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<>();
    appender.setContext(loggerContext);
    appender.setFile(fileName + ".log");

    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(loggerContext);
    rollingPolicy.setParent(appender);
    rollingPolicy.setFileNamePattern(fileName + "_%i.log");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(10);
    rollingPolicy.start();

    SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<>();
    triggeringPolicy.setMaxFileSize(FileSize.valueOf("10MB"));
    triggeringPolicy.start();

    PatternLayoutEncoder encoder = new PatternLayoutEncoder();
    encoder.setContext(loggerContext);
    encoder.setPattern("%d{MMM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{15}: %msg %xEx%n");
    encoder.start();

    appender.setEncoder(encoder);
    appender.setRollingPolicy(rollingPolicy);
    appender.setTriggeringPolicy(triggeringPolicy);
    appender.start();

    logbackLogger = loggerContext.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
    logbackLogger.addAppender(appender);
    logbackLogger.setLevel(Level.INFO);

    // log errors in separate file
    // not working as expected still.... damn logback...
   /* FileAppender errorAppender = new FileAppender();
    errorAppender.setEncoder(encoder);
    errorAppender.setName("Error");
    errorAppender.setContext(loggerContext);
    errorAppender.setFile(fileName + "_error.log");
    LevelFilter levelFilter = new LevelFilter();
    levelFilter.setLevel(Level.ERROR);
    levelFilter.setOnMatch(FilterReply.ACCEPT);
    levelFilter.setOnMismatch(FilterReply.DENY);
    levelFilter.start();
    errorAppender.addFilter(levelFilter);
    errorAppender.start();
    logbackLogger.addAppender(errorAppender);*/
}
 
Example 3
Source File: Logger.java    From metrics_publish_java with MIT License 4 votes vote down vote up
/**
 * Initialize Logback
 */
private static void initLogback() {
    // reset logger context
    LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
    context.reset();
    
    // shared console appender
    ConsoleAppender.setContext(context);
    ConsoleAppender.setTarget("System.out");
    
    PatternLayoutEncoder consoleEncoder = new PatternLayoutEncoder();
    consoleEncoder.setContext(context);
    consoleEncoder.setPattern(LogPattern);
    consoleEncoder.start();
    ConsoleAppender.setEncoder(consoleEncoder);
    ConsoleAppender.start();
    
    // rolling file
    String logFile = getFilePath() + File.separatorChar + getFileName();
    FileAppender.setContext(context);
    FileAppender.setFile(logFile);
    
    // log pattern
    PatternLayoutEncoder fileEncoder = new PatternLayoutEncoder();
    fileEncoder.setContext(context);
    fileEncoder.setPattern(LogPattern);
    fileEncoder.start();
    FileAppender.setEncoder(fileEncoder);
    
    // rolling policy
    FixedWindowRollingPolicy rollingPolicy = new FixedWindowRollingPolicy();
    rollingPolicy.setContext(context);
    rollingPolicy.setParent(FileAppender);
    rollingPolicy.setFileNamePattern(logFile + "%i.zip");
    rollingPolicy.setMinIndex(1);
    rollingPolicy.setMaxIndex(1);
    rollingPolicy.start();
    
    // file max size - if fileLimit is 0, set max file size to maximum allowed
    long fileLimit = getFileLimitInKBytes() != 0 ? getFileLimitInKBytes() * 1024 : Long.MAX_VALUE;
    SizeBasedTriggeringPolicy<ILoggingEvent> triggeringPolicy = new SizeBasedTriggeringPolicy<ILoggingEvent>(String.valueOf(fileLimit));
    triggeringPolicy.start();
    
    FileAppender.setRollingPolicy(rollingPolicy);
    FileAppender.setTriggeringPolicy(triggeringPolicy);
    FileAppender.start();
}