Java 应用程序:让 Log4j 在 Eclipse 环境中工作
我已尽力设置 Eclipse 和我的 Java 应用程序以使用 log4j.properties
文件。但是,它似乎没有使用属性文件,我不知道为什么。
库:slf4j-api-1.6.1、slf4j-jdk14-1.6.1
在应用程序中,日志记录工作正常。我能够将信息、警告和错误打印到 Eclipse 控制台中。
我希望能够做的是更改日志级别以进行调试并将所有日志消息打印到控制台和日志文件。
我创建了一个 log4j.properties
文件,如下所示:
log4j.rootLogger=DEBUG,console,file
log4j.rootCategory=DEBUG, R, O
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=5
log4j.appender.file.File=checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
我的目录结构如下所示:
My Project
--src/
----MYProject/
------*.java
--bin/
----MYProject/
------*.class
--log4j/
----log4j.properties
在 Eclipse 中,我是这样的:
运行配置 ->类路径(选项卡)->,右键单击用户条目 ->添加“log4j”作为新文件夹并保存。
然后在我的代码中,我像这样调用记录器(示例代码来演示我的方法,因此它可能存在语法错误):
package MYProject;
import org.slf4j.LoggerFactory;
public class MyClass{
final org.slf4j.Logger test_logger = LoggerFactory.getLogger(MyClass.class);
public MyClass(){}
public someMethod(){
test_logger.debug("Some Debug");
test_logger.info("Some Info");
test_logger.warn("Some Warning");
test_logger.error("An Error");
}
}
然后我调用 someMethod
并打印 INFO
, WARN
、ERROR
到 Eclipse 控制台。它不会打印 DEBUG
并且不会打印到文件。
如果有人对我可能做错的事情提出建议,我将不胜感激。
I've done my best to setup Eclipse and my Java application to use a log4j.properties
file. However, it does not seem to be using the properties file and I'm not sure why.
Libraries: slf4j-api-1.6.1, slf4j-jdk14-1.6.1
Within the application the logging works fine. I am able to print info, warnings, and errors into the Eclipse console.
What I would like to be able to do is change the log level to debug and print all logging messages to both the console and a log file.
I have created a log4j.properties
file that looks like this:
log4j.rootLogger=DEBUG,console,file
log4j.rootCategory=DEBUG, R, O
# Stdout
log4j.appender.O=org.apache.log4j.ConsoleAppender
# File
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=log4j.log
# Control the maximum log file size
log4j.appender.R.MaxFileSize=100KB
# Archive log files (one backup file here)
log4j.appender.R.MaxBackupIndex=5
log4j.appender.file.File=checkLog.log
log4j.appender.file.threshold=DEBUG
log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.O.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
log4j.appender.O.layout.ConversionPattern=[%d{ISO8601}]%5p%6.6r[%t]%x - %C.%M(%F:%L) - %m%n
My directory structure looks like this:
My Project
--src/
----MYProject/
------*.java
--bin/
----MYProject/
------*.class
--log4j/
----log4j.properties
In Eclipse I this this:
Run Configurations -> Classpath (tab) ->, right clicked on User Entries -> Added "log4j" as a new folder, and saved.
Then in my code I call the logger like this (sample code to demonstrate my approach so it may have syntax errors):
package MYProject;
import org.slf4j.LoggerFactory;
public class MyClass{
final org.slf4j.Logger test_logger = LoggerFactory.getLogger(MyClass.class);
public MyClass(){}
public someMethod(){
test_logger.debug("Some Debug");
test_logger.info("Some Info");
test_logger.warn("Some Warning");
test_logger.error("An Error");
}
}
I then call someMethod
and it prints INFO
, WARN
, ERROR
to the Eclipse console. It won't print DEBUG
and won't print to a file.
I'd appreciate any suggestions on what I may be doing wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
类路径中的
log4j.properties
之前可能还有另一个log4j.properties
或log4j.xml
文件。打开项目的运行配置,然后添加-Dlog4j.debug=true
作为项目的 VM 参数。这将指示 log4j 在控制台上打印许多附加信息,包括它正在使用的配置文件。There may be another
log4j.properties
orlog4j.xml
file in the classpath ahead of yourlog4j.properties
. Open the run configuration for your project, and add-Dlog4j.debug=true
as a VM Argument for your project. This will instruct log4j to print a lot of additional information on the console, including the config file that it is using.如果您使用日志外观 slf4j,那么您需要通过包含该后端相应的 jar 文件来准确指定一个 日志后端。在您的情况下,您已经在类路径上安装了 slf4j-jdk14-xxxjar ,这只是一个通用的记录器后端。
为了使用 log4j 后端,您需要删除
slf4j-jdk14-xxxjar
并将其替换为slf4j-log4j12-xxxjar
。如果你不删除它,slf4j 必须只选择一个后端 jar,而且可能不是你想要的。当然,您还需要类路径中的实际
log4j-xxxjar
文件。一旦这些 jar 正确就位,那么
-Dlog4j.debug
的 VM 参数将真正起作用,并且可用于调试日志记录配置的来源。If you are using the logging façade slf4j, then you need to specify exactly one logging backend by including the corresponding jar file for that backend. In your case, you have installed
slf4j-jdk14-x.x.x.jar
on your classpath, which is just a generic logger backend.In order to use the log4j backend, you need to remove
slf4j-jdk14-x.x.x.jar
and replace it withslf4j-log4j12-x.x.x.jar
. If you don't remove it, slf4j must choose only one backend jar, and probably not the one you want.Of course you will also need the actual
log4j-x.x.x.jar
file on your classpath too.Once these jars are properly in place, then the VM parameter of
-Dlog4j.debug
will actually work and be useful in debugging where your logging configs are coming from.您需要告诉您的代码使用属性文件。在完成任何日志记录之前,请输入
You need to tell your code to use the properties file. Before any logging is done please put
您的目标目录中可能有旧版本。
清理项目并重试。
除此之外,如果您没有通过 eclipse 添加 log4j.properties,则应该确保刷新 eclipse 项目。
You might have an old version in your target directory.
Clean the project and try again.
Apart from that you should make sure that you refresh the eclipse project if you didn't add the log4j.properties via eclipse.
在路径项目属性/库选项卡中删除项目的jre(JRE系统库)并重新设置jre!
delete jre(JRE System Library) of project in path project properties/library tab and set jre again!