如何在Spring项目中使用属性来配置log4j.xml[英] How to use properties in a Spring project to configure log4j.xml

本文是小编为大家收集整理的关于如何在Spring项目中使用属性来配置log4j.xml的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我在春季项目中有多个属性文件.春季上下文以方便的方式加载这些属性并处理属性.是否有一种方法可以将我的Spring Configuration XML文件(即${myprop})可用的属性获取并在我的log4j.xml文件中以类似的方式使用它们?我知道我可以在启动时使用-Dprop=value将系统属性传递给log4j,但是我更喜欢在项目中的属性文件中使用所有配置.这可能吗?

我的应用程序在tomcat中运行.

推荐答案

在将多个属性文件集成到一个属性之后,尝试使用此类.

public class DOMConfiguratorWithProperties extends DOMConfigurator {

    private Properties propertiesField = null;

    public synchronized Properties getProperties() {
        return propertiesField;
    }

    public synchronized void setProperties(final Properties properties) {
        propertiesField = properties;
    }

    @Override
    protected String subst(final String value) {
        return super.subst(value, getProperties());
    }

    public static void configure(final String filename) {
        new DOMConfiguratorWithProperties().doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }

    public static void configure(
            final String filename,
            final Properties properties) {
        DOMConfiguratorWithProperties configurator = new DOMConfiguratorWithProperties();
        configurator.setProperties(properties);
        configurator.doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }
}

其他推荐答案

我认为,与log4j交互的唯一方法是通过嵌套诊断上下文.

因此,如果您非常绝望,则可以编写一个弹簧AOP方面,该方面为您的弹簧Bean日志设置诊断上下文(并且可以使用那里的属性).但是,这将要求日志可用于Spring Bean,因此您需要在服务接口中添加getLog()方法(如果您使用static extackJ编译,这会变得容易得多,并且在 Action in Action ).

但是,没有使用AOP,我想不出一种让Spring和Log4J相互作用的明智方法.

本文地址:https://www.itbaoku.cn/post/1574991.html

问题描述

I have multiple properties files in my Spring project. The spring context loads these properties and handles property overriding in a convenient manner. Is there a way to take the properties that are available to my Spring configuration XML files (ie. ${myprop}) and use them in a similar fashion in my log4j.xml file? I know that I can pass system properties to log4j using -Dprop=value on startup, but I would prefer having all of the configuration in the properties files in my project. Is this possible?

My app runs in Tomcat.

推荐答案

Try to use this class, after integrating your multiple properties files to one Properties.

public class DOMConfiguratorWithProperties extends DOMConfigurator {

    private Properties propertiesField = null;

    public synchronized Properties getProperties() {
        return propertiesField;
    }

    public synchronized void setProperties(final Properties properties) {
        propertiesField = properties;
    }

    @Override
    protected String subst(final String value) {
        return super.subst(value, getProperties());
    }

    public static void configure(final String filename) {
        new DOMConfiguratorWithProperties().doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }

    public static void configure(
            final String filename,
            final Properties properties) {
        DOMConfiguratorWithProperties configurator = new DOMConfiguratorWithProperties();
        configurator.setProperties(properties);
        configurator.doConfigure(
                filename,
                LogManager.getLoggerRepository());
    }
}

其他推荐答案

I think the only way you can interact with Log4J is through the Nested diagnostic context.

So if you are very desperate, you could write a Spring AOP aspect that sets the diagnostic context for your Spring Bean logs (and you could use the properties there). However, that would require that the Log be available to the Spring Bean, so you would need to add a getLog() method to your service interfaces (this gets a lot easier if you use static AspectJ compilation and is described in AspectJ in Action).

But short of using AOP, I can't think of a sensible way to let Spring and Log4J interact.