java - How can I programmatically configure a log4j 2.5 RollingRandomAccessFileAppender to limit files without renaming? -
i have thousands of processes creating logs using log4j2 , renaming of files on every rollover not acceptable due burden places on filesystem.
currently have following code create appender:
pathcondition[] pathconditions = new pathcondition[1]; pathconditions[0] = ifaccumulatedfilecount.createfilecountcondition(10); deleteaction deleteaction = deleteaction.createdeleteaction(basepath, true, 1, true, null, pathconditions, null, config); action[] actions = new action[1]; actions[0] = deleteaction; string filename = "file"; string filepattern = filename + "_%i.log"; rollingrandomaccessfileappender appender = rollingrandomaccessfileappender.createappender( filename, filepattern, "false", // append "rollingrandomaccessfileappender", // name "true", // immediateflush "8192", // buffersizestr sizebasedtriggeringpolicy.createpolicy(long.valueof(maxfilesizeinmb*1024*1024).tostring()), defaultrolloverstrategy.createstrategy(integer.valueof(integer.max_value).tostring(), "1", "max", null, actions, true, config), getlayout(), null, // filter "true", // ignore exceptions (they logged) "false", // advertise "", // advertiseuri config);
but when run process gets murdered os spamming filesystem trying stat every file between 1 , integer.max_value came quite shock (seems defect, after finding 1 non-existent file there no reason subsequent numbers), strace -f:
... [pid 18155] stat("file_2147412901.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412901.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412900.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412900.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412899.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412899.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412898.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412898.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412897.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412897.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412896.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412896.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412895.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412895.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412894.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412894.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412893.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412893.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412892.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412892.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) [pid 18155] stat("file_2147412891.log", 0x2b74134f96d0) = -1 enoent (no such file or directory) ...
stack trace in jvm while happening:
at java.io.unixfilesystem.getbooleanattributes0(native method) @ java.io.unixfilesystem.getbooleanattributes(unixfilesystem.java:242) @ java.io.file.exists(file.java:819) @ org.apache.logging.log4j.core.appender.rolling.defaultrolloverstrategy.purgeascending(defaultrolloverstrategy.java:305) @ org.apache.logging.log4j.core.appender.rolling.defaultrolloverstrategy.purge(defaultrolloverstrategy.java:279) @ org.apache.logging.log4j.core.appender.rolling.defaultrolloverstrategy.rollover(defaultrolloverstrategy.java:506) @ org.apache.logging.log4j.core.appender.rolling.rollingfilemanager.rollover(rollingfilemanager.java:196) @ org.apache.logging.log4j.core.appender.rolling.rollingfilemanager.rollover(rollingfilemanager.java:124) - locked <0x00000005c0196238> (a org.apache.logging.log4j.core.appender.rolling.rollingrandomaccessfilemanager) @ org.apache.logging.log4j.core.appender.rolling.rollingfilemanager.checkrollover(rollingfilemanager.java:119) - locked <0x00000005c0196238> (a org.apache.logging.log4j.core.appender.rolling.rollingrandomaccessfilemanager) @ org.apache.logging.log4j.core.appender.rollingrandomaccessfileappender.append(rollingrandomaccessfileappender.java:89) @ org.apache.logging.log4j.core.config.appendercontrol.trycallappender(appendercontrol.java:152) @ org.apache.logging.log4j.core.config.appendercontrol.callappender0(appendercontrol.java:125) @ org.apache.logging.log4j.core.config.appendercontrol.callappenderpreventrecursion(appendercontrol.java:116) @ org.apache.logging.log4j.core.config.appendercontrol.callappender(appendercontrol.java:84) @ org.apache.logging.log4j.core.config.loggerconfig.callappenders(loggerconfig.java:390) @ org.apache.logging.log4j.core.config.loggerconfig.processlogevent(loggerconfig.java:378) @ org.apache.logging.log4j.core.config.loggerconfig.log(loggerconfig.java:362) @ org.apache.logging.log4j.core.config.loggerconfig.log(loggerconfig.java:352) @ org.apache.logging.log4j.core.config.awaitcompletionreliabilitystrategy.log(awaitcompletionreliabilitystrategy.java:63) @ org.apache.logging.log4j.core.logger.logmessage(logger.java:147) @ org.apache.logging.log4j.spi.abstractlogger.logmessage(abstractlogger.java:1011) @ org.apache.logging.log4j.spi.abstractlogger.logifenabled(abstractlogger.java:988) @ org.apache.logging.log4j.spi.abstractlogger.log(abstractlogger.java:866)
i trying prevent file renaming somehow use deleteaction keep number of files down while still letting them roll arbitrarily high numbers.
if read documentation on defaultrolloverstrategy see describes behavior. designed keep small, fixed number of files, not arbitrary number.
for trying think adding fileindex value of "unlimited" or none not try purge files @ , instead rely on deleteaction configuration make sense. suggest create jira issue this. if can provide patch better.
update: of version 2.8 log4j no longer tries files between min , max , looks @ files on disk. should eliminate problem having. also, in same release new feature added allow write directly rolled on file no renames need take place @ all.
Comments
Post a Comment