Java 使用log4j2.xml初始化slf4j
我想在log4j上使用slf4j。我在pom.xml中添加了以下依赖项(我对slf4j使用了1.7.25,对log4j2使用了2.10.0): 我搜索了一种提供配置文件的正确方法,结果是:Java 使用log4j2.xml初始化slf4j,java,log4j2,slf4j,Java,Log4j2,Slf4j,我想在log4j上使用slf4j。我在pom.xml中添加了以下依赖项(我对slf4j使用了1.7.25,对log4j2使用了2.10.0): 我搜索了一种提供配置文件的正确方法,结果是: LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false); File file = new File("path/to/a/different/log4j2.xml");
LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
File file = new File("path/to/a/different/log4j2.xml");
context.setConfigLocation(file.toURI());
问题是,在我的例子中,LogManager.getContext(false)将始终返回Slf4JLoggerContext的实例(考虑到我使用slf4j作为记录器的门面),初始化将失败,ClassCastException。我试图存储Slf4JLoggerContext的实例,但它没有为该上下文提供setter。我也没有找到从log4j检索LoggerContext的方法
有没有办法向slf4j提供配置文件(log4j2.xml),以便查看所有的附加器和记录器
更新
考虑这个我的配置文件(我替换了原来的包和AppSame名称):
10
${oo.home}/var/logs
10MB
警告
org.apache.log4j.PatternLayout
org.apache.log4j.PatternLayout
org.apache.log4j.PatternLayout
从您的问题来看,您似乎没有试图指定自定义配置位置!请参阅自动配置
部分:
Log4j能够在运行期间自动配置自身
初始化。当Log4j启动时,它将定位所有
ConfigurationFactory插件,并从
从最高到最低。交付时,Log4j包含四个
ConfigurationFactory实现:一个用于JSON,一个用于YAML,一个用于
一个用于属性,一个用于XML
Log4j将检查“Log4j.configurationFile”系统属性,并,
如果设置,将尝试使用
与文件扩展名匹配的ConfigurationFactory
- 如果未设置任何系统属性,则properties ConfigurationFactory将在类路径中查找log4j2-test.properties
- 如果未找到此类文件,YAML配置工厂将在类路径中查找log4j2-test.YAML或log4j2-test.yml
- 如果未找到此类文件,JSON ConfigurationFactory将在类路径中查找log4j2-test.JSON或log4j2-test.jsn
- 如果没有找到这样的文件,XML ConfigurationFactory将查找 类路径中的log4j2-test.xml
- 如果找不到测试文件,properties ConfigurationFactory将在类路径上查找log4j2.properties
- 如果找不到属性文件,YAML ConfigurationFactory将在上查找log4j2.YAML或log4j2.yml 类路径
- 如果无法找到YAML文件,JSON ConfigurationFactory将在类路径上查找log4j2.JSON或log4j2.jsn
- 如果无法找到JSON文件,XML ConfigurationFactory将尝试在类路径上找到log4j2.XML
- 如果找不到配置文件,将使用DefaultConfiguration。这将导致日志输出转到控制台
您可以通过将其放置在
src/main/resources
文件夹中或在命令行上使用-cp/-classpath
指定它来将其放置在“类路径”上。我已经测试了此配置。log4j2文件必须位于类路径中
Maven
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
org.apache.logging.log4j
log4j型芯
org.apache.logging.log4j
log4j-slf4j-impl
log4j2.xml
另外,添加xsd可以帮助您创建配置。我已经为spring框架添加了记录器。注意,jcl-over-slf4j需要用于spring的内部日志记录
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error"
xmlns="http://logging.apache.org/log4j/2.0/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config
https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.springframework" level="error">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
我已经提供了名为“log4j.configurationFile”的参数,该参数的值表示我的log4j2.xml文件的路径。我认为这个问题表现在我使用slf4j作为门面这一事实上。它知道也要搜索那个论点吗?在我提供的示例中,它可能会在ClassCastException之后中断记录器,但我尝试了另一种方法,其中记录器也将无法设置上下文,因为它将从log4j而不是Slf4JLoggerContext中期望LoggerContext,但它不会引发异常(它只返回null,并且没有NullPointerExc).只要您在DEP中包含了桥接器(您已经有了),slf4j的内容就应该通过log4j2进行记录。你是如何通过辩论的?当您尝试我在底部提到的默认类路径位置时发生了什么?我在命令行-Dlog4j.configuration=fullPathToMyFile传递了参数。该文件位于类路径(src/main/resources)上。我没有创建日志文件。相反,它在几乎任何操作之后都会在我的包装器中记录一个警告。警告与帖子中的警告完全相同(没有为logger找到appender)。我的意思是,你基本上做了正确的事情。您的log4jxml
config是什么样子的?用org.slf4j中的log4j-over-slf4j替换当前网桥(log4j-slf4j-impl)怎么样?
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Properties>
<Property name="def.files.backup.count">10</Property>
<Property name="log.file.path">${oo.home}/var/logs</Property>
<Property name="def.file.max.size">10MB</Property>
<Property name="log.level">WARN</Property>
</Properties>
<ThresholdFilter/>
<Appenders>
<RollingFile name="Appender1" fileName="${log.file.path}/file1.log" maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
<RollingFile name="Appender2" fileName="${log.file.path}/file2.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
<RollingFile name="Appender3" fileName="${log.file.path}/file3.log"
maxFileSize="${def.file.max.size}"
maxBackupIndex="${def.files.backup.count}">
<PatternLayout>org.apache.log4j.PatternLayout</PatternLayout>
<Policies>
<SizeBasedTriggeringPolicy size="10MB"/>
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<AsyncLogger name="com.package1.oo" level="${log.level}" additivity="false">
<AppenderRef ref="Appender1"/>
</AsyncLogger>
<AsyncLogger name="io.package2" level="${log.level}" additivity="false">
<AppenderRef ref="Appender2"/>
</AsyncLogger>
<AsyncLogger name="com.package3.package3" level="${log.level}" additivity="false">
<AppenderRef ref="Appender3"/>
</AsyncLogger>
<AsyncLogger name="org.package4" level="${log.level}">
</AsyncLogger>
<AsyncLogger name="com.package5.Class1" level="${log.level}">
</AsyncLogger>
<AsyncRoot level="${log.level}">
<AppenderRef ref="Appender1"/>
<AppenderRef ref="Appender2"/>
<AppenderRef ref="Appender3"/>
</AsyncRoot>
</Loggers>
</Configuration>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
</dependency>
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error"
xmlns="http://logging.apache.org/log4j/2.0/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://logging.apache.org/log4j/2.0/config
https://raw.githubusercontent.com/apache/logging-log4j2/master/log4j-core/src/main/resources/Log4j-config.xsd">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} [%t] %-5level %logger{36}} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="debug">
<AppenderRef ref="Console"/>
</Root>
<Logger name="org.springframework" level="error">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>