What's Up with Logging in Java? [closed]

2019-01-20 21:16发布

Why one would use one of the following packages instead of the other?

  • Java Logging
  • Commons Logging
  • Log4j
  • SLF4j
  • Logback

8条回答
一夜七次
2楼-- · 2019-01-20 21:57

See also answers to the question What are the best practices to log an error?, especially:

  • There are some potential classloading issues with Commons Logging.

  • Log4J and SLF4J were developed by the same person, learning from issues found in practice with Log4J.

查看更多
The star\"
3楼-- · 2019-01-20 22:06

The Commons Logging overview gives the reason for its existence: logging from library code, when you have no control over the underlying logging framework. Very important for the various Apache projects, which will be linked into outside applications. Perhaps not so important for internal IT projects, where you have complete control.

That said, I write to Commons Logging, as do many of the other developers I know. The reason is to minimize mental baggage: you can change projects or jobs, and not have to learn a new framework (provided the new job/project also uses CL, and/or you can convince them to move to it).

Also, there is some value to creating your own wrappers around whatever framework you use. As described here, I like to use a LogWrapper object to provide custom stringification (important), and minimize the visual clutter of logging statements (less important).

查看更多
登录 后发表回答