Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Progress bar on console using Log4J or LogBack

I have a console application that prints its output using Log4J. This is useful as I can easily implement a switch to optionally also show debug messages, even though they are hidden by default.

This application has some long running actions for which a progress bar would be appropriate.

What is the best way to implement this using either Log4J or LogBack?

like image 565
Axel Fontaine Avatar asked Aug 19 '13 17:08

Axel Fontaine


People also ask

What is Log4j and Logback?

In this article, I want to introduce you to Log4j and its two successors Logback and Log4j2. Apache Log4j is a very old logging framework and was the most popular one for several years. It introduced basic concepts, like hierarchical log levels and loggers, that are still used by modern logging frameworks.

What is Logback classic?

Logback Classic - The logback-classic module extends Logback's core functionality to provide support for logging libraries, such as SLF4J, Log4j. It natively implements the SLF4J API, allowing a developer to easily switch between logback and other logging frameworks such as java.util.logging or log4j 1.x.

What should the configuration of log4j2 look like?

The configuration of Log4j2 follows the same principles as the configuration of the two previous logging frameworks and, therefore, looks pretty similar. Log4j, Logback, and Log4j2 are good logging frameworks that are broadly used. So which one should you use?

What is Simple Logging Facade for Java (SLF4J) in Maven dependency Logback?

Maven Dependency Logback uses the Simple Logging Facade for Java (SLF4J) as its native interface. Before we can start logging messages, we need to add Logback and SLF4J to our pom.xml:


1 Answers

To write a progress bar or any sort of updating text to the console, simply end your output with \r (carriage return) instead of \n (new line) so that the next line overwrites it. This question goes into more detail. The F-ANSI library enables you to easily generate nice-looking console output including overwriting the previous line.

But if you are set on using a logging framework, not only is this not possible, but it's not what you want. As ChrisM suggests, the right way to log progress is to log a new line every so often with the percent done so far. This way, you can see both what your program is doing and how far along it is in context.

What it sounds like you want to do is separate your logging from your program's actual output (this is generally a good idea, even disregarding the progress bar issue). Use your logging framework to log information about the program's execution; things you or someone debugging would be interested in. The actual program's output should be written directly to stdout via System.out.

You can then separately decide what you want to do with your logging. It can also be written to stdout, and your logs and output can live together, but better would be to write to stderr (which makes redirecting it much easier) or to a log file (which makes your program's output cleaner, and persists your logs for reference later).


Also, if Java isn't a prerequisite the pv command provides a powerful, customizable progress bar based on the amount of data piped through it.

like image 178
dimo414 Avatar answered Oct 17 '22 08:10

dimo414