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
  • 32,719
  • 11
  • 84
  • 109
user432024
  • 3,928
  • 7
  • 42
  • 72

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
  • 32,719
  • 11
  • 84
  • 109
  • 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,145
  • 4
  • 30
  • 47