20

I want to to do like this:

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

Notice this line: <param name="File" value="${error.log.path}"/>

I tried to set the values like this:

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.

Is log4j gets configured before calling the main method?

Is there any other way to do this?

Mawia
  • 4,220
  • 13
  • 40
  • 55
  • 1
    So is a logfile produced with the name `${error.log.path}`? I.e. is it just the substitution that's not working, or are you seeing no log data at all? – Duncan Jones Jan 15 '13 at 07:20
  • It's a working project. I just change the value from "/test/crm/log/error.log" to "${error.log.path}". How can I set this variable? – Mawia Jan 15 '13 at 07:23
  • Read my comment carefully - what *exactly* happens when you run your application? Do you see log output? – Duncan Jones Jan 15 '13 at 07:24
  • No output. I mentioned "But I don't see any effect" – Mawia Jan 15 '13 at 07:30
  • ConsoleAppender is displaying, but not FileAppender. No effect on FileAppender nor any directories created. – Mawia Jan 15 '13 at 07:32
  • First off, add -Dlog4j.debug to the java command line to see how log4j configures itself, it is probably getting configured before you set the system property. You can force it to reconfigure via "DOMConfigurator.configure(URL)" – Malcolm Boekhoff Apr 20 '17 at 01:08

6 Answers6

11

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

Community
  • 1
  • 1
Mark Bramnik
  • 39,963
  • 4
  • 57
  • 97
5

Access to your property via "sys:" prefix.

Example:

        <param name="File" value="${sys:error.log.path}"/>

For more information follow this link: https://logging.apache.org/log4j/2.x/manual/lookups.html

s1ckret
  • 51
  • 1
  • 1
4

You can do it by configure appender pragmatically

  FileAppender fa = new FileAppender();
  fa.setFile("/test/crm/log/error.log");
  fa.setLayout(new 
   PatternLayout("%C{1} %L [%t] %d{dd MMM,yyyy HH:mm:ss.SSS} %-5p - %m%n"));
  fa.setThreshold(Level.ERROR);
  fa.setAppend(true);
  fa.activateOptions();
  Logger.getRootLogger().addAppender(fa);
  // similarly you can add all appenders.

 // or just append file name alone 
 Logger log = Logger.getLogger(YourClass.class);
 FileAppender appender = (FileAppender) log.getAppender("ErrorLog");
 appender.setFile("appender");
vels4j
  • 11,208
  • 5
  • 38
  • 63
4

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>
gursahib.singh.sahni
  • 1,559
  • 3
  • 26
  • 51
1

maven document:

System properties. The formats are ${sys:some.property} and ${sys:some.property:-default_value}.

from Maven Property Substitution

yuefei7746
  • 11
  • 1
-1

Setting the system property does not come into affect here. You'll need to pass it as a argument to java while executing. Try

 java -Derror_log_path=/test/crm/log/error.log

Note: I am not sure if dot . works in there so replaced it with underscore _.

mtk
  • 13,221
  • 16
  • 72
  • 112