Join 34,000+ subscribers and receive articles from our blog about software quality, testing, QA and security.
 

Java logMessage and dynamic number of params


#1

Hello,

Is it possible to define String argument in Session.logMessage( String format, Object [] arg1), so that it would display all passed elements in arg1?

I’d like to use logMessage in spring Interceptor to log method arguments, so number of elements will differ for different methods.

best regards
Piotr Rezmer


#2

I think this is not possible. The format overloads of logMessage et. al. use the MessageFormat.format API to format the given format string and array. I haven’t found anything in the docs that would indicate such a behavior. However, you could create a custom Session method for this scenario. Such a method (let’s call it logArguments) could take a string (the title) and an array of Object. It could then concat the array and the title to a single string with the help of a StringBuffer/StringBuilder and the Objects’ toString methods and then log this string with a standard logMessage call.