在使用System属性变量创建RollingFileAppender时,我收到以下错误。
ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.RollingFileAppender for element RollingFile. java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.logging.log4j.core.config.plugins.util.PluginBuilder.build(PluginBuilder.java:132)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createPluginObject(AbstractConfiguration.java:918)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:858)
at org.apache.logging.log4j.core.config.AbstractConfiguration.createConfiguration(AbstractConfiguration.java:850)
at org.apache.logging.log4j.core.config.AbstractConfiguration.doConfigure(AbstractConfiguration.java:479)
at org.apache.logging.log4j.core.config.AbstractConfiguration.initialize(AbstractConfiguration.java:219)
at org.apache.logging.log4j.core.config.AbstractConfiguration.start(AbstractConfiguration.java:231)
at org.apache.logging.log4j.core.LoggerContext.setConfiguration(LoggerContext.java:496)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:566)
at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:582)
at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:217)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:152)
at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
at org.apache.logging.log4j.LogManager.getLogger(LogManager.java:551)
at com.hifx.lens.services.AvroSerializerFactory.<init>(AvroSerializerFactory.java:16)
at com.hifx.lens.services.AvroSerializerFactory.init(AvroSerializerFactory.java:43)
at com.hifx.lens.Accumulo.main(Accumulo.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassCastException: org.apache.logging.log4j.core.appender.FileManager cannot be cast to org.apache.logging.log4j.core.appender.rolling.RollingFileManager
at org.apache.logging.log4j.core.appender.rolling.RollingFileManager.getFileManager(RollingFileManager.java:103)
at org.apache.logging.log4j.core.appender.RollingFileAppender.createAppender(RollingFileAppender.java:191)
... 27 more
我的log4j2.yml配置文件是
Configutation:
name: Default
Properties:
Property:
name: LOG_PATH
value: "logs"
Appenders:
Console:
name: Console_Appender
target: SYSTEM_OUT
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
File:
name: File_Appender
fileName: ${sys:LOG_PATH}/log.log
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
RollingFile:
name: RollingFile_Appender
fileName: ${sys:LOG_PATH}/log.log
filePattern: "logs/archive/rollingfile.log.%d{yyyy-MM-dd-hh-mm}.gz"
PatternLayout:
pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"
Policies:
SizeBasedTriggeringPolicy:
size: 100 KB
DefaultRollOverStrategy:
max: 30
Loggers:
Root:
level: debug
AppenderRef:
- ref: RollingFile_Appender
Logger:
- name: Accumulo_File_logger
level: debug
AppenderRef:
- ref: File_Appender
如果我将RollingFile_Appender的文件名更改为使用属性名称,即:
fileName: ${LOG_PATH}/log.log
然后错误就消失了,一切正常。
如果我使用File_Appender(也使用相同的System属性变量(sys:LOG_PATH)),那么一切正常。
如果我将sys更改为env,则会出现相同的错误: 我认为杰克逊有一些解析问题。 我需要从环境变量配置日志路径。
使用依赖
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.6.2</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>2.6.2</version>
</dependency>
有人请帮帮我
答案 0 :(得分:2)
这是因为您定义了File_Appender和RollingFile_Appender来登录同一个文件。他们有点&#34;拥有&#34;他们的日志文件,所以一旦为${sys:LOG_PATH}/log.log
创建了File appender,就无法创建指向同一文件的RollingFile appender。配置文件中的第二个appender尝试创建一个负责该文件的LogManager,找到第一个已存在的文件,因此尝试转换为它需要的类 - 并且失败。
如果将RollingFile_Appender移到File_Appender上方,则异常将是:
main ERROR Unable to invoke factory method in class class org.apache.logging.log4j.core.appender.FileAppender for element File. java.lang.reflect.InvocationTargetException
现在RollingFile_Appender将拥有${sys:LOG_PATH}/log.log
文件,但由于同样的原因,您无法创建File_Appender。
解决方案:删除其中一个appender,同时两者都没有意义 - 它们必须相互竞争,或者将每个appen指向不同的文件。