views:

8123

answers:

5

In weblogic I can configure in the console for the Serverlog to use log4j instead of default JDK logging.

However the serverlog is not using a log4j.properties file, but seems to use the configuration in config.xml Even if the log4j.properties file is in the classpath and I set these properties:

set JAVA_OPTIONS=%JAVA_OPTIONS% -Dlog4j.configuration=file:<path>/log4j.properties
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger   
set JAVA_OPTIONS=%JAVA_OPTIONS% -Dweblogic.log.Log4jLoggingEnabled=true

Is it possible to use log4j.properties configuration for Weblogic Server Logging, or can I only change the log4j configuration with java code?

A: 

If you put the log4j.xml in your classpath, WebLogic will pick it up. I use Apache Commons logging with log4j in WebLogic, and it's an easy thing to do. No need for those Java options.

duffymo
This does not work. With the described java option, applications already log correctly using the log4j.properties configuration, but what I want is to configure the BEA SERVER LOGGING. Apparently the bea server logging does not look at the log4j.xml in the classpath.
Edwin
A: 

Where are you setting the above options? Try putting the -Dlog4j option in the Server Start options for each managed server that will use log4j

awithrow
The above options where set in the startup script (startweblogic.cmd). I see that the log4.properties is being used by custom-code I deploy, but it's not used by the Serverlog (and because Aqualogic logs to the Serverlog aqualogic logging is not using the log4j.properties configuration)
Edwin
A: 

To specify logging to a Log4j Logger instead of the default Java Logging:

* When you start the Administration Server, include the following Java option in the weblogic.Server command:

  -Dweblogic.log.Log4jLoggingEnabled=true

From: http://edocs.bea.com/wls/docs103/logging/config_logs.html#wp1014610

jon077
I already tried that, and even documented that in the question. (third java option). This is included in the java options of my admin-server but does not have the effect I wanted.
Edwin
Did you change the settings in config.xml?
jon077
The config.xml was changed based on the changes made in the console. It looks like the config.xml settings overrule the log4j.properties settings, so logging is configured in config.xml and I do not know how to change that to log4j.properties configuration.
Edwin
+2  A: 

I don't know anything about WebLogic in particular, but adding -Dlog4j.debug will cause log4j to tell you where it's looking for its configuration. I've found that to be invaluable when tracking down logging issues in tomcat previously.

Check out the docs for PropertyConfigurator and DOMConfigurator for details on the log4j configuration process.

Dominic Mitchell
A: 

I never got this working as I intented.

What I eventually did was create some kind of work-around. I register a Handler that listens to the Weblogic serverlog. From this handler I do my own logging to log4j. That logging can be redirected to do anything I want.

create a custom logHandler:

public class CustomLogHandler extends Handler {
..

    public CustomLogHandler () throws SecurityException, IOException,
            NamingException {

        String log4jConfig = LogFilterConfiguration.getLog4jDirectory();
        classlogger.info("log4j configured for file"+ log4jConfig );
        PropertyConfigurator.configure(log4jConfig);
        logFilterConfiguration = new LogFilterConfiguration();
    }

    public void publish(LogRecord record) {
        WLLogRecord rec = (WLLogRecord) record;
        if (!isLoggable(rec))
            return;
        if (getLoggerName().. is something i want to log) {
                        // do my own log4j logging
                  }

then create an ApplicationLifecycleListener. with a postStart method:

public void postStart(ApplicationLifecycleEvent evt) {
    Logger logger = LoggingHelper.getServerLogger();
    Handler oldHandler = null;
    Handler[] currentHandlers = logger.getHandlers();

              .. code to remove an old custom handler if exists...
    with something like logger.removeHandler(oldHandler);

    // add custom handler to serverlogger.
    CustomLogHandler h = null;      
    try {
        h = new CustomLogHandler ();
        // If handler was removed we can add a new version.
        if (!(unRemovedHandlerClasses.contains(h.getClass()))){
            logger.addHandler(h);
            registerMBean(h) ;
        }
    } catch (Exception nmex) {
    classLogger.error("Error adding CustomLogHandler to serverlogger "
                + nmex.getMessage());
        logger.removeHandler(h);
    }


}
Edwin