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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/576937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 16:26:04  来源:igfitidea点击:

Using log4j logging in weblogic 9/10

javalogginglog4jweblogic

提问by Edwin

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 服务器日志记录,或者我只能使用 java 代码更改 log4j 配置?

采纳答案by Dominic Mitchell

I don't know anything about WebLogic in particular, but adding -Dlog4j.debugwill 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.

我对 WebLogic 一无所知,但是添加-Dlog4j.debug会导致 log4j 告诉您它在哪里寻找其配置。我发现这在之前在 tomcat 中跟踪日志记录问题时非常有用。

Check out the docs for PropertyConfiguratorand DOMConfiguratorfor details on the log4j configuration process.

有关 log4j 配置过程的详细信息,请查看PropertyConfiguratorDOMConfigurator的文档。

回答by duffymo

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.

如果您将 log4j.xml 放在您的类路径中,WebLogic 会选取它。我在 WebLogic 中使用带有 log4j 的 Apache Commons 日志记录,这很容易做到。不需要那些 Java 选项。

回答by awithrow

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 的每个托管服务器的服务器启动选项中

回答by jon077

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

要指定日志记录到 Log4j Logger 而不是默认的 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

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

回答by Edwin

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.

我最终做的是创造某种变通办法。我注册了一个监听 Weblogic 服务器日志的处理程序。从这个处理程序我自己记录到 log4j。该日志记录可以重定向到我想做的任何事情。

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. with a postStart method:

然后创建一个 ApplicationLifecycleListener。使用 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);
    }


}