JUL适配器不适用于Jersey

aho*_*xha 8 java jersey java-ee log4j2

我正在尝试使用JUL Adapter将Java Util Logging委派给Log4j2.更准确地说,任何使用JUL生成日志的第三方库都应该委托给Log4j2.

作为一个简单的练习,我创建了一个使用库的独立应用程序(我创建此库用于测试目的,它使用JUL生成日志)来测试JUL适配器.当我按照此处所述更改日志管理器时,我可以看到效果.它工作正常.

她是代码:

import org.apache.logging.log4j.LogManager;
import com.ah.loggen.LogGenerator;

public class TestLogging {

    static {
        System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
    }

    private static final org.apache.logging.log4j.Logger LOG4J = LogManager.getLogger();

    public static void main(String[] args) {
        System.out.println("Java Util Logging");
        LogGenerator.generateError("This is an error message.");
        LogGenerator.generateInfo("This is an info message.");
        LogGenerator.generateWarning("This is a warning message.");
        System.out.println("LOG4J");
        LOG4J.info("[LOG4J] This is an info message.");
        LOG4J.error("[LOG4J] This is an error message.");
        LOG4J.warn("[LOG4J] This is a warning message.");
    }
}
Run Code Online (Sandbox Code Playgroud)

需要的依赖关系:

dependencies {
    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.10.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.10.0'
    compile group: 'org.apache.logging.log4j', name: 'log4j-jul', version: '2.10.0'

    compile files('lib/loggen.jar')

    testCompile 'junit:junit:4.12'
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法在使用Jersey的java Web应用程序上使用它.Jersey使用JUL,我正在尝试用Log4j2桥接它.

这是build.gradle文件:

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'eclipse-wtp'
apply from: 'https://raw.github.com/akhikhl/gretty/master/pluginScripts/gretty.plugin'

repositories {
    mavenCentral()
}

task wrapper(type: Wrapper) {
    gradleVersion = '4.4.1'
}

dependencies {
    compile "javax.ws.rs:javax.ws.rs-api:2.1"
    compile "org.glassfish.jersey.core:jersey-server:2.22.1"
    compile "org.glassfish.jersey.containers:jersey-container-servlet:2.22.1"
    compile "org.glassfish.jersey.media:jersey-media-json-jackson:2.22.1" 
    providedCompile "javax.servlet:javax.servlet-api:3.1.0" 

    compile group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.8'
    compile group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.8'
    compile group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.8'
    compile group: 'org.apache.logging.log4j', name: 'log4j-jul', version: '2.8'
}


gretty {
    servletContainer = 'tomcat8'
    httpPort = 8081
}
Run Code Online (Sandbox Code Playgroud)

我试过这些选项:

  1. 在扩展的类中更改日志管理器Application.

    package com.ahoxha.test;
    
    import java.util.HashSet;
    import java.util.Set;
    import javax.annotation.PostConstruct;
    import javax.ws.rs.ApplicationPath;
    import javax.ws.rs.core.Application;
    import com.ahoxha.filters.JerseyLoggingFilter;
    
    @ApplicationPath("")
    public class HelloApplication extends Application {
        @Override
        public final Set<Class<?>> getClasses() {
            final Set<Class<?>> classes = new HashSet<>();
            classes.add(Hello.class);
            classes.add(MessageResource.class);
            classes.add(JerseyLoggingFilter.class);
            return classes;
        }
    
        @PostConstruct
        public void init() {
            String cn = "org.apache.logging.log4j.jul.LogManager";
            System.setProperty("java.util.logging.manager", cn);
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在实现的类中更改日志管理器 ServletContextListener

    package com.ahoxha.context;
    
    import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.annotation.WebListener;
    
    @WebListener
    public class ContextInitializer implements ServletContextListener {
    
        @Override
        public void contextInitialized(ServletContextEvent sce) {
            System.out.println("Initializing context.");
            System.setProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager");
        }
    
        @Override
        public void contextDestroyed(ServletContextEvent sce) {
            System.out.println("Context destroyed.");
        }
    }
    
    Run Code Online (Sandbox Code Playgroud)
  3. 除了上面两个选项之外,我还尝试将日志管理器设置为a static block(对于选项1和2).

不幸的是,这些选项都不适合我.我想知道应该在哪里做.有什么东西我错过了吗?

Vik*_*eva 4

java.util.logging.LogManager当像 , 这样的 Web 应用程序容器启动时tomcat,类就会被初始化gretty。在加载时(在 中static block),此类检查系统属性的值java.util.logging.manager并创建Logger相应的值。一旦初始化,此类就不会再次初始化。

\n\n

因此,对于 Web 应用程序来说,通过 Web 应用程序代码设置此系统属性为时已晚。

\n\n

一种可能的解决方案是将此系统属性值传递VM arguments到应用程序容器 -

\n\n
-Djava.util.logging.manager="org.apache.logging.log4j.jul.LogManager"\n
Run Code Online (Sandbox Code Playgroud)\n\n

在这种情况下,您必须log4j在启动容器时提供jar和配置文件,以便org.apache.logging.log4j.ju\xe2\x80\x8c\xe2\x80\x8bl.LogManager可以通过System ClassLoader.

\n\n

对于 tomcat,您必须加载以下 3 个 jar bootstrap.jar(tomcat 启动)、tomcat-juli.jar(日志记录)才能使其工作 -

\n\n
log4j-jul\nlog4j-api\nlog4j-core\n
Run Code Online (Sandbox Code Playgroud)\n\n

类似的方法也需要用于其他容器。

\n