Log4j配置打印到控制台和文件

时间:2012-02-17 16:06:54

标签: java log4j

我的log4jproperties:

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=100KB
log4j.appender.file.maxBackupIndex=5
log4j.appender.file.File=checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

log4j.appender.console=org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n

我想将log4j打印到控制台和文件但是 当我导入slf4j-log12-1.6.4.jar时,它只在文件中打印并抛出此异常:

Could not instantiate class [org.apache.log4j.ConsoleAppender log4j.appender.console.layout=org.apache.log4j.PatternLayout].

当我导入slf4jsimple-1.6.4.jar时,它只打印到控制台,没有异常

那么我应该导入什么?

我应该在属性中更改打印stacktrace。我正在使用这个:

logger.error("ERROR!: " + e); 

但我只看到:

16:59:28,703 ERROR PlayOffPanel:275 - ERROR!: java.lang.IndexOutOfBoundsException: 

Index: 15, Size: 15

1 个答案:

答案 0 :(得分:3)

你应该在开头有这一行:

log4j.rootLogger=DEBUG, console, file