Log4j控制台上的警告

时间:2018-03-06 14:22:59

标签: java log4j

我有一个使用log4j的java应用程序。我将log4j.xml文件放在源文件夹中。在运行时,log4j在给定位置创建文件(跟踪,错误)但无法打印日志消息,我在控制台上看到警告。我搜遍了所有的解决方案,我无法解决它。我尝试使用main方法中的BasicConfigurator.configure(),它在控制台上工作并打印日志消息,但我想在文件上打印它们。

log4j:WARN No appenders could be found for logger (com.aafes.pix.Pix).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for 
more info.


Screenshot(01) of the location where I placed my log4j.xml file in the project.
Screenshot(02) of the files that were created on the location.

enter image description here

enter image description here

以下是我的LOG4J.XML文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration>

<appender name="mapps_all" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.all.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="ALL"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
</appender>

<appender name="mapps_trace" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.trace.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="trace"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
</appender>

<appender name="mapps_error" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.error.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="WARN"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="WARN"/> 
        <param name="LevelMax" value="FATAL"/> 
    </filter>     
</appender>

<appender name="mapps_local" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.local.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="LOCAL"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
    <filter class="org.apache.log4j.varia.LevelRangeFilter">
        <param name="LevelMin" value="DEBUG"/> 
        <param name="LevelMax" value="FATAL"/> 
    </filter>     
</appender>
<appender name="mapps_debug" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.debug.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <param name="Threshold" value="DEBUG"/>
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="LevelToMatch" value="DEBUG"/> 
        <param name="AcceptOnMatch" value="true"/> 
    </filter>     
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>  
</appender>

<appender name="mapps_info" class="org.apache.log4j.DailyRollingFileAppender">
    <param name="file" value="/home/tlog/logs/mapps.info.log" />
    <param name="DatePattern" value="'.'yyyy-MM-dd" />
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %X{ipAddress} - %X{messageId}: %m%n" />
        </layout>
    <filter class="org.apache.log4j.varia.LevelMatchFilter">
        <param name="LevelToMatch" value="INFO"/> 
        <param name="AcceptOnMatch" value="true"/> 
    </filter>     
    <filter class="org.apache.log4j.varia.DenyAllFilter"/>  
</appender>

<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
        <param name="ConversionPattern" value="%d{ABSOLUTE} %5p %c{1}:%L - %m%n"/>
    </layout>
</appender>
<logger name="com.aafes.demand">
  <level value ="all"/>
  <appender-ref ref="mapps_trace"/>
  <appender-ref ref="mapps_debug"/>
  <appender-ref ref="mapps_info"/>
  <appender-ref ref="mapps_error"/>
  <appender-ref ref="mapps_all"/>
</logger>


<logger name="org.apache.axis">
   <level value="info"/>
   <appender-ref ref="mapps_trace"/>
</logger>

<root>
  <priority value="all"></priority>
</root>

</log4j:configuration>

以下是主要方法:

package com.aafes.pix;

import javax.xml.parsers.*;

import org.w3c.dom.CharacterData;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import java.io.*;

import org.xml.sax.*;

import java.net.*;
import java.sql.Connection;

import org.apache.log4j.Logger;


public class Pix {

    private static final Logger logger = Logger.getLogger(Pix.class
            .getName());


     // all the reset stuff

}

1 个答案:

答案 0 :(得分:0)

尝试更新root。在那里添加一些appender。

<root>
    <level value="ALL" />
    <appender-ref ref="stdout" />
</root>

我想因为root没有提供appender而且没有名为com.aafes.pix的单独记录器,所以log4j发出了相关信号。

或者你可以创建一个单独的记录器:

<logger name="com.aafes.pix">
   <level value="info"/>
   <appender-ref ref="mapps_trace"/>
</logger>

请注意,这些仅仅是示例 - 因此您应该使用您需要的appender mapps_trace <appender-ref ref="mapps_trace"/>

我会留下root打印到控制台,但只有DEBUG的东西。