获取Log4j2日志文件位置

时间:2017-10-25 13:54:19

标签: java log4j2

如何获取log4j日志文件的位置? Log4j ver 2.3配置:

<Appenders>
    <RollingFile name="ROLLLOGAPPENDER" fileName="./log/${infoFile}.log" filePattern="./log/${infoFile}-%i.log" append="true">
        <PatternLayout pattern="%d %-5p [%c{1}] %m%n"/>
        <ThresholdFilter level="${stdLevel}" onMatch="ACCEPT" onMismatch="DENY"/>
        <Policies>
            <SizeBasedTriggeringPolicy size="${sizeOfFile}" />
        </Policies>
        <DefaultRolloverStrategy max="${filesCount}"/>
    </RollingFile>
    <Async name="ASYNC-LOG">
        <AppenderRef ref="ROLLLOGAPPENDER"/>
    </Async>
</Appenders>

但它不起作用:

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.config.Configuration;
...

LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Configuration config = ctx.getConfiguration();
RollingFileAppender techical = (RollingFileAppender) config.getAppender(FILE_APPENDER_NAME);

java.lang.ClassCastException:

org.apache.logging.log4j.core.appender.AsyncAppender cannot be cast to org.apache.logging.log4j.core.appender.RollingFileAppender

0 个答案:

没有答案