简体   繁体   中英

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)

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.

Not sure if log4j provides any such setting. But you can always write a wrapper method which takes the string message and the Log level as input and within it use a switch statement to do different intendation for different levels.

Below link is old but it is working. It extends Layout class but may be helpful to extend PatternLayout class on similar lines.

https://github.com/zepheira/tracer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM