我无法让Log4j2在日志文件上写入Spring和Spring Security日志消息(后者我绝对需要解决一个问题)。我在这里和其他地方读了很多(当然,就像在Spring参考中),但我无法使我找到的解决方案适用于我的案例。
需要注意的是,在下面的两个解决方案中,地雷日志记录消息和hibernate消息都正确地写入了文件。该问题只影响Spring消息。
据我所知,Spring使用jcl(commons-logging),有两种方法可以让Spring使用Log4J2:
我试图从所有其他依赖项中排除commons-logging,以便手动控制它的包含。在第一种解决方案中,我让它包括在内,在第二种方案中,我排除了它,但我看到了关于这个话题的不同意见。无论如何,在这两种情况下Spring都不会日志记录。
我可以看到hibernate使用jboss-logging。这是它在两种配置上都起作用的原因吗?它的包含对Spring日志记录会是一个问题吗?
这是我的环境:
我猜这是一个依赖问题:一些不应该存在的库或者缺少了库。
<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>
<!–<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.6.1</version>
</dependency>–>
<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>
<!–<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</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>
-->
<!-- 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>
<?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>
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
谢谢大家,dolfiz
我有几个使用log4j2进行日志记录的Spring应用程序。如您所说,推荐的方法是保留commons-logging jar并使用log4j-jcl将其桥接到log4j2。在这种情况下,jcl-over-slf4j jar必须不存在。在本例中,日志记录调用将是commons-logging->log4j-jcl->log4j-api。
正如您所说的,另一种html" target="_blank">方法是删除commons-logging jar并用jcl-over-SLF4J替换它。然后,您将需要您注释掉的所有JAR。在这种情况下,日志记录调用将是jcl-over-slf4j->slf4j-api->log4j-slf4j-impl->log4j-api。
如您所见,第二条路径稍长一些。
我目前正在尝试用log4j/slf4j和java 11构建一个应用程序,但在运行时我面临这个问题: 此问题的根源代码是我的记录器的初始化: 我依赖于: org.apache.logging.log4j: log4j-api: 2.11.1 org.apache.logging.log4j: log4j-core: 2.11.1 org.apache.logging.log4j: log4j-slf
我们使用spring boot 2.1.5和starter parent作为pom依赖项。 Spring启动使用默认的日志回馈进行日志记录,我们尚未显式切换到Log4j2或更改任何配置。下面是我们的项目依赖关系树。 我们的项目中有很多lombok@log4j2注释。但是,我们在依赖关系树中发现我们没有任何log4j2-core jar依赖项(发现它容易受到最近log4j问题的影响)。 lombok
我需要将事件记录到系统日志中。我使用lo4j2和syslog appender。我的附加程序在如下所示: 如您所见,我有一个控制台appender和一个具有特定PatternLayout的RollingFile appender。我想对Syslog appender使用相同的PatternLayout。然而,似乎总是在syslog中使用预定义的消息布局。我试着做到以下几点: 但这没有任何效果。系统
问题内容: 我已将应用程序迁移到log4j 2,并通过log4j2.xml对其进行了配置 但是,我正在使用的某些库取决于log4j1。如果我使用以下命令运行该应用程序: log4j 1抱怨找不到配置文件。我正在使用log4j 2,log4j-1.2-api-2.0-rc1.jar提供的log4j 1.x桥。是否可以使用单个log4j2.xml进行配置? 我尝试过的替代方法是同时配置log4j和lo
我已经将应用程序迁移到Log4J2,并通过log4j2.xml对其进行了配置 但是,我使用的一些库依赖于log4j1。如果我使用以下方式运行应用程序: 我尝试的另一种方法是将log4j和log4j2配置在一起: 我担心的是日志配置文件和输出的碎片化。我还担心log4j.xml和log4j2.xml之间可能存在的冲突。例如,logfile Error.log被配置为使用log4j1中的FileApp
我的问题是:如何在使用Log4j2时查看所有记录器的完整列表,而不在Spring-boot应用程序中进行登录? 编辑:这里是我的pom.xml