tomcat - 如何登录jar库的静态 block

标签 tomcat logging memory-leaks log4j static-block

我有以下情况:

  • 我们有一个公共(public)库(由我们的组织制作),它有一些静态类(因为优化在内存中有很多记录)并且它部署在 tomcat 的 lib 目录中(可以访问所有部署的网络应用程序) .
  • 静态类需要访问数据库连接,为此(如果有任何问题)我们使用 log4j,但该方法会产生以下问题:

log4j:WARN No appenders could be found for logger (dev.sample.test.TestLog4J). log4j:WARN Please initialize the log4j system properly.

And also when the server is stop, there are some memory leaks in the webapps that load (or use) the static class:
Jul 24, 2014 11:29:34 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/fake_smsc] has started
Jul 24, 2014 11:29:34 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/fake_smsc] appears to have started a thread named [MultiThreadedHttpConnectionManager cleanup] but has failed to stop it. This is very likely to create a memory leak.
Jul 24, 2014 11:29:34 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/fake_smsc] appears to have started a thread named [Timer-2] but has failed to stop it. This is very likely to create a memory leak.
Jul 24, 2014 11:29:34 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/fake_smsc] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@65686a12]) and a value of type [org.mockito.configuration.DefaultMockitoConfiguration] (value [org.mockito.configuration.DefaultMockitoConfiguration@2a0bf7c1]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 24, 2014 11:29:34 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/fake_smsc] created a ThreadLocal with key of type [com.google.gson.Gson$1] (value [com.google.gson.Gson$1@7c43d507]) and a value of type [java.util.HashMap] (value [{}]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 24, 2014 11:29:34 AM org.apache.catalina.loader.WebappClassLoader checkThreadLocalMapForLeaks
SEVERE: The web application [/fake_smsc] created a ThreadLocal with key of type [java.lang.ThreadLocal] (value [java.lang.ThreadLocal@4956fe4d]) and a value of type [org.mockito.internal.progress.MockingProgressImpl] (value [iOngoingStubbing: org.mockito.internal.stubbing.OngoingStubbingImpl@6e5196d8, verificationMode: null, stubbingInProgress: null]) but failed to remove it when the web application was stopped. Threads are going to be renewed over time to try and avoid a probable memory leak.
Jul 24, 2014 11:29:35 AM org.apache.catalina.core.StandardContext reload
INFO: Reloading Context with name [/fake_smsc] is completed
Jul 24, 2014 11:29:43 AM org.apache.catalina.core.StandardServer await
INFO: A valid shutdown command was received via the shutdown port. Stopping the Server instance.
Jul 24, 2014 11:29:43 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["http-bio-9191"]
Jul 24, 2014 11:29:43 AM org.apache.coyote.AbstractProtocol pause
INFO: Pausing ProtocolHandler ["ajp-bio-9109"]
Jul 24, 2014 11:29:43 AM org.apache.catalina.core.StandardService stopInternal
INFO: Stopping service Catalina
Jul 24, 2014 11:29:44 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/sms_services] appears to have started a thread named [MultiThreadedHttpConnectionManager cleanup] but has failed to stop it. This is very likely to create a memory leak.
Jul 24, 2014 11:29:44 AM org.apache.catalina.loader.WebappClassLoader clearReferencesThreads
SEVERE: The web application [/sms_services] appears to have started a thread named [ActiveMQ Connection Executor: vm://localhost#2] but has failed to stop it. This is very likely to create a memory leak.
Jul 24, 2014 11:29:44 AM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["http-bio-9191"]
Jul 24, 2014 11:29:44 AM org.apache.coyote.AbstractProtocol stop
INFO: Stopping ProtocolHandler ["ajp-bio-9109"]
Jul 24, 2014 11:29:44 AM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["http-bio-9191"]
Jul 24, 2014 11:29:44 AM org.apache.coyote.AbstractProtocol destroy
INFO: Destroying ProtocolHandler ["ajp-bio-9109"]

此问题在以下位置有详细记录:https://wiki.apache.org/commons/Logging/StaticLog但是没有彻底解决这个问题(比如 tomcat 服务器的 logging.properties 文件中的一些配置)

记录日志的java类的代码非常简单:

public class TestLog4J {
    private static Logger log = Logger.getLogger(TestLog4J.class);

    static {
        log.debug("We created the instance of the Test class.");

        try {
            //Do black magic connecting to the db
            connectDatabase();
        } catch (Exception e) {
            log.error("What a Terrible Failure (WTF): ", e);
        }
    }
}

那么,有哪些选择呢?使用 System.out.println(...) 进行“记录”似乎有点难看,所以我希望可以使用 log4j 解决此问题。

问候,感谢您的回答:)

最佳答案

好的,解决方案在我发布的链接中:

  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;

  public static void doStuff(...) {
    Log log = LogFactory.getLog("stuff");
    log.warn(...);
  }

https://wiki.apache.org/commons/Logging/StaticLog

引用私有(private)静态Logger log = Logger.getLogger(TestLog4J.class);必须删除,所有调用静态方法, block 和类中的日志必须用LogFactory的getLog方法完成,并且commons-logging-*.jar必须在tomcat服务器的lib文件夹中,所以自定义lib 可以找到它(当然还有 log4j.jar)。

关于tomcat - 如何登录jar库的静态 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24938331/

相关文章:

tomcat - 在删除 conf/Catalina/localhost 目录之前,Tomcat6 中所有应用程序中的 404

java - 如何使用Hibernate自带的JBoss logging?

jquery - 如何正确解除 jQuery 小部件中 dom 元素的绑定(bind)?

c - 使用 realloc 安全吗?

c++ - 将终端上的所有消息和警告打印到文件中?

memory-leaks - bitcoind 0.8.4发生大量内存泄漏,导致守护程序崩溃

tomcat - 如何通过 Tomcat 强制重试集成 Windows 身份验证?

java - tomcat.dbcp...无法为连接 URL '' 创建类 'null' 的 JDBC 驱动程序

java - 调用 Https 网址

c++ - 为事件记录系统设计(递归)记录对象