Skip to content

Instantly share code, notes, and snippets.

@tomaszalusky
Created May 7, 2015 13:33
Show Gist options
  • Save tomaszalusky/b9109c0faddafe099835 to your computer and use it in GitHub Desktop.
Save tomaszalusky/b9109c0faddafe099835 to your computer and use it in GitHub Desktop.
import java.io.IOException;
import org.apache.logging.log4j.core.appender.AbstractManager;
import org.apache.logging.log4j.core.appender.rolling.DefaultRolloverStrategy;
import org.apache.logging.log4j.core.appender.rolling.RollingFileManager;
import org.apache.logging.log4j.core.appender.rolling.RolloverDescription;
import org.apache.logging.log4j.core.appender.rolling.RolloverDescriptionImpl;
import org.apache.logging.log4j.core.appender.rolling.RolloverStrategy;
import org.apache.logging.log4j.core.appender.rolling.action.AbstractAction;
import org.apache.logging.log4j.core.appender.rolling.action.Action;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginConfiguration;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.lookup.StrSubstitutor;
import internal.Helper; // internal utility class, reassigning final field based on http://stackoverflow.com/a/3301720/653539 trick
/**
* This rollover strategy <em>immediately</em> passes log events into file
* specified by <code>RollingFile</code> appender's<code>filePattern</code> attribute.
* When rollover is triggered, old file just closes and new file is created.
* (This behavior differs from {@link DefaultRolloverStrategy} which keeps the active file unchanged
* and implements rollover as renaming active file into pattern-named file.)
* See also http://stackoverflow.com/questions/30059333/log4j2-rolling-appender-filename-sliding-according-to-pattern .
* @author Tomas Zalusky
*/
@Plugin(name = "SlidingFilenameRolloverStrategy", category = "Core", printObject = true)
public class SlidingFilenameRolloverStrategy implements RolloverStrategy {
private final StrSubstitutor subst;
protected SlidingFilenameRolloverStrategy(StrSubstitutor subst) {
this.subst = subst;
}
/**
* Performs rollover. Unlike {@link DefaultRolloverStrategy}, no renaming is planned (manager update is planned instead).
* @see org.apache.logging.log4j.core.appender.rolling.RolloverStrategy#rollover(org.apache.logging.log4j.core.appender.rolling.RollingFileManager)
*/
@Override
public RolloverDescription rollover(final RollingFileManager manager) throws SecurityException {
final Action shiftToNextActiveFile = new AbstractAction() {
@Override
public boolean execute() throws IOException {
manager.getPatternProcessor().updateTime(); // adjust pattern processor time to new time
StringBuilder newActiveFileNameBuf = new StringBuilder(255);
manager.getPatternProcessor().formatFileName(subst, newActiveFileNameBuf, 0); // generate file name based on current time
String newActiveFileName = newActiveFileNameBuf.toString();
Helper.assignFinalField(manager, AbstractManager.class, "name", newActiveFileName); // reset manager to use new name (manager stays in registry under old key but it does not mind)
return true;
}
};
return new RolloverDescriptionImpl("dummy", false, shiftToNextActiveFile, null); // first 2 args are not utilized by framework
}
@PluginFactory
public static SlidingFilenameRolloverStrategy createStrategy(@PluginConfiguration final Configuration config) {
return new SlidingFilenameRolloverStrategy(config.getStrSubstitutor());
}
}
@Anshu-Garg
Copy link

Hi tomaszalusky,

How are you updating file name in AbstractManager ?

Can you post what is in Helper.assignFinalField(manager, AbstractManager.class, "name", newActiveFileName);

@Anshu-Garg
Copy link

I want to have file name appended with time stamp of the instant when it was created not when it is rolled off. So I want to get rid of renaming and simply want to close current file and open next with similar pattern name but current timestamp? How can I achieve it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment