开发者

Logging in Scala: what to use when writing a library

There are questions (here and here) about what library to use for logging in Scala, but I'd like to ask a more specific version of the question.

When you're writing a library - ie some code that will become part of lots of different applictions - you don't have as much freedom to choose what you'd like to use. If you use a different logging solution from the one used in the rest of the application, then the poor application developer has to look in two (or more) places for his debuggin开发者_JS百科g information. So ideally you want to use something compatible with the most used solutions.

Of the many logging solutions out there, a lot of them seem to go via slf4j. Does that mean any solution using slf4j would be best? Or slf4s?


Yes, use SLF4J or a Scala wrapper around it. That way your clients get to choose the actual implementation of the logging. From this perspective it doesn't make any difference if you use a wrapper or the SLF4J API directly.

The only real alternative is common.logging, but it has been effectively superseded.


I would recommend log4j. It is quite popular and very flexible. I use it like this:

java -Dlog4j.configuration=log4j.txt -cp .:app_name.jar com.your.program.Main

with the config file in the local dir as:

log4j.rootLogger=WARN, stdout, R

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

# Pattern to output the caller's file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log

log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1

log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%d{ISO8601} %p %t %c - %m%n

log4j.logger.com.your.program=DEBUG
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜