简体   繁体   English

使用 slf4j 打印数组只打印第一个元素

[英]Printing an array with slf4j only prints the first element

I have the following code:我有以下代码:

private static final Logger logger = LoggerFactory.getLogger(Some.class);
...
String[] splits=someString.split("..");
logger.info("The string was split into <{}>",splits); // prints first element

What is the right way to print the full content of an array with slf4j?使用 slf4j 打印数组的全部内容的正确方法是什么?

The issue is that with the following code问题是使用以下代码

logger.info("The string was split into <{}>", splits);

you are invoking the method info(String format, Object... arguments) .您正在调用方法info(String format, Object... arguments) Note that the last argument is a varargs.请注意,最后一个参数是可变参数。 Therefore, the array you pass is interpreted as each argument of the variable argument.因此,您传递的数组被解释为变量参数的每个参数。

However, in this case, you want to pass an array as first argument.但是,在这种情况下,您希望将数组作为第一个参数传递。 A simple workaround is to cast it to Object .一个简单的解决方法是将其转换为Object

String[] splits = { "foo", "bar" };
logger.info("The string was split into {}", (Object) splits);

will log The string was split into [foo, bar] , as expected.正如预期的那样The string was split into [foo, bar]将 log The string was split into [foo, bar]

I am facing a strange issue where the entire list of arguments is logged for the first place holder eg: log.info("Initializing Service. arg1={}, arg2={}, pid={}", arg1, arg2, arg3);我面临一个奇怪的问题,其中为第一个占位符记录了整个参数列表,例如: log.info("Initializing Service. arg1={}, arg2={}, pid={}", arg1, arg2, arg3 );

logs the following: Initializing Service.记录以下内容:正在初始化服务。 build=[123456, main, 77378], branch=null, pid={}构建=[123456,主要,77378],分支=空,pid={}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM