简体   繁体   中英

Using log4j logging in weblogic 9/10

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?

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.

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);
    }


}

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM