问题描述
我正在尝试使用Java中的log4j捕获日志.可执行文件是在Linux Envirnment中,它显示了日志记录消息,但是,它没有写入日志文件.我正在使用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文件放在应用程序的类Path上,Log4j会发现自动配置自身时第一次使用log4j.
.但是,通过调用BasicConfigurator.configure(),您正在删除XML文件完成的任何配置 - 因为这仅将控制台Appender设置为根记录器.
使用-Dlog4j.debug运行您的Java程序以使Log4j输出一堆有关其在启动时在哪里寻找配置文件的信息.
其他推荐答案
1)在Windows Env上, 即使所有参数都是正确的,也需要确保文件的路径是绝对的,并且由"/"而不是正常的" \"分开.
在确定路径时," \"被忽略,因此成为一个大名.
因此,看来没有写任何东西,实际上是因为它被写在另一个地方. :)
2)和Eclipse,以尝试以" matt b"的上一个答案建议的选项.
转到运行>运行配置>参数[TAB],然后将VM参数>应用和运行中的值放入. 显然您的文件应该具有主.
问题描述
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.
其他推荐答案
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 "\" .
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..
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..