Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use decimalformat in slf4j or logback to pad or ident numbers so they align right

I have this log statement in SLF4J (with Logback below it):

        logger.info("{} has {} depots, {00} vehicles and {} customers with a search space of {}.", ...);

And I get this output:

A-n62-k8 has 1 depots, 8 vehicles and 61 customers with a search space of 10^108.
A-n63-k10 has 1 depots, 10 vehicles and 62 customers with a search space of 10^111.

But I want this output, which add extra space padding/indentation:

A-n62-k8  has 1 depots,  8 vehicles and 61 customers with a search space of 10^108.
A-n63-k10 has 1 depots, 10 vehicles and 62 customers with a search space of 10^111.

Is this possible with SLF4J?

like image 901
Geoffrey De Smet Avatar asked Mar 28 '14 13:03

Geoffrey De Smet


People also ask

Does SLF4J use Logback?

Native implementation of slf4j is logback, thus using both as logger framework implies zero memory and computational overhead.

What is the use of SLF4J in Java?

The SLF4J or the Simple Logging Facade for Java is an abstraction layer for various Java logging frameworks, like Log4j 2 or Logback. This allows for plugging different logging frameworks at deployment time without the need for code changes.

How does SLF4J binding work?

Bindings are basically implementations of a particular SLF4J class meant to be extended to plug in a specific logging framework. By design, SLF4J will only bind with one logging framework at a time. Consequently, if more than one binding is present on the classpath, it will emit a warning.

Which Logger is used by SLF4J?

If SLF4J cannot find a binding on the class path it will emit a single warning message and default to no-operation implementation. SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP.


1 Answers

It seems no. You should use String.format(...) for each argument which has to be with extra spaces. Something like this

logger.info("{} has {} depots, {} vehicles and {} customers with a search space of {}.", String.format("%-9s", "A-n62-k8"), "1", String.format("%-2s", "8"), "61", "10^108");
like image 132
dronidze Avatar answered Sep 23 '22 09:09

dronidze