本文介绍了在log4j中使用系统属性或变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想这样做:

<appender name="ErrorLog" class="org.apache.log4j.FileAppender">
        <param name="File" value="${error.log.path}"/>
        <param name="Append" value="true" />
        <param name="Threshold" value="ERROR"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%C{1} %L [%t] %d{dd MMM,yyyy HH:mm:ss.SSS} %-5p - %m%n" />
        </layout>
    </appender>

注意这一行:< param name =Filevalue = $ {error.log.path}/>

我试图像这样设置值:

public static void main(String[] args) {
     System.setProperty("error.log.path", "/test/crm/log/error.log");
     ApplicationContext context = new ClassPathXmlApplicationContext("blah.xml");
     ..........
     .......... 
  }

但是我没有看到任何影响。

But I don't see any effect.

在调用 main之前是否配置了log4j 方法?

还有其他办法吗?

推荐答案

查看

看起来你做的一切都是正确的。我不认为在主类中使用 System.setProperty()设置属性并通过命令行指定它,只要它发生在实际中log4j初始化。

It looks like you did everything right. I don't think there is any difference between setting the property inside your main class with System.setProperty() and specifying it via the command line as long as it happens befor actual log4j initialization.

我认为您的问题是您的日志框架在指定属性之前加载。
我可以说在调用配置程序时会配置日志框架(log4j)。像 BasicConfigurator.configure()(在你的情况下是它的xml配置器)的东西。

I think your issue is that your logging framework gets loaded before you specify the property.I can say that the logging framework (log4j) will get configured when you call the configurator. Stuff like BasicConfigurator.configure() (in your case its xml configurator).

否则第一次尝试使用日志记录将导致log4j配置不正确等消息。

Otherwise the first attempt to use the logging will cause message like "log4j is not configured properly".

真正的问题是你的'main'代码片段是否过于简单。

The real question is whether your code snippet with 'main' is not oversimplified.

考虑到这一点,我要问的另一个问题是 - 你是在一个容器内运行还是你正在运行一个真正的vanilla方法主要并自己配置一切?我问,因为如果你在容器中运行,容器本身可能会以某种方式配置其日志记录,例如JBoss会这样做。在这种情况下,需要进行更多的调查。

With this in mind, another question that I have to ask - whether you're running inside some container or you're running a real vanilla method main and configure everything by yourself? I'm asking because if you're running in container, the chances are that container will by itself somehow configure its logging, for example JBoss will do so. In this case more investigation is required.

希望这会有所帮助

这篇关于在log4j中使用系统属性或变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:43