Calfater
Calfater

Reputation: 1335

Log4j default placeholder value

I use log4j with this appender:

<appender name="fileAppenderRoot" class="org.apache.log4j.FileAppender">
    <param name="file" value="${log.location}/logFile.log" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ISO8601} %-5p [%c{1}] - %m%n" />
    </layout>
</appender>

If I start the server with -Dlog.location=/path/to/logs, log4j will log into /path/to/logs/logFile.log but if I specify nothing, log4j will fail appending to /logFile.log.

Is it possible to specify a default placeholder value?

Upvotes: 4

Views: 3251

Answers (3)

v.ladynev
v.ladynev

Reputation: 19976

The simplest way is add the default value in the code

private static void configureLogger() {
    String logLocation = System.getProperty("log.location");
    if (logLocation == null) {
        System.setProperty("log.location", "/path/to/logs");
    }

    // if a logger is needed in the current class, create it after 

    log = Logger.getLogger(SomeClass.class);
}

You need to add this code before all code that uses the logger. It can be added in a startup servlet, for example. Please, don't create a logger in the usual way, with a static field initializer, in such servlet.

Upvotes: 0

David
David

Reputation: 1177

I know this doesn't directly answer your question, but I find a better solution is to use a whole server-specific log4j configuration file. This allows customization of specific logger levels, etc. on a per-server basis.

Both WebLogic and GlassFish (and probably other servers) provide mechanisms to place extra files on the classpath for one specific application. You can use these mechanisms to override a default log4j.xml baked into the WAR. WebLogic provides File Loading Overrides and GlassFish Application-Specific Class Loading.

Upvotes: 0

Santhosh
Santhosh

Reputation: 8217

No. you need to provide a location to write the log file.

If you need to generate the logs in the folder where you war is deployed ,

<param name="file" value="../logs/logFile.log" />

will create logfile in the folder where you have deployed

Read more here

Upvotes: 2

Related Questions