5

Is there a way to left pad with zeroes in the rolling file appender of log4j?

For instance myfile-00001.txt

I'm using a 3rd party log processor that reads files in lexicographical order so the the files must be left padded...

I added the following in the filePattern of log4j but I get spaces.

${logPath}/myfile-%05i.txt

Remko Popma
  • 35,130
  • 11
  • 92
  • 114
user432024
  • 4,392
  • 8
  • 49
  • 85

2 Answers2

3

Log4j2 does not currently provide this out of the box. You can add a feature request on the log4j2 Jira issue tracker.

Meanwhile, as a workaround you can implement a custom lookup (https://stackoverflow.com/a/27418802/1446916) that generates a zero padded counter and use this in the filePattern of your RollingFile appender configuration.

Community
  • 1
  • 1
Remko Popma
  • 35,130
  • 11
  • 92
  • 114
  • 1
    This would be relatively easy to do. The syntax would most likely be %i{format} where the format could be anything you could pass to String.format for an integer value. – rgoers Jan 31 '16 at 17:01
2

I created a JIRA Ticket for this issue and also prepared a pull request that solves it. I would be glad if this was included in the next release of Log4j2.

radlan
  • 2,393
  • 4
  • 33
  • 53
  • Looks like the pull request was [accepted](https://github.com/apache/logging-log4j2/pull/224). – Archie Aug 17 '23 at 21:25