Log4j default headroom

I am using log4j with this application:

<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 logs in /path/to/logs/logFile.log

, but if I don't specify anything, log4j is not appended to /logFile.log

.

Is it possible to specify a default value for bookmarks ?

+4


source to share


3 answers


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

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

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

      



will create logfile

in the folder where you deployed

More details here

+2


source


I know this does not directly answer your question, but I believe the best solution is to use the entire server-specific log4j config file. This allows you to customize certain levels of registration, etc. For each server.



Both WebLogic and GlassFish (and possibly other servers) provide mechanisms for placing additional files in the classpath for one particular application. These mechanisms can be used to override the default log4j.xml

baked into the WAR. WebLogic provides File Overflow and GlassFish Application Class Loading .

0


source


The easiest way is to add a default value in your 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 must add this code before all the code the logger uses. For example, it can be added to the startup servlet. Please do not create a logger in the usual way, with a static field initializer, in such a servlet.

0


source







All Articles