Log4J print empty line to logfile

瘦欲@ 提交于 2019-11-27 07:52:43

问题


In java you could use System.out.println() to print a blank line but how does it work with log4j in a logfile? There I also want to have one or more blank lines inside the logfile.

I already read the follwing: log4j how to append blank line

But this is not really helpful because with

logger.debug("\n");
logger.debug("");

you do not print a message but other information like time and so on (the layout of the logger) are still stored inside the logfile. But I just want to have a total empty line. Can anyone help me please?


回答1:


Log4j is not designed to write blank lines.

You cannot find this option in the logger, because the logger is independent of his appenders who write to a file or the console or something different.

I think you need to create your custom FileAppender which checks the Logging Message before the writing. If your message.equals("\n"); you append a blank line without the layout by accessing the file by your own and skip the normal logging with the layout. then you can use Logger.debug("\n");

Also it is a bad practice to add blank lines. It is similar to split your Log-Message over several lines. Your want all your Logmessage in one line each, so they are easy to parse for LogViewer-Tools like chainsaw or OtrosLogViewer One exception are Stacktraces.




回答2:


Create your own appender with empty layout and a logger that uses that appender. Then write the blank line using that logger. Make certain to set the additivity attribute for logger to false so that the root logger doesn't write the message using the formatting of its appenders.



来源:https://stackoverflow.com/questions/11843941/log4j-print-empty-line-to-logfile

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!