JUL适配器不适用于Jersey [英] JUL Adapter not working for Jersey

查看:118
本文介绍了JUL适配器不适用于Jersey的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to use JUL Adapter to delegate Java Util Logging to Log4j2. More precisely, any third-party library that use JUL to generate logs, should be delegated to Log4j2.

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

As a simple exercise, I created a standalone application that uses a library (I created this library for testing purposes, it generates logs using JUL) to test the JUL Adapter. When I change the log manager as described here I can see the effects. And it works fine.

她的代码是:

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

所需的依赖关系:

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'
}

但是,我不能让它在使用Jersey的java web应用程序上运行。 Jersey使用JUL,我正在尝试用Log4j2桥接它。

However, I cannot get this to work on a java web app that uses Jersey. Jersey uses JUL, and I'm trying to bridge it with 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
}

我试过这些选项:


  1. 更改扩展应用程序的类中的日志管理器。

  1. Change the log manager in a class that extends 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);
    }
}


  • 更改类中的日志管理器实现 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.");
        }
    }
    


  • 除上述两个选项外,我还尝试在静态块中设置日志管理器(对于选项1和2)。

  • Apart from two options above, I also tried to set the log manager in a static block (for both options 1. and 2.)

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

    Unfortunately, none of these options worked for me. I am wondering where should this be done. Is there something that I am missing?

    推荐答案

    java.util.logging.LogManager tomcat gretty 等Web应用程序容器启动时,$ c>类被初始化。在加载时(在静态块中),此类检查 java.util.logging.manager 系统属性并相应地创建 Logger 。初始化后,此类永远不会再次初始化。

    java.util.logging.LogManager class gets initialized when web application container like tomcat, gretty gets started. At the time of loading (in static block), this class checks for the value of java.util.logging.manager system property and create the Logger accordingly. Once initialized, this class never gets initialized again.

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

    So, for a web application, setting this system property through web application code would be too late.

    一种可能的解决方案是将此系统属性值通过 VM参数传递给应用程序容器 -

    One possible solution is to pass this system property value through VM arguments to the application container -

    -Djava.util.logging.manager="org.apache.logging.log4j.jul.LogManager"
    

    在这种情况下,你必须在启动容器时提供 log4j jars和配置文件,以便 org.apache.logging.log4j.ju l.LogManager 可以通过 System ClassLoader 加载。

    In this situation, you have to provide log4j jars and configuration file at the time of starting container so that org.apache.logging.log4j.ju‌​l.LogManager can be loaded through System ClassLoader.

    对于tomcat,在3个罐子之后你必须加载 bootstrap.jar (tomcat启动), tomcat -juli.jar (日志)使它工作 -

    For tomcat, following 3 jars you must load along with bootstrap.jar (tomcat startup), tomcat-juli.jar (logging) to make it work -

    log4j-jul
    log4j-api
    log4j-core
    

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

    Similar approach needs to be used for other container also.

    这篇关于JUL适配器不适用于Jersey的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆