Log4J print empty line to logfile

Metalhead89 picture Metalhead89 · Aug 7, 2012 · Viewed 9.1k times · Source

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?

Answer

Simulant picture Simulant · Aug 7, 2012

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.