让 log4j2 与 Eclipse 一起工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17944905/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting log4j2 to work with eclipse
提问by alexis
I know there is allot of questions been asked on this but i have been trying to get this to work for few day's and i am not any more forward then when i started.
我知道有人对此提出了很多问题,但我一直在努力让它发挥作用,并且在我开始时我不再向前了。
i have tried to use -Dlog4j.configuration=file:/path/to/log4j.properties
and -Dlog4j.debug
in eclipse vm arguments (under debug & run) and get no output
我试图在 Eclipse vm 参数中使用-Dlog4j.configuration=file:/path/to/log4j.properties
和-Dlog4j.debug
(在调试和运行下)并且没有得到任何输出
I have tried to use .properties and .xml but no joy
我曾尝试使用 .properties 和 .xml 但没有乐趣
Tried to put the .xml and .properties files at the root, in the src and in an external folder which i added to my classpath ... still no joy
试图将 .xml 和 .properties 文件放在根目录、src 和我添加到我的类路径中的外部文件夹中......仍然没有乐趣
I think its using another .xml or .properties files in another lib/jar but because i cant get any debug to work i am finding very difficult to track what i am doing wrong here...
我认为它在另一个 lib/jar 中使用另一个 .xml 或 .properties 文件,但是因为我无法进行任何调试,我发现很难跟踪我在这里做错了什么......
any help would be great! below is the code .. only the error message get's printed.
任何帮助都会很棒!下面是代码.. 只打印错误消息。
I have download (http://logging.apache.org/log4j/2.x/download.html) and imported into my app the
log4j-api-2.0-beta8.jar
log4j-core-2.0-beta8
我已经下载(http://logging.apache.org/log4j/2.x/download.html)并导入到我的应用程序中
log4j-api-2.0-beta8.jar
log4j-core-2.0-beta8
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
public class CucmServMonitor
{
private static final Logger logger = LogManager.getLogger(CucmServMonitor.class.getName())
public static void main(String[] args)
{
logger.error("testing ERROR level");
logger.trace("exiting application");
System.out.println(logger.getName());
}
}
the xml file i am using just now log4j2.xml
我刚刚使用的 xml 文件 log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="WARN">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<root level="debug">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>
采纳答案by alexis
Manage to figure this one out. The hint was here.
设法弄清楚这一点。提示就在这里。
I needed to add a "class folder" of where the log4j2.xml was located and then make sure it was at TOP of the list:
我需要添加 log4j2.xml 所在位置的“类文件夹”,然后确保它位于列表的顶部:
Right click on your project and go to properties
右键单击您的项目并转到属性
Then follow the step shown below. After adding the folder make sure its at the top and then click ok
然后按照下图所示的步骤操作。添加文件夹后确保它在顶部,然后单击确定
回答by Mastering_the_Object
Or... just create a resources directory like src/test/resources and add the log4j.xml file to that dir and then make that directory a source folder. Then eclipse will automatically copy the file to the the classes dir and there you have it.
或者...只需创建一个资源目录,如 src/test/resources 并将 log4j.xml 文件添加到该目录,然后将该目录设为源文件夹。然后 eclipse 会自动将文件复制到 classes 目录,然后就可以了。
回答by jet.lau
according to Alexis comment, I setup it, but eclipse still can not find log4j2.xml. finally, I solved it by removing the other jars imported, only keep log4j-api and log4j-core. before I imported all the jars downloaded from the Apache website. hope this can help someone.
根据 Alexis 的评论,我设置了它,但 eclipse 仍然找不到 log4j2.xml。最后,我通过删除导入的其他 jar 来解决它,只保留 log4j-api 和 log4j-core。在我导入从 Apache 网站下载的所有 jar 之前。希望这可以帮助某人。
回答by Kunalan S
1) Create the log4j.properties file inside the root folder
1) 在根文件夹中创建 log4j.properties 文件
log4j.rootCategory=DEBUG, CONSOLE
# Appender writes to console
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
2) Modify the code like this
2)修改代码如下
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class CucmServMonitor {
private static final Logger logger = Logger.getLogger(CucmServMonitor.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
logger.error("testing ERROR level");
logger.trace("exiting application");
System.out.println(logger.getName());
}
}