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.