2

我有与 Hibernate 集成的 Spring MVC 应用程序。

我正在尝试使用 Log4j 记录 jdbc SQL 语句参数,但它不起作用。我的 log4j.properties 配置文件似乎没有做任何更改。这是项目配置:

我正在使用 Maven、Eclipse 和 log4j.properties 文件位于项目源文件夹的顶部(所以我在类路径中理解)。

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>TestProject</groupId>
  <artifactId>TestProject</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <org.hibernate.version>3.6.0.Final</org.hibernate.version>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
    <org.apache.tiles.version>2.2.2</org.apache.tiles.version>
    <slf4j-log4j12.version>1.7.0</slf4j-log4j12.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${org.hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.16.1-GA</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>

log4j.properties

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

log4j.rootLogger=ERROR, stdout

log4j.logger.main.mvc.model.hibernate=DEBUG

log4j.logger.org.hibernate=INFO
log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
4

3 回答 3

3

好的。所以我终于让它工作了!

所有这些问题都是由 JBoss AS 7.1 造成的。

所以这里是如何在 JBoss AS 7.1 上运行的 Spring MVC 应用程序上设置 slf4j+log4j 日志记录:

  1. 有内置的 JBoss 模块必须关闭所以 - 将jboss-deployment-structure.xml添加到您的META-INF/目录
  2. 您必须在类路径中有这些库:log4j.jarslf4j-api.jarslf4j-log4j12.jarjcl-over-sfl4j.jar。Spring 的默认common-logging.jar必须从类路径中排除。
  3. 您必须在类路径中有log4j.propertieslog4j.xml文件。

这些文件让我可以控制日志,我实现了我的目标——记录 Hibernate SQL 语句参数。

jboss-部署-结构.xml

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
    <deployment>
        <exclusions>
            <module name="org.apache.log4j" />
            <module name="org.slf4j" />
            <module name="org.slf4j.impl" />
        </exclusions>
    </deployment>
</jboss-deployment-structure> 

pom.xml

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>bladositto</groupId>
  <artifactId>TestLogging</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <properties>
    <org.hibernate.version>3.6.0.Final</org.hibernate.version>
    <org.springframework.version>3.1.2.RELEASE</org.springframework.version>
    <org.apache.tiles.version>2.2.2</org.apache.tiles.version>
    <slf4j-log4j12.version>1.7.0</slf4j-log4j12.version>
  </properties>
  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework.version}</version>
        <scope>runtime</scope>
        <exclusions>
            <exclusion>
                <artifactId>commons-logging</artifactId>
                <groupId>commons-logging</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${org.hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hsqldb</groupId>
        <artifactId>hsqldb</artifactId>
        <version>2.2.8</version>
    </dependency>
    <dependency>
        <groupId>org.javassist</groupId>
        <artifactId>javassist</artifactId>
        <version>3.16.1-GA</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.tiles</groupId>
        <artifactId>tiles-extras</artifactId>
        <version>${org.apache.tiles.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${org.springframework.version}</version>
    </dependency>
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${slf4j-log4j12.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
  </dependencies>
</project>

log4j.properties

log4j.rootLogger=INFO, A1, R

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

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=example.log
log4j.appender.R.MaxFileSize=100KB
log4j.appender.R.MaxBackupIndex=1
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=%p %t %c - %m%n

log4j.logger.org.hibernate.SQL=DEBUG
log4j.logger.org.hibernate.type=TRACE
于 2012-09-16T19:41:04.517 回答
1

您在 log4j 配置中使用它

org.apache.commons.digester.Digester=INFO
org.hibernate=INFO
org.springframework.orm=DEBUG,stdout

而它应该是

log4j.logger.org.apache.commons.digester.Digester=INFO
log4j.logger.org.hibernate=INFO
log4j.logger.org.springframework.orm=DEBUG,stdout
于 2012-09-16T08:10:30.180 回答
0

hibernate.cfg.xml在您的或您的中尝试以下内容persistence.xml

<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>

加上您的以下内容log4j.properties

log4j.rootLogger=INFO,stdout

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Threshold=INFO
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd/MM/yyyy HH:mm:ss} %-5p %x - %m%n

org.apache.commons.digester.Digester=INFO
org.hibernate=INFO
org.springframework.orm=DEBUG,stdout

如果你把这两个文件放在你的 webapp 的类路径中,我毫不怀疑你会有日志。

使用 Maven,您应该将这些文件放在文件夹src/main/resources中。target如果您执行“mvn clean package”,它们将被打包在您将获得的结果文件夹中。

要声明一个 slf4j 记录器,你应该这样做:

public class User {
    public static final Logger logger = LoggerFactory.getLogger(User.class);

    //fields getters and setters...

    public User() {

    }

    public User(DTO userDTO) {
        this.username = userDTO.username;
        // etc.
        logger.debug("your user was initialized with the DTO, see this.toString()=?", this.toString());
    } 


    //etc. some other methods, if any

    //toString()
    public String toString() {
        //return a String representation of an User object.
    }
}

要限制特定附加程序的日志级别,您应该使用:

log4j.logger.appenderName.packageName.className=LEVEL

请注意,您当前正在使用:

log4j.logger.packageName.className=LEVEL

祝你好运!

于 2012-09-14T16:24:22.130 回答