将 Log4j.properties 放在 Eclipse 项目中的正确位置在哪里?
我应该在 Eclipse 项目中的哪个位置添加 log4j.properties
文件才能使其按预期工作?
Where in my Eclipse project should I add the log4j.properties
file so that it will work as intended?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
您可以将其添加到任何您想要的位置,当您运行项目时,配置类路径并通过单击添加 log4j.properties 文件的位置:
运行->运行配置-> [类路径选项卡]->点击用户条目->高级->选择添加文件夹->选择 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
and then -> OK -> run
and it should get loaded
IMO 最安全的方法是指向运行/调试配置中的文件
!请注意:使用 eclipse 启动配置时,
file:
协议的规范是强制性的。通过这种方式,记录器将不会捕获类路径中之前的任何logging.properties,也不会捕获JDK中的默认日志记录。
另外,考虑实际使用 log4j.xml,它具有更丰富的表达式语法,并且允许更多内容(log4j.xml 优先于 log4j.properties。
The safest way IMO is to point at the file in your run/debug config
! Be aware: when using the eclipse launch configurations the specification of the
file:
protocol is mandatory.In this way the logger will not catch any logging.properties that come before in the classpath nor the default one in the 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.properties文件添加到项目的运行时类路径中。
有些人将其添加到源树的根目录中(以便将其复制到已编译类的根目录中)。
编辑:
如果你的项目是maven项目,
您可以将 log4j.properties 放在 src/main/resources 文件夹中(以及用于单元测试的 src/test/resources 文件夹)。
如果您有多个环境(例如开发和生产),
希望每个环境有不同的日志记录,
并希望将相同的 jar(或 war、ear)文件部署到每个环境
(就像在所有环境的一个构建中一样)
然后将 log4j.properties 文件存储在 jar 文件之外,并将其放入每个环境的类路径中(可按环境配置)。
从历史上看,我会在类路径中的每个环境中包含一些已知的目录,并在那里部署特定于环境的内容。
例如,
~tomcat_user/localclasspath 其中 ~tomcat_user 是将运行 tomcat 实例的用户的主目录,我的 war 文件将部署到该实例。
Add the log4j.properties file to the runtime class path of the project.
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).
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).
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.
最好的方法是创建名为
resources
的特殊源文件夹并将其用于包括 log4j.properties 在内的所有资源。所以,就把它放在那里吧。在动态 Web 项目自动创建的 Java Resources 文件夹上,右键单击并添加一个新的源文件夹并将其命名为“resources”。这里的文件然后会导出成war文件到classes目录下
The best way is to create special source folder named
resources
and use it for all resource including 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'. Files here will then be exported to the war file to the classes directory
如果您有一个库并且想要附加 log4j:
(我假设您添加了 log4j 库。)
PD:抱歉我的英语。
If you have a library and you want to append the log4j:
(I have assumed that you have the log4j library added.)
PD: Sorry for my english.
这个问题已在此处得到解答
类路径从不包含特定文件。它包括目录和 jar 文件。因此,将该文件放入类路径中的目录中。
Log4j
属性(通常)不用于开发应用程序(除非您正在调试 Eclipse 本身!)。那么您真正想要构建可执行的 Java 应用程序(应用程序、WAR、EAR 或其他)并将 Log4j 属性包含在运行时类路径中。This question is already answered here
The classpath never includes specific files. It includes directories and jar files. So, put that file in a directory that is in your
classpath
.Log4j
properties aren't (normally) used in developing apps (unless you're debugging Eclipse itself!). So what you really want to to build the executable Java app (Application, WAR, EAR or whatever) and include theLog4j
properties in the runtime classpath.将
log4j.properties
放入运行时类路径中。这个 论坛 显示了一些有关可能方法的帖子去做它。
Put
log4j.properties
in the runtime classpath.This forum shows some posts about possible ways to do it.
我发现 log4j.properties 文件的位置取决于 Eclipse 项目的类型。
具体来说,对于 Eclipse 动态 Web 项目,大多数涉及将 log4j.properties 添加到 war 文件的答案实际上并未将属性文件添加到正确的位置,特别是对于 Tomcat/Apache。
以下是我的一些研究和我对该问题的解决方案(同样专门针对在 Tomcat/Apache 6.0 上运行的动态 Web 项目)
请参阅这篇有关 Tomcat 如何加载类的文章。它与 Java 的普通类加载器不同。 (https://www.mulesoft.com/tcat/tomcat-classpath) 请注意它只在 war 文件中查找两个位置:WEB-INF/classes 和 WEB-INF/lib。
请注意,对于动态 Web 项目,将 .properties 文件存储在 build/../classes 目录中并不明智,因为每当您清理构建项目时都会擦除该目录。
Tomcat 不处理 WEB-INF/lib 位置中的 .property 文件。
您无法将 log4j.properties 文件存储在 src 目录中,因为 Eclipse 会从您的视图中抽象出该目录。
我发现解决此问题的一种方法是更改构建并添加一个附加目录,该目录最终将加载到 war 文件中的 WEB-INF/classes 目录中。具体来说......
(1) 在项目资源管理器中右键单击您的项目,选择“新建”->“文件夹”。您可以将文件夹命名为任何名称,但本例中的标准是“资源”。新文件夹应出现在项目的根级别。
(2) 将 log4j.properties 文件移动到这个新文件夹中。
(3) 再次右键单击该项目,选择“Build-Path”->“Configure Build Path”。选择“来源”选项卡。单击“添加文件夹”按钮。浏览并找到您在上述步骤 (1) 中创建的新文件夹。选择“确定”。
(4) 返回到 eclipse Project Explorer 视图后,请注意该文件夹现在已移动到“Java Resources”区域(即,由于 eclipse 表示抽象,它不再位于根目录)。
(5) 清理构建您的项目。
(6) 要验证 .properties 文件现在是否存在于您的 war 文件中的 WEB-INF/classes 中,请将 war 文件导出到一个简单的位置(右键单击项目 -> 导出 -> War 文件)并签出内容。请注意,log4j.properties 文件现在出现在 WEB-INF/classes 中。
(7) 将您的项目升级到 Tomcat/Apache,并注意 log4j 现在可以工作了。
现在 log4j 可以工作了,开始记录,解决世界问题,休息一下,享受美味的成人饮料。
I'm finding out that the location of the log4j.properties file depends on the type of Eclipse project.
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.
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)
Please refer to this article around how Tomcat will load classes. It's different than the normal class loader for 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.
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.
Tomcat does not handle .property files in the WEB-INF/lib location.
You cannot store the log4j.properties file in the src directory, as Eclipse abstracts that directory away from your view.
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. Specifically....
(1) Right click your project in the project explorer, select 'New'->'Folder'. 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.
(3) Right click the project again, and select '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. 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).
(5) Clean build your project.
(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. Note that the log4j.properties file now appears in the WEB-INF/classes.
(7) Promote your project to Tomcat/Apache and note that log4j now works.
Now that log4j works, start logging, solve world problems, take some time off, and enjoy a tasty adult beverage.
您不希望将 log4j.properties 与可部署的项目一起打包——正如其他发帖者所提到的那样,这是一个坏主意。
找到 Eclipse 在运行应用程序时所指向的根 Tomcat 安装,并将 log4j.properties 文件添加到那里的适当位置。对于 Tomcat 7,正确的位置是
${TOMCAT_HOME}/lib
You do not want to have the log4j.properties packaged with your project deployable -- that is a bad idea, as other posters have mentioned.
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. For Tomcat 7, the right place is
${TOMCAT_HOME}/lib
一般来说,我将其放在特殊文件夹“res”或“资源”中,如前所述,但在 Web 应用程序之后,我将带有 ant 任务的
log4j.properties
复制到 WEB-INF/classes这与将文件放在 src/ 文件夹的根目录中相同,但通常我更喜欢将其放在专用文件夹中,通常放置在文件夹
src/main/resources 中 。
正如这篇其他帖子中的回答。那里的所有资源都将进入根类路径中的构建(例如
target/classes/
)如果您想要一个强大的记录器,您还可以查看 slf4j 库,它是一个记录器外观,可以使用后面的 log4j 实现。
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.With Maven, the usual place to put is in the folder
src/main/resources
as answered in this other post.All resources there will go to your build in the root classpath (e.g.
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.
对于 Eclipse 中的普通(非 Maven 和非 Web)Java 项目。
For a normal (non maven and non web) java project in eclipse.