日志文件未在Java中使用log4j更新/创建 [英] Log file not being updated/created using log4j in java

查看:73
本文介绍了日志文件未在Java中使用log4j更新/创建的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Java中的log4j捕获日志. executeable在Linux环境中,它显示日志消息,但是,它没有写入日志文件.我正在使用log4j.xml,这是我到目前为止所拥有的

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="LocalFileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File"   value="logs/error.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="500KB" />
    <param name="maxBackupIndex" value="1" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d %-5p [%c] - %m%n"/>
      </layout>
  </appender>

  <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.SimpleLayout"/>
  </appender>

  <root>
    <priority value ="Debug" />
    <appender-ref ref="console" /> 
  </root>
</log4j:configuration>

我不清楚java程序如何知道xml文件的位置.我认为这就是问题所在.而且,这就是我在代码中设置的方式,

log = Logger.getLogger(MyClass.class);

BasicConfigurator.configure();

任何帮助都会令您感激.谢谢.

解决方案

您可以将log4j.xml文件放在应用程序的类路径上,并且log4j会在您第一次使用log4j时自动发现它来进行自我配置. /p>

但是,通过调用BasicConfigurator.configure(),您正在撤消XML文件所做的任何配置-因为这将仅将控制台附加程序设置为root记录器.

使用-Dlog4j.debug运行Java程序,以使log4j在启动时输出一堆有关其在哪里寻找配置文件的信息.

I am trying to capture the logs using log4j in Java. The executeable is in linux envirnment and it displays the logging message, however, it is not writing into the log file. I am using log4j.xml and this is what I have so far

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

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

  <appender name="LocalFileAppender" class="org.apache.log4j.RollingFileAppender">
    <param name="File"   value="logs/error.log" />
    <param name="Append" value="true" />
    <param name="MaxFileSize" value="500KB" />
    <param name="maxBackupIndex" value="1" />
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%d %-5p [%c] - %m%n"/>
      </layout>
  </appender>

  <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.SimpleLayout"/>
  </appender>

  <root>
    <priority value ="Debug" />
    <appender-ref ref="console" /> 
  </root>
</log4j:configuration>

I am not clear how the java program knows where the xml file is located. I think that is the problem. And, this is how I setup in the code,

log = Logger.getLogger(MyClass.class);

BasicConfigurator.configure();

Any help would be appreicate it. Thanks.

解决方案

You can place the log4j.xml file on the classpath of your application and log4j will find it automatically to configure itself with the very first time you use log4j.

However, by calling BasicConfigurator.configure(), you are undoing any configuration done by the XML file - as this sets up a console appender to the root logger only.

Run your java program with -Dlog4j.debug to have log4j output a bunch of information about where it is looking for the configuration files when it starts up.

这篇关于日志文件未在Java中使用log4j更新/创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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