如何配置log4j使用JULAppender发送日志事件到java.util.logging?[英] How do I configure log4j to send log events to java.util.logging using JULAppender?

本文是小编为大家收集整理的关于如何配置log4j使用JULAppender发送日志事件到java.util.logging?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我熟悉java.util.logging(Jul)框架,我广泛使用它.最近,我开始使用一个通过Log4J进行登录的库.当我启动应用程序时,我现在在控制台上打印以下内容:

log4j:WARN No appenders could be found for logger (com.example.thirdparty.Library).
log4j:WARN Please initialize the log4j system properly.

看来log4j有一个解决方案: julappender 它将以log4j记录的所有内容发送到我使用的记录框架.

我找不到任何示例向我展示如何配置log4j使用此appender.

推荐答案

配置log4j的标准方法是在类路径的根部创建 log4j.xml .这是为Julappender配置的该文件的内容:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="jul" class="org.apache.log4j.JulAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d %-5p %c - %m%n "/> 
        </layout> 
    </appender> 
    <root> 
        <priority value="all" /> 
        <appender-ref ref="jul" /> 
    </root>  
</log4j:configuration>

其他推荐答案

julappender (btw,可以下载在这里),现在很旧(从2008年起),目标是JDK 1.4和Log4J版本1.2.15 .

If you work with log4j version 2.0 (and above), an easy solution would be to have all messages logged with log4j's slf4j appender ,又,它将其设置为使用java.util.logging作为其基础实现.

如果您使用Maven,请简单地将以下内容包含在您的<dependencies>段落中:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.7</version>
</dependency>

如果您在项目中不使用Maven,请(从Maven网站上下载),并在您的类路径中包含以下3罐以获得相同的效果(除log4j-api-2.0.jar外):

  • log4j-to-slf4j-2.0.jar
  • 1slf4j-jdk14-1.7.7.jar
  • slf4j-api-1.7.7.jar

其他推荐答案

log4j2-3282 提出了(tbd)log4j-to-jul桥的创建.

本文地址:https://www.itbaoku.cn/post/1574842.html

问题描述

I am familiar with the java.util.logging (JUL) framework, I use it extensively. Recently, I started using a library that does its logging through log4j. When I start my application I now get the following printed on the console:

log4j:WARN No appenders could be found for logger (com.example.thirdparty.Library).
log4j:WARN Please initialize the log4j system properly.

It appears that log4j has a solution for this: JULAppender which will send everything logged with log4j to the logging framework that I use.

I can't find any examples that show me how to configure log4j to use this appender.

推荐答案

The standard way of configuring log4j is to create log4j.xml in the root of the classpath. Here are contents of that file configured for JULAppender:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="jul" class="org.apache.log4j.JulAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" value="%d %-5p %c - %m%n "/> 
        </layout> 
    </appender> 
    <root> 
        <priority value="all" /> 
        <appender-ref ref="jul" /> 
    </root>  
</log4j:configuration>

其他推荐答案

The implementation of JULAppender (which, btw, is available for download here), is very old now (from 2008), and targets JDK 1.4 and log4j version 1.2.15.

If you work with log4j version 2.0 (and above), an easy solution would be to have all messages logged with log4j's SLF4J appender, which, in turn, is set to use java.util.logging as its underlying implementation.

If you use maven simply include the following in your <dependencies> paragraph:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-to-slf4j</artifactId>
    <version>2.0</version>
</dependency>
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-jdk14</artifactId>
    <version>1.7.7</version>
</dependency>

If you don't use maven in your project, just download (from maven website) and include the following 3 jars in your CLASSPATH to get the same effect (in addition to log4j-api-2.0.jar):

  • log4j-to-slf4j-2.0.jar
  • 1slf4j-jdk14-1.7.7.jar
  • slf4j-api-1.7.7.jar

其他推荐答案

https://issues.apache.org/jira/browse/LOG4J2-3282 proposes the creation of a (TBD) log4j-to-jul Bridge.