Skip to content

Commit

Permalink
Added logic to rollover and archive log files once they reach 10MB
Browse files Browse the repository at this point in the history
in size
  • Loading branch information
jtnelson committed May 25, 2014
1 parent 6ec16e6 commit 7644993
Showing 1 changed file with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.ConsoleAppender;
import ch.qos.logback.core.FileAppender;
import ch.qos.logback.core.rolling.FixedWindowRollingPolicy;
import ch.qos.logback.core.rolling.RollingFileAppender;
import ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy;

/**
* Contains methods to print log messages in the appropriate files.
Expand Down Expand Up @@ -82,6 +84,7 @@ public static void debug(String message, Object... params) {
DEBUG.debug(message, params);
}

private static String MAX_LOG_FILE_SIZE = "10MB";
private static final String LOG_DIRECTORY = "log";
private static final ch.qos.logback.classic.Logger ERROR = setup(
"org.cinchapi.concourse.server.ErrorLogger", "error.log");
Expand Down Expand Up @@ -118,11 +121,30 @@ private static ch.qos.logback.classic.Logger setup(String name, String file) {
encoder.setContext(context);
encoder.start();

// Configure File Appender
FileAppender<ILoggingEvent> appender = new FileAppender<ILoggingEvent>();
// Create File Appender
RollingFileAppender<ILoggingEvent> appender = new RollingFileAppender<ILoggingEvent>();
appender.setFile(LOG_DIRECTORY + File.separator + file);
appender.setEncoder(encoder);
appender.setContext(context);

// Configure Rolling Policy
FixedWindowRollingPolicy rolling = new FixedWindowRollingPolicy();
rolling.setMaxIndex(1);
rolling.setMaxIndex(5);
rolling.setContext(context);
rolling.setFileNamePattern(LOG_DIRECTORY + File.separator + file
+ ".%i.zip");
rolling.setParent(appender);
rolling.start();

// Configure Triggering Policy
SizeBasedTriggeringPolicy<ILoggingEvent> triggering = new SizeBasedTriggeringPolicy<ILoggingEvent>();
triggering.setMaxFileSize(MAX_LOG_FILE_SIZE);
triggering.start();

// Configure File Appender
appender.setEncoder(encoder);
appender.setRollingPolicy(rolling);
appender.setTriggeringPolicy(triggering);
appender.start();

// Get Logger
Expand Down

0 comments on commit 7644993

Please sign in to comment.