Search code examples
javalog4jindentation

Indent log4j messages based on their log level


Can log4j be configured to automatically indent messages with an amount proportional to the message log level? I need to get an output like this:

    [2013-09-13 09:38:24,638]  INFO - Processing graph nodes...
    [2013-09-13 09:38:24,640] DEBUG -   Processed node 1...
    [2013-09-13 09:38:24,646] DEBUG -   Processed node 2...
    [2013-09-13 09:38:24,649] DEBUG -   Processed node 3...
    [2013-09-13 09:38:25,948]  INFO - Processed 3 node(s)

Solution

  • I don't think you can do that by configuration. But you should be able to do it by implementing an custom Layout class that understands how to implement your indentation scheme. Then add that to your appender via the configuration file and all of your log messages will be formatted as you want them to be.