我在一个独立的应用程序中使用Spring和Hibernate。我需要在控制台上看到记录器消息,如下所示......
2011-02-10 12:24:17 DriverManagerDataSource [INFO] Loaded JDBC driver: org.h2.Driver
2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1]
2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource
2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa]
2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource
2011-02-10 12:24:17 JdbcTemplate [DEBUG] Executing SQL statement [SELECT 1]
2011-02-10 12:24:17 DataSourceUtils [DEBUG] Fetching JDBC Connection from DataSource
2011-02-10 12:24:17 DriverManagerDataSource [DEBUG] Creating new JDBC DriverManager Connection to [jdbc:h2:/data/h2/testa]
2011-02-10 12:24:17 DataSourceUtils [DEBUG] Returning JDBC Connection to DataSource
我有log4j属性文件。如上所述,我需要设置哪些属性来查看控制台?
谢谢!
答案 0 :(得分:3)
您需要定义一个控制台appender:
<appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-12d{HH:mm:ss,SSS} - %p - %C{1}.%M(%-3L) | %m%n" />
</layout>
</appender>
然后将其添加到根目录:
<root>
<appender-ref ref="CONSOLE" />
</root>
答案 1 :(得分:0)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<!-- Appenders -->
<appender name="console" class="org.apache.log4j.ConsoleAppender">
<param name="Target" value="System.out" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %c (%L) - %m%n" />
</layout>
</appender>
<logger name="com.opensymphony.xwork2">
<level value="error" />
</logger>
<!-- Root Logger -->
<root>
<priority value="info" />
<appender-ref ref="console" />
</root>
</log4j:configuration>
然后在你想要记录的类中,你需要像这样声明记录器:
private final static Logger logger = Logger.getLogger(Giannis.class);
然后你做了类似
的事情logger.debug(“Hello there”);
当程序运行时,你应该在日志中看到这个
答案 2 :(得分:0)
从您的评论看起来您只有一个appender即stdout,并且您要求在控制台上显示,您需要在包定义中提及该appender,如下所示。
log4j.log4j.rootLogger=DEBUG, stdout
log4j.logger.org.hibernate=DEBUG, stdout
log4j.logger.org.springframework=DEBUG, stdout
log4j.logger.org.apache.axis=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.threshold=DEBUG
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
它将打印DEBUG级别的'org.hibernate','org.springframework','org.apache.axis'软件包中的日志。
注意:如果你想从spring / hibernate中捕获特定包的日志,你可以用下面的appender提到那个包
log4j.logger.org.springframework.transaction=DEBUG, stdout