日志文件未在 java 中使用 log4j 更新/创建

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12466353/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:00:03  来源:igfitidea点击:

Log file not being updated/created using log4j in java

javalog4j

提问by Tony

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

我正在尝试使用 Java 中的 log4j 捕获日志。可执行文件在 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>

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,

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

log = Logger.getLogger(MyClass.class);

BasicConfigurator.configure();

Any help would be appreicate it. Thanks.

任何帮助都会很感激。谢谢。

回答by matt b

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

您可以将log4j.xml文件放在应用程序的类路径中,并且在您第一次使用 log4j 时,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.

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

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

运行你的java程序,-Dlog4j.debug让log4j输出一堆关于它在启动时在哪里寻找配置文件的信息。

回答by Roshan Khandelwal

1)On the Windows env , Even if all the parameters are right , need to make sure that the path to the file is absolute and that is separated by " / " rather than the normal "\" .

1)在 Windows env 上,即使所有参数都正确,也需要确保文件路径是绝对路径,并且用“/”而不是普通的“\”分隔。

While deciding on the path , "\" are ignored and so it becomes one large name .

在决定路径时,“\”被忽略,因此它成为一个大名称。

And hence it might seem that nothing's been written , it actually is that it's getting written at a different place . :)

因此看起来好像什么都没写,实际上是在不同的地方写的。:)

2)And on Eclipse , to try out the option that the previous answer by "matt B" suggests..

2)在 Eclipse 上,尝试“matt B”之前的答案所建议的选项..

Go to RUN > RUN Configurations > Arguments [tab ] and put in the value in the VM arguments > Apply and RUN . Obviously your file should have main..

转到 RUN > RUN Configurations > Arguments [tab] 并在 VM arguments > Apply 和 RUN 中输入值。显然你的文件应该有主..