log4j2 创建空日志文件

log4j2 creates empty logs file

我是 log4j2 的新手,但是我有一个使用 log4j 的应用程序,我不得不将它更改为 log4j2 版本。

正在创建日志文件,但它始终为空,该项目不使用 maven,所以我没有任何 pom.xml,我只有 log4j2.xml,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
    <Appenders>
        <RollingFile name="archive" fileName="/${sys:ENTORNO}/online/es/web/logs/WebSeal_EAI${sys:cloneId}.log"
                filePattern="/${sys:ENTORNO}/online/es/web/logs/WebSeal_EAI${sys:cloneId}.log.%d{yyyy-MM-dd}.gz">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %log{36} - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" />
            </Policies>
        </RollingFile>
        <Console name="screen" target="SYSTEM_OUT">
            <PatternLayout>
                <Pattern>%d{HH:mm:ss.SSS} [%t] %-5level %log{36} - %m%n</Pattern>
            </PatternLayout>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="debug">
            <AppenderRef ref="archive"/>
        </Root>
    </Loggers>
</Configuration>

在 java 中,我加载了这样的配置:

            File file = new File(log4Jcfg);
            ConfigurationFactory factory =  XmlConfigurationFactory.getInstance();
            ConfigurationSource configurationSource = new ConfigurationSource(new FileInputStream(new File(log4Jcfg)));
            Configuration configuration = factory.getConfiguration(context, configurationSource);
            context = new LoggerContext("JournalDevLoggerContext");
            context.start(configuration);

log4Jcfg 具有 xml 路径。它创建的文件,但它总是空的,我真的不知道为什么。

我已经阅读了很多这样的问题,并且在我将 .xml 放入类路径时尝试了几乎所有的东西(我认为),我改变了一些关于 xml 的东西但是 none 对我有用。

任何人都知道发生了什么,我的意思是,如果文件已创建,为什么它不打印任何内容? 我加载配置的 .xml 或 java 代码有问题吗?

此外,此项目使用服务器 Websphere v9.0。

非常感谢。

如果有人遇到同样的问题,我已经解决了。 问题是我加载 xml 配置的方式;我把它改成了这样:

LoggerContext ctx = getLoggerContext();
ConfigurationSource configurationSource = new ConfigurationSource(new FileInputStream(new File(log4Jcfg)));
ctx.start(ConfigurationFactory.getInstance().getConfiguration(ctx, configurationSource));

现在完美运行!