0

我在一个独立的应用程序中使用 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 属性文件。我需要设置哪些属性才能看到上面提到的控制台?

谢谢!

4

3 回答 3

3

您需要定义一个控制台附加程序:

<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>
于 2012-07-16T08:37:16.867 回答
0

从您的评论看来,您只有一个附加程序,即标准输出,并且正如您要求在控制台上显示的那样,您需要在包定义中提及该附加程序,如下所示。

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
于 2012-07-16T09:17:39.980 回答
0

below i give you an example of a log4j configuration:

<?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>

then in the class that you want logging you need to declare the logger like this :

private final static Logger logger = Logger.getLogger(Giannis.class);

and then you do something like

logger.debug("Hello there");

you shoudl see this in the logs when the program is run

于 2012-07-16T08:40:15.543 回答