How do I configure log4j to send log events to java.util.logging using JULAppender?

自作多情 提交于 2019-12-02 02:19:18

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 add the following 3 jars to your classpath (in addition to log4j-api-2.0.jar):

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!