简体   繁体   English

Log4j:相对于类路径的FileAppender

[英]Log4j: FileAppender on file relative to classpath

I managed to properly put a configuration file for log4j and direct to it with this: 我设法正确地为log4j放置了一个配置文件,并使用此文件直接定向到它:

URL logProperties =  Program.class.getResource("log.properties");
PropertyConfigurator.configure(logProperties.getFile());

( I think it can do simpler, I read something about the default properties file, but is not the actual topic) Question is: property file is loaded from the class path of the calling method, which is fine, but I have in the config something like: (我认为它可以做得更简单,我阅读了有关默认属性文件的内容,但不是实际的主题)问题是:属性文件是从调用方法的类路径中加载的,这很好,但是我在配置文件中就像是:

log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=console.log

I was wondering 'console.log' was in the classpath too, but unfortunately it seems to be in the current directory of the executable ( Launching from eclipse, is in the directory parent of the /bin folder). 我想知道“ console.log”也位于类路径中,但不幸的是它似乎位于可执行文件的当前目录中(从eclipse启动,位于/ bin文件夹的父目录中)。 Is there some way to specify a file in the classpath, of course without specifying the full path as log4net does? 有没有某种方法可以在类路径中指定文件,而无需像log4net那样指定完整路径? EDIT by this SO question What is the .NET equivalent of java's System.getProperty("user.dir")? 通过这个SO问题进行编辑 什么是Java的System.getProperty(“ user.dir”).NET等效项? I learn the equivalence of System.getProperty("user.dir") and the .NET 我了解到System.getProperty("user.dir")和.NET的等效性

Assembly.GetExecutingAssembly().Location

unfortunately it seems to me that writing ${user.dir}/console.log does not make any differebce ( ie log seems to stay in the current directory, instead of near the executable. 不幸的是,在我看来,编写$ {user.dir} /console.log不会造成任何区别(即,日志似乎保留在当前目录中,而不是在可执行文件附近。

The entry 入口

log4j.appender.R.File=console.log

defines the output of your logging statements ie where the log statements in your code should go. 定义日志记录语句的输出,即代码中的日志语句应放置的位置。 It makes no sense to put this output file in your classpath. 将此输出文件放在您的类路径中没有任何意义。 Instead (if no other path is specified) the file is written in the working directory (current directory) of your application. 而是(如果未指定其他路径)将文件写入应用程序的工作目录(当前目录)中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM