How to continue log on same line in Java? Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to continue log on same line in Java? without wasting too much if your time.

The question is published on by Tutorial Guruji team.

This is similar to this question, which is about a bash file.

We’ve created a log which outputs to the console while we’re running our TestNG tests:

private Log log = LogFactory.getLog(xyz.class);

Later on, during the tests, the log is filled with details to let us know what exactly is being done:"Setting browser...");
this.browser = browser;"Completed.");

Right now it’s printing out as you would expect:

Setting browser...

I’d like to have this print out on the same line:

Setting browser...

and a few milliseconds later:

Setting browser...Completed.

Is this possible with the LogFactory?


Without knowing precisely which logging library you are using, I would guess that the answer is almost certainly not unless you want to explicitly specify a newline character everywhere else.

The java.util.logging (and Log4J) frameworks allow you to plugin formatters to format your statements. You’d need to specify a format which did not end with a newline character. For example, in Log4J. they give an example pattern as

 %-5p [%t]: %m%n

So if you removed everything except the %m (the %n is the newline and the other stuff includes the time and log-level; %t and %p respectively, I think), newlines would not be automatically appended to each statement.

However, this means that everywhere else, your other log statements would have to look like this:"I want this on one linen");
We are here to answer your question about How to continue log on same line in Java? - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji