在slf4j中使用decimalformat或logback来填充或识别号码,使它们对齐

时间:2014-03-28 13:08:36

标签: java logging slf4j logback

我在SLF4J中有这个日志语句(在它下面有Logback):

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

我得到了这个输出:

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.

但是我想要这个输出,它会增加额外的空间填充/缩进:

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.

这可以用SLF4J吗?

1 个答案:

答案 0 :(得分:3)

似乎没有。你应该对每个必须带有额外空格的参数使用String.format(...)。像这样的东西

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");