I am trying to write a wrapper method to prefix my log messages with method name and user id. I am using String.format to compose the message structure, but the problem comes with interpretation of varargs by String.format()
private String logMessage(@NonNull String methodName, @NonNull String userId, @NonNull String message, Object... arguments) {
//String temp = String.format(message, arguments);
//String msg = String.format("%s:: User:%s : %s", methodName, userId, temp);<--This works
String msg = String.format("%s:: User:%s : " + message, methodName, userId, arguments);<--This prints address of arguments object
log.info(msg);
}
Calling the above method as
logMessage(methodName, userId, "Some text here: %s",
"test"));
The above call prints
someMethod:: User:1266 : Some text here: [Ljava.lang.Object;@705a8dbc
Why is String.format printing address of the String "test"?