2

I am using log4j for my logs. My application has a simple XML configuration file and I need to have in my config file the path for the log file. At the moment, it's another XML configuration file for log4j that contains this:

<log4j:configuration>
    <appender name="file" class="org.apache.log4j.FileAppender">
    <errorHandler class="org.apache.log4j.helpers.OnlyOnceErrorHandler" />
        <param name="file" value="log.out" /> 
        (..........)

(log.out is the default log which is in the project's directory.) How can I move this configuration into my application's configuration file?

Joshua Taylor
  • 84,998
  • 9
  • 154
  • 353
Wojtek
  • 27
  • 1
  • 2
  • 10

1 Answers1

6

There are basically three options:

  1. During the build, create a config for log4j which contains an absolute path for the file parameter. Least flexible.

  2. Use a System property. While this gives you some flexibility, this causes problems when you run in a container (J2EE server) and you have several applications which all use log4j.

The second option comes in two flavors:

  1. You can specify the path for the log file using ${logFile} in the XML and use -DlogFile= on the command line to specify the path.

  2. You can keep the XML config in a different place and tell log4j to load it when it starts using -Dlog4j.configuration=/absolute/path/to/log4j.xml

Related articles:

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820