How to register a non-line item in slf4j

It seems that the slf4j method accepts only a string argument, do I need to convert everything to a string when using its method?

+4
source share
3 answers

The main reason to require String instead of Object for a message type is to avoid ambiguity in method signatures.

Accepts the following signatures:

1) debug(Object) // a message 2) debug(Object, Object) // message followed by a parameter 3) debug(Object, Exception) // message followed by an exception 

then when you write

 debug("hello", new Exception("world")); 

it is unclear whether option 2 or option 3 should be used.

In any case, with the existing SLF4J API, you can always write:

  logger.debug("{}", yourObject); 

If the basic structure of logging is login, then your object will be available to all participants without changes. The other logging framework does not support message options, so SLF4J must format the message before invoking the base structure.

+3
source

It seems that the slf4j method accepts only a string argument, do I need to convert everything to a string when using its method?

If you are talking about 1 argument methods like Logger.debug(String message) , then yes, yes. But there are other methods such as Logger.debug(String format, Object[] args) that do not require this.

I'm not sure why sl4fj's APIs look like this (and, for example, log4j's APIs are not), but this is probably some combination:

  • trying to use basic behavior for logging,
  • an attempt to minimize the impact of performance and
  • "developer prerogative."

(The latter is that if something is developed / implemented by one person who does not respond to any design committee, his opinions on style, etc., have more weight than anyone else.)

+2
source

try

 String.valueOf(yourObject) 

and then make sure the toString () methods are really meaningful

+1
source

Source: https://habr.com/ru/post/1345626/


All Articles