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?
We cannot print array elements directly in Java, you need to use Arrays. toString() or Arrays. deepToString() to print array elements. Use toString() method if you want to print a one-dimensional array and use deepToString() method if you want to print a two-dimensional or 3-dimensional array etc.
Print Array in One Line with Java StreamsArrays. toString() and Arrays. toDeepString() just print the contents in a fixed manner and were added as convenience methods that remove the need for you to create a manual for loop.
This is the main purpose of SLF4J (Simple Logging Facade for Java) – a logging abstraction which helps to decouple your application from the underlying logger by allowing it to be plugged in – at runtime. Of course, the flexibility that such an abstraction provides is the main reason to use SLF4J.
SLF4J supports popular logging frameworks, namely log4j, java. util. logging, Simple logging and NOP. The logback project supports SLF4J natively.
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)
. 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
.
String[] splits = { "foo", "bar" }; logger.info("The string was split into {}", (Object) splits);
will log The string was split into [foo, bar]
, as expected.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With