1

I have code written that should edit my RollingFileAppenders DefaultRolloverStrategy in code -

DefaultRolloverStrategy newStrategy = DefaultRolloverStrategy.createStrategy(Integer.toString(max), "1", "max", 
            "2", null, true, config);
    Appender appender = config.getAppender("RollingFile");
    ((RollingFileAppender) appender).getManager().setRolloverStrategy(newStrategy);

however, since in my xml file, i have this under the RollingFileAppender-

<Delete basePath="logs/">
    <IfFileName glob="logs/${baseFileName}-*.log.gz" />
    <IfLastModified age="2d" />
</Delete>

to delete log files over 2 days old, I need some way to apply that Delete section to the DefaultRollOverStrategy before I add it to the appender...

Looking at the API for the createStrategy, its method call goes like this -

public static DefaultRolloverStrategy createStrategy(@PluginAttribute(value="max")
                                  String max,
                                  @PluginAttribute(value="min")
                                  String min,
                                 @PluginAttribute(value="fileIndex")
                                 String fileIndex,
                                 @PluginAttribute(value="compressionLevel")
                                 String compressionLevelStr,
                                 @PluginElement(value="Actions")
                                 Action[] customActions,
       @PluginAttribute(value="stopCustomActionsOnError",defaultBoolean=true)
                                  boolean stopCustomActionsOnError,
                                  @PluginConfiguration
                                  Configuration config)

I figured that maybe you created a delete action somehow and added it to the Action array customActions?

According to this page-

https://logging.apache.org/log4j/2.x/log4j-core/apidocs/org/apache/logging/log4j/core/appender/rolling/action/DeleteAction.html

there is a delete action but I am not sure if it is the correct one to use or even if its the type of action that is supposed to be put in Action[] CustomActions...

In that Delete Action, You are able to specify Path Conditions, and in path conditions, it has this method -

accept(Path baseDir, Path relativePath, BasicFileAttributes attrs)

which would allow you to specify base directory, the relativePath (maybe this is where I would put the information in IfFileName?), and BasicFileAttributes, under which one attribute is last modified so that seems to fit....

does anyone know if I am on the right track here and this will work or is it even possible at all to do this without building a new configuration or editing the xml file?

Chase
  • 267
  • 1
  • 6
  • 20

0 Answers0