Log4j2和Spring 4 [英] Log4j2 and Spring 4

查看:148
本文介绍了Log4j2和Spring 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法让Log4j2编写Spring和Spring Security日志消息(后者我绝对需要

I'm not able to make Log4j2 write Spring and Spring Security log messages (the latter I absolutely need to solve an issue) on log file. I've read a lot here and elsewhere (like in the Spring reference, of course) but I cannot make the solutions I found work for my case.

重要的是要注意,在以下两种解决方案中,矿井日志消息和休眠消息均已正确记录在文件中.该问题仅影响Spring消息.

It's important to note that on both the solutions below, mine logging messages and hibernate ones are written correctly on file. The problem affects only Spring messages.

据我了解,Spring使用jcl(通用日志记录),并且有两种方法可以让Spring使用Log4j2:

As far as I understand, Spring uses jcl (commons-logging) and there are two ways to let Spring uses Log4j2:

  • 只需添加网桥 log4j-jcl (jcl-> log4j2)
  • 将SLF4J与 jcl-over-jcl log4j-slf4j-impl (jcl-> slf4j-> log4j2)一起使用
  • simply adding the bridge log4j-jcl (jcl -> log4j2)
  • using SLF4J with jcl-over-jcl and log4j-slf4j-impl (jcl -> slf4j -> log4j2)

我试图从其他依赖项中排除公共日志,以便手动控制其包含.在第一个解决方案中,我让它包括在内;在第二个解决方案中,我排除了它,但是我对此主题已有不同的见解.无论如何,在两种情况下,Spring都不记录.

I tried to exclude commons-logging from every other dependecy, in order to control manually its inclusion. In the first solution I let it included, in the second I exclude it, but I've seen diverse opinions on the topic. Anyway, in both cases Spring doesn't log.

我可以看到休眠状态使用了 jboss-logging .这是为什么它在两种配置下都起作用的原因吗?对于Spring日志记录来说,包含它是否会成为问题?

I can see that hibernate uses jboss-logging. Is this the reason why it works on both configuration? Can its inclusion be a problem for Spring logging?

应用服务器在这方面是否有问题?也许默认情况下加载了一些库,这些库会覆盖使应用程序库弄得一团糟?

Can the application server be a problem on this? Maybe some library loaded by default that overrides application libs making a mess?

这是我的环境:

  • Java 1.6
  • Servlet 2.5
  • Oracle weblogic 12.1
  • Spring MVC 4.3.4.RELEASE
  • Spring Security 4.2.0.RELEASE
  • Log4j 2.3(最新版本与Java 6兼容)
  • 休眠实体管理器4.2.21.final
  • SLF4J 1.7.22

我想这是一个依赖问题:一些不应该存在或缺少的库.

I guess there is a dependency problem: some library that shouldn't be there or that is missing.

从我的依赖关系树中,我没有看到spring-corecommons-logging的依赖关系,如参考资料所述.我可以在slf4jjcl-over-slf4j上看到spring-data-jpa之一.正如rgoers建议的那样,jcl-over-sl4j的存在应该是问题所在.我将更新结果...

From my dependency tree I don't see a dependency of spring-core on commons-logging, like reference says. I can see one of spring-data-jpa on slf4j and on jcl-over-slf4j. The presence of jcl-over-sl4j shluld be the problem, as rgoers suggested. I'll update results...

所以这是我的 pom.xml 依赖项部分(如您所见,有两种不同的日志记录依赖项配置,其中一个已注释;此配置都使我的日志和休眠日志正常工作,但是两者春季写的两本):

So this is my pom.xml dependencies section (as you can see there are two different logging dependencies configuration, one commented; both this configuration make mine and hibernate logs working good, but neither of the two makes Spring ones written):

<properties>
    <spring.version>4.3.4.RELEASE</spring.version>
    <spring.security.version>4.2.0.RELEASE</spring.security.version>
    <!--<spring.data.jpa.version>1.10.5.RELEASE</spring.data.jpa.version>-->
    <spring.data.jpa.version>1.7.2.RELEASE</spring.data.jpa.version>
    <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
    <log4j.version>2.3</log4j.version>
    <hibernate.validator.version>5.3.2.Final</hibernate.validator.version>
    <hibernate.entitymanager.version>4.2.21.Final</hibernate.entitymanager.version> <!-- LAST VERSION SUPPORTING JPA 2.0 (JPA 2.1 is not supported by WebLogic 12.1.1) -->
    <javax.transaction.version>1.1</javax.transaction.version>
    <oracle.jdbc.version>12.1.0.2</oracle.jdbc.version>
    <querydsl.jpa.version>3.6.9</querydsl.jpa.version>
    <google.guava.version>18.0</google.guava.version>
    <quartz.version>2.2.1</quartz.version>
    <pdfbox.version>2.0.4</pdfbox.version>

</properties>

<dependencies>

    <!-- SPRING SECURITY -->
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-web</artifactId>
        <version>${spring.security.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-config</artifactId>
        <version>${spring.security.version}</version>
    </dependency>

    <!-- WEB AND VALIDATION -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>${hibernate.validator.version}</version>
    </dependency>

    <!-- THYMELEAF -->
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf</artifactId>
        <version>${thymeleaf.version}</version>
    </dependency>
    <dependency>
        <groupId>org.thymeleaf</groupId>
        <artifactId>thymeleaf-spring4</artifactId>
        <version>${thymeleaf.version}</version>
    </dependency>
    <dependency>
        <groupId>com.github.mxab.thymeleaf.extras</groupId>
        <artifactId>thymeleaf-extras-data-attribute</artifactId>
        <version>2.0.1</version>
    </dependency>

    <!-- LOGGING -->

    <!-- Using JCL -->
    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <!-- Using SLF4J -->
    <!--
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>1.7.22</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.22</version>
    </dependency>
    &lt;!&ndash;<dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
    </dependency>&ndash;&gt;

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-slf4j-impl</artifactId>
        <version>${log4j.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    &lt;!&ndash;<dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-jcl</artifactId>
        <version>${log4j.version}</version>
    </dependency>&ndash;&gt;
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-web</artifactId>
        <version>${log4j.version}</version>
        <scope>runtime</scope>
    </dependency>
    -->

    <!-- PERSISTENCE LIBRARIES -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.entitymanager.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.transaction</groupId>
        <artifactId>jta</artifactId>
        <version>${javax.transaction.version}</version>
    </dependency>

    <!-- SPRING FRAMEWORK COMPONENTS -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring.data.jpa.version}</version>
    </dependency>

    <!-- ORACLE JDBC -->
    <dependency>
        <groupId>com.oracle.jdbc</groupId>
        <artifactId>ojdbc7</artifactId>
        <version>${oracle.jdbc.version}</version>
    </dependency>

    <dependency>
        <groupId>commons-configuration</groupId>
        <artifactId>commons-configuration</artifactId>
        <version>1.10</version>
        <!--<exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>

    <!-- SPRING-DATA JPA DATATABLES -->
    <dependency>
        <groupId>com.github.darrachequesne</groupId>
        <artifactId>spring-data-jpa-datatables</artifactId>
        <version>3.0</version>
    </dependency>

    <!-- QUERYDSL -->
    <dependency>
        <groupId>com.mysema.querydsl</groupId>
        <artifactId>querydsl-jpa</artifactId>
        <version>${querydsl.jpa.version}</version>
    </dependency>

    <!-- GOOGLE GUAVA UTILS -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${google.guava.version}</version>
    </dependency>

    <!-- APACHE PDF BOX -->
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>${pdfbox.version}</version>
        <!--<exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>fontbox</artifactId>
        <version>${pdfbox.version}</version>
        <!--<exclusions>
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>
        </exclusions>-->
    </dependency>
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox-tools</artifactId>
        <version>${pdfbox.version}</version>
    </dependency>

    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-io</artifactId>
        <version>1.3.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-csv</artifactId>
        <version>1.1</version>
    </dependency>

    <!-- ESAPI security features (prevent sql injection during password change -->
    <dependency>
        <groupId>org.owasp.esapi</groupId>
        <artifactId>esapi</artifactId>
        <version>2.1.0.1</version>
        <exclusions>
            <!--<exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
            </exclusion>-->
            <exclusion>
                <groupId>log4j</groupId>
                <artifactId>log4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz</artifactId>
        <version>${quartz.version}</version>
    </dependency>
    <dependency>
        <groupId>org.quartz-scheduler</groupId>
        <artifactId>quartz-jobs</artifactId>
        <version>${quartz.version}</version>
    </dependency>


</dependencies>

这是我的 log4j2配置.但是我真的不认为问题出在这里.

And this is my log4j2 configuration. But I really don't think that the problem is here.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn">

<Properties>
    <Property name="basePath">..\\logs\\myapp\\</Property>
</Properties>

<Appenders>
    <RollingFile name="fileLogger" fileName="${basePath}myapp.log" filePattern="${basePath}myapp-%d{yyyy-MM-dd}.log">
        <PatternLayout>
            <pattern>[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{10} - %msg%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true" />
        </Policies>
    </RollingFile>

    <Console name="console" target="SYSTEM_OUT">
        <PatternLayout   pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} %c{10} - %msg%n" />
    </Console>
</Appenders>

<Loggers>
    <Logger name="com.myapp" level="debug" additivity="true">
        <appender-ref ref="fileLogger" level="debug" />
    </Logger>
    <Logger name="org.springframework" level="info" additivity="true">
        <appender-ref ref="fileLogger" level="info" />
    </Logger>
    <!--<Logger name="org.springframework.security" level="debug" additivity="true">
        <appender-ref ref="fileLogger" level="debug" />
    </Logger>-->
    <Logger name="org.thymeleaf" level="info" additivity="true">
        <appender-ref ref="fileLogger" level="info" />
    </Logger>

    <Logger name="org.hibernate" level="debug" additivity="true">
        <appender-ref ref="fileLogger" level="debug" />
    </Logger>

    <Root level="info" additivity="false">
        <appender-ref ref="console" />
    </Root>
</Loggers>
</Configuration>

以下是关于我的战争的图书馆,也许正在出事.

And the following are the libraries on my war, maybe something is in the way.

antisamy-1.5.3.jar
antlr-2.7.7.jar
aopalliance-1.0.jar
aspectjrt-1.8.4.jar
attoparser-2.0.1.RELEASE.jar
batik-css-1.8.jar
batik-ext-1.8.jar
batik-util-1.8.jar
bridge-method-annotation-1.13.jar
bsh-core-2.0b4.jar
c3p0-0.9.1.1.jar
classmate-1.3.1.jar
codegen-0.6.8.jar
commons-beanutils-core-1.8.3.jar
commons-codec-1.2.jar
commons-collections-3.2.2.jar
commons-configuration-1.10.jar
commons-csv-1.1.jar
commons-fileupload-1.3.1.jar
commons-httpclient-3.1.jar
commons-io-1.3.2.jar
commons-lang-2.6.jar
commons-logging-1.2.jar
dom4j-1.6.1.jar
ecj-4.3.1.jar
esapi-2.1.0.1.jar
fontbox-2.0.4.jar
guava-18.0.jar
hibernate-commons-annotations-4.0.2.Final.jar
hibernate-core-4.2.21.Final.jar
hibernate-entitymanager-4.2.21.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
hibernate-validator-5.3.2.Final.jar
jackson-annotations-2.6.0.jar
jackson-core-2.6.6.jar
jackson-databind-2.6.6.jar
javassist-3.18.1-GA.jar
javax.el-api-2.2.5.jar
javax.inject-1.jar
jboss-logging-3.3.0.Final.jar
jboss-transaction-api_1.1_spec-1.0.1.Final.jar
jcl-over-slf4j-1.7.10.jar
jsr305-1.3.9.jar
jta-1.1.jar
log4j-api-2.3.jar
log4j-core-2.3.jar
log4j-jcl-2.3.jar
log4j-web-2.3.jar
mysema-commons-lang-0.2.4.jar
nekohtml-1.9.16.jar
ognl-3.1.10.jar
ojdbc7-12.1.0.2.jar
pdfbox-2.0.4.jar
pdfbox-debugger-2.0.4.jar
pdfbox-tools-2.0.4.jar
quartz-2.2.1.jar
quartz-jobs-2.2.1.jar
querydsl-apt-3.6.9.jar
querydsl-codegen-3.6.9.jar
querydsl-core-3.6.9.jar
querydsl-jpa-3.6.9.jar
slf4j-api-1.6.6.jar
spring-aop-4.3.4.RELEASE.jar
spring-beans-4.3.4.RELEASE.jar
spring-context-4.3.4.RELEASE.jar
spring-core-4.3.4.RELEASE.jar
spring-data-commons-1.9.2.RELEASE.jar
spring-data-jpa-1.7.2.RELEASE.jar
spring-data-jpa-datatables-3.0.jar
spring-expression-4.3.4.RELEASE.jar
spring-jdbc-4.3.4.RELEASE.jar
spring-orm-4.3.4.RELEASE.jar
spring-security-config-4.2.0.RELEASE.jar
spring-security-core-4.2.0.RELEASE.jar
spring-security-web-4.2.0.RELEASE.jar
spring-tx-4.3.4.RELEASE.jar
spring-web-4.3.4.RELEASE.jar
spring-webmvc-4.3.4.RELEASE.jar
thymeleaf-3.0.2.RELEASE.jar
thymeleaf-extras-data-attribute-2.0.1.jar
thymeleaf-spring4-3.0.2.RELEASE.jar
unbescape-1.1.4.RELEASE.jar
validation-api-1.1.0.Final.jar
xalan-2.7.0.jar
xercesImpl-2.8.0.jar
xml-apis-1.3.03.jar
xml-apis-ext-1.3.04.jar
xom-1.2.5.jar

每个建议都值得赞赏.

谢谢大家, 多尔菲兹

推荐答案

我有几个使用Log4j 2进行记录的Spring应用程序.推荐的方法,正如您所说的那样,是将commons-logging jar保留在其中,并使用log4j-jcl来进行.将其桥接到Log4j2.在这种情况下,不得存在jcl-over-slf4j jar.在这种情况下,日志记录调用将是commons-logging-> log4j-jcl-> log4j-api.

I have several Spring applications that log using Log4j 2. The recommended method, as you say is to leave the commons-logging jar in and use log4j-jcl to bridge it to Log4j 2. In that scenario the jcl-over-slf4j jar must not be present. In this case the logging calls will be commons-logging -> log4j-jcl -> log4j-api.

您已经说过,另一种方法是删除commons-logging jar并将其替换为jcl-over-slf4j.然后,您将需要注释掉的所有罐子.在这种情况下,日志记录调用将是jcl-over-slf4j-> slf4j-api-> log4j-slf4j-impl-> log4j-api.

As you have said, the other way is to remove the commons-logging jar and replace it with jcl-over-slf4j. You would then need all the jars you have commented out. In this case the logging calls would be jcl-over-slf4j -> slf4j-api -> log4j-slf4j-impl -> log4j-api.

如您所见,第二条路径更长.

As you can see the second path is a bit longer.

顺便说一句-您的log4j2配置缺少包装的<configuration>元素.

BTW - Your log4j2 configuration is missing the wrapping <configuration>element.

这篇关于Log4j2和Spring 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆