Use system properties OR variables in log4j

Look at this thread

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.

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).

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

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

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.

Hope this helps


System Properties can be used as ${user.home}, pick required from here http://docs.oracle.com/javase/tutorial/essential/environment/sysprop.html

example :

<appender name="errorLog" class="com.qait.logger.IOPFileAppender">
    <param name="Threshold" value="ERROR" />
    <param name="File"
        value="${user.home}/Harvestors/IOP Error Logs/error.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d%-5p  [%c{1}] %m %n" />
    </layout>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="LevelToMatch" value="ERROR" />
        <param name="AcceptOnMatch" value="true" />
    </filter>
    <filter class="org.apache.log4j.varia.DenyAllFilter" />
</appender>

Tags:

Java

Log4J