简体   繁体   English

将 Log4j.properties 放在 Eclipse 项目中的正确位置在哪里?

[英]Where is the correct location to put Log4j.properties in an Eclipse project?

我应该在我的 Eclipse 项目中的哪个位置添加log4j.properties文件,以便它按预期工作?

you can add it any where you want, when you run your project, configure the classpath and add the location of the log4j.properties files by clicking on: Run->Run Configuration -> [classpath tab] -> click on user Entries -> Advanced -> Select Add Folder -> select the location of your log4j.properties file 您可以在任何需要的地方添加它,在运行项目时,配置类路径并通过单击添加log4j.properties文件的位置:运行 - >运行配置 - > [类路径选项卡] - >单击用户条目 - >高级 - >选择添加文件夹 - >选择log4j.properties文件的位置

and then -> OK -> run 然后 - >确定 - >运行

and it should get loaded 它应该加载

The safest way IMO is to point at the file in your run/debug config IMO最安全的方式是指向运行/调试配置中的文件

-Dlog4j.configuration=file:mylogging.properties

! Be aware: when using the eclipse launch configurations the specification of the file: protocol is mandatory. 请注意:使用eclipse启动配置时, file:协议的规范是必需的。

In this way the logger will not catch any logging.properties that come before in the classpath nor the default one in the JDK. 这样,记录器就不会捕获以前在类路径中出现的任何logging.properties,也不会捕获JDK中的缺省值。

Also, consider actually use the log4j.xml which has a richer expression syntax and will allow more things (log4j.xml tahe precedence over log4j.properties. 另外,考虑实际使用log4j.xml,它具有更丰富的表达式语法,并允许更多的东西(log4j.xml优先于log4j.properties。

Add the log4j.properties file to the runtime class path of the project. 将log4j.properties文件添加到项目的运行时类路径中。 Some people add this to the root of the source tree (so that it gets copied to the root of the compiled classes). 有些人将此添加到源树的根目录(以便将其复制到已编译类的根目录)。

Edit: If your project is a maven project, you can put the log4j.properties in the src/main/resources folder (and the src/test/resources for your unit tests). 编辑:如果您的项目是maven项目,您可以将log4j.properties放在src / main / resources文件夹中(以及用于单元测试的src / test / resources)。

If you have multiple environments (for example development and production), want different logging for each environment, and want to deploy the same jar (or war, or ear) file to each environment (as in one build for all environments) then store the log4j.properties file outside of the jar file and put it in the class path for each environment (configurable by environment). 如果您有多个环境(例如开发和生产),需要为每个环境进行不同的日志记录,并希望将相同的jar(或war或ear)文件部署到每个环境(如同所有环境的一个构建中),然后存储log4j.properties文件位于jar文件之外,并将其放在每个环境的类路径中(可由环境配置)。 Historically, I would include some known directory in each environment in the classpath and deploy environment specific stuff there. 从历史上看,我会在类路径中的每个环境中包含一些已知目录,并在那里部署特定于环境的东西。 For example, ~tomcat_user/localclasspath where ~tomcat_user is the home directory of the user that will be running the tomcat instance to which my war file will be deployed. 例如,~tomcat_user / localclasspath其中~tomcat_user是将运行我的war文件将部署到的tomcat实例的用户的主目录。

The best way is to create special source folder named resources and use it for all resource including log4j.properties. 最好的方法是创建名为resources特殊源文件夹,并将其用于包括log4j.properties在内的所有资源。 So, just put it there. 所以,就把它放在那里。

On the Java Resources folder that was automatically created by the Dynamic Web Project, right click and add a new Source Folder and name it 'resources'. 在动态Web项目自动创建的Java Resources文件夹中,右键单击并添加新的源文件夹并将其命名为“resources”。 Files here will then be exported to the war file to the classes directory 然后,此处的文件将导出到war文件中的classes目录

If you have a library and you want to append the log4j: 如果您有一个库,并且想要附加log4j:

  1. Create a folder named "resources" in your projet. 在项目中创建名为“resources”的文件夹。
  2. Create a .properties file named log4j 创建名为log4j的.properties文件
  3. Set properties in log4j.properties file 在log4j.properties文件中设置属性
  4. Push right button in the project and go to properties->Java Build Path and, finally, go to the "Source" tab. 在项目中按右键,转到属性 - > Java构建路径,最后转到“源”选项卡。
  5. Push Add folder and search the "resources" folder created in step 1. 按“添加”文件夹并搜索在步骤1中创建的“资源”文件夹。
  6. Finish. 完。

(I have assumed that you have the log4j library added.) (我假设你已经添加了log4j库。)

PD: Sorry for my english. PD:抱歉我的英文。

This question is already answered here 这个问题已在这里得到解答

The classpath never includes specific files. 类路径从不包含特定文件。 It includes directories and jar files. 它包括目录和jar文件。 So, put that file in a directory that is in your classpath . 因此,将该文件放在classpath中的目录中。

Log4j properties aren't (normally) used in developing apps (unless you're debugging Eclipse itself!). Log4j属性不是(通常)用于开发应用程序(除非您正在调试Eclipse本身!)。 So what you really want to to build the executable Java app (Application, WAR, EAR or whatever) and include the Log4j properties in the runtime classpath. 那么您真正想要构建可执行Java应用程序(Application,WAR,EAR或其他)并在运行时类路径中包含Log4j属性。

Put log4j.properties in the runtime classpath. log4j.properties放在运行时类路径中。

This forum shows some posts about possible ways to do it. 论坛显示了一些有关可能的方法的帖子。

In general I put it in a special folder "res" or "resources as already said, but after for the web application, I copy the log4j.properties with the ant task to the WEB-INF/classes directory. It is the same like letting the file at the root of the src/ folder but generally I prefer to see it in a dedicated folder. 一般情况下,我把它放在一个特殊的文件夹“res”或“资源已经说过,但是在web应用程序之后,我将带有ant任务的log4j.properties复制到WEB-INF / classes目录。就像是一样的让文件位于src /文件夹的根目录但通常我更喜欢在专用文件夹中看到它。

With Maven, the usual place to put is in the folder src/main/resources as answered in this other post . 使用Maven,通常放置的文件位于src/main/resources文件夹中,如另一篇文章所述 All resources there will go to your build in the root classpath (eg target/classes/ ) 那里的所有资源都将在根类路径中进行构建(例如target/classes/

If you want a powerful logger, you can have also a look to slf4j library which is a logger facade and can use the log4j implementation behind. 如果你想要一个强大的记录器,你也可以看看slf4j库 ,它是一个记录器外观,可以使用后面的log4j实现。

I'm finding out that the location of the log4j.properties file depends on the type of Eclipse project. 我发现log4j.properties文件的位置取决于Eclipse项目的类型。

Specifically, for an Eclipse Dynamic Web Project, most of the answers that involve adding the log4j.properties to the war file do not actually add the properties file in the correct location, especially for Tomcat/Apache. 具体来说,对于Eclipse Dynamic Web Project,大多数涉及将log4j.properties添加到war文件的答案实际上并未将属性文件添加到正确的位置,尤其是对于Tomcat / Apache。

Here is some of my research and my solution to the issue (again specifically for a Dynamic Web Project running on Tomcat/Apache 6.0) 以下是我的一些研究和我对该问题的解决方案(同样专门针对在Tomcat / Apache 6.0上运行的Dynamic Web Project)

  • Please refer to this article around how Tomcat will load classes. 关于Tomcat如何加载类,请参阅这篇文章。 It's different than the normal class loader for Java. 它与Java的普通类加载器不同。 ( https://www.mulesoft.com/tcat/tomcat-classpath ) Note that it only looks in two places in the war file, WEB-INF/classes and WEB-INF/lib. https://www.mulesoft.com/tcat/tomcat-classpath )请注意,它只能查看war文件中的两个位置,WEB-INF / classes和WEB-INF / lib。

  • Note that with a Dynamic Web Project, it is not wise to store your .properties file in the build/../classes directory, as this directory is wiped whenever you clean-build your project. 请注意,对于动态Web项目,将.properties文件存储在build /../ classes目录中是不明智的,因为每当您清理构建项目时都会擦除此目录。

  • Tomcat does not handle .property files in the WEB-INF/lib location. Tomcat不处理WEB-INF / lib位置中的.property文件。

  • You cannot store the log4j.properties file in the src directory, as Eclipse abstracts that directory away from your view. 您无法将log4j.properties文件存储在src目录中,因为Eclipse会将该目录从您的视图中抽象出来。

  • The one way I have found to resolve this is to alter the build and add an additional directory that will eventually load into the WEB-INF/classes directory in the war file. 我发现解决此问题的一种方法是更改​​构建并添加一个最终将加载到war文件中的WEB-INF / classes目录中的其他目录。 Specifically.... 特别....

(1) Right click your project in the project explorer, select 'New'->'Folder'. (1)在项目浏览器中右键单击您的项目,选择“新建” - >“文件夹”。 You can name the folder anything, but the standard in this case is 'resources'. 您可以将文件夹命名为任何名称,但本例中的标准是“资源”。 The new folder should appear at the root level of your project. 新文件夹应出现在项目的根级别。

(2) Move the log4j.properties file into this new folder. (2)将log4j.properties文件移动到这个新文件夹中。

(3) Right click the project again, and select 'Build-Path'->'Configure Build Path'. (3)再次右键单击该项目,然后选择“Build-Path” - >“Configure Build Path”。 Select the 'Sources' tab. 选择“来源”标签。 Click the 'Add Folder' button. 单击“添加文件夹”按钮。 Browse to find your new folder you created in step (1) above. 浏览以找到您在上面步骤(1)中创建的新文件夹。 Select 'OK'. 选择“确定”。

(4) Once back to the eclipse Project Explorer view, note that the folder has now moved to the 'Java Resources' area (ie it's no longer at the root due to eclipse presentation abstraction). (4)回到eclipse Project Explorer视图后,请注意该文件夹现在已移至“Java Resources”区域(即由于eclipse表示抽象而不再位于根目录下)。

(5) Clean build your project. (5)清理构建项目。

(6) To validate that the .properties file now exists in WEB-INF/classes in your war file, export a war file to an easy location (right click Project -> Export -> War file) and checkout the contents. (6)要验证.properties文件现在存在于war文件的WEB-INF / classes中,请将war文件导出到一个简单的位置(右键单击Project - > Export - > War file)并签出内容。 Note that the log4j.properties file now appears in the WEB-INF/classes. 请注意,log4j.properties文件现在出现在WEB-INF / classes中。

(7) Promote your project to Tomcat/Apache and note that log4j now works. (7)将您的项目推广到Tomcat / Apache并注意log4j现在可以正常工作。

Now that log4j works, start logging, solve world problems, take some time off, and enjoy a tasty adult beverage. 现在log4j工作,开始记录,解决世界问题,休息一下,享受美味的成人饮料。

You do not want to have the log4j.properties packaged with your project deployable -- that is a bad idea, as other posters have mentioned. 您不希望将log4j.properties与您的项目一起打包 - 这是一个坏主意,正如其他海报所提到的那样。

Find the root Tomcat installation that Eclipse is pointing to when it runs your application, and add the log4j.properties file in the proper place there. 找到Eclipse在运行应用程序时指向的根Tomcat安装,并将log4j.properties文件添加到适当的位置。 For Tomcat 7, the right place is 对于Tomcat 7来说,正确的地方是

${TOMCAT_HOME}/lib $ {} TOMCAT_HOME / lib目录

For a normal (non maven and non web) java project in eclipse.对于 eclipse 中的普通(非 maven 和非 web)java 项目。

  1. Create a "source" folder with new options and with any name (as a standard we can name it as resources) under the project directory在项目目录下创建一个带有新选项和任何名称(作为标准,我们可以将其命名为资源)的“源”文件夹
  2. push the log4j.properties file to this "source" folder.将 log4j.properties 文件推送到这个“源”文件夹。
  3. Build and run the application.构建并运行应用程序。

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

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