简体   繁体   English

在weblogic 9/10中使用log4j日志记录

[英]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. 在weblogic中,我可以在控制台中配置Serverlog使用log4j代替默认的JDK日志记录。

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: 但是,serverlog并未使用log4j.properties文件,而是似乎使用config.xml中的配置,即使log4j.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? 是否可以将log4j.properties配置用于Weblogic Server日志记录,还是只能使用Java代码更改log4j配置?

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. 我对WebLogic并不特别了解,但是添加-Dlog4j.debug将使log4j告诉您它在哪里寻找其配置。 I've found that to be invaluable when tracking down logging issues in tomcat previously. 我发现在以前跟踪tomcat中的日志记录问题时,这是无价的。

Check out the docs for PropertyConfigurator and DOMConfigurator for details on the log4j configuration process. 查看有关PropertyConfiguratorDOMConfigurator的文档,以获取有关log4j配置过程的详细信息。

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. 我注册了一个侦听Weblogic服务器日志的处理程序。 From this handler I do my own logging to log4j. 从该处理程序中,我将自己记录到log4j。 That logging can be redirected to do anything I want. 可以重定向该日志以执行我想要的任何事情。

create a custom logHandler: 创建一个自定义的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. 然后创建一个ApplicationLifecycleListener。 with a postStart method: 使用postStart方法:

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. 如果将log4j.xml放在类路径中,WebLogic会选择它。 I use Apache Commons logging with log4j in WebLogic, and it's an easy thing to do. 我在WebLogic中将Apache Commons日志与log4j一起使用,这是一件容易的事。 No need for those Java options. 不需要那些Java选项。

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 尝试将-Dlog4j选项放在将使用log4j的每个受管服务器的“服务器启动”选项中

To specify logging to a Log4j Logger instead of the default Java Logging: 要指定记录到Log4j记录器而不是默认Java记录:

* 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 来自: http : //edocs.bea.com/wls/docs103/logging/config_logs.html#wp1014610

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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