Log4j How do I stop my logger from printing to the console?(Log4j 如何阻止我的记录器打印到控制台?)
问题描述
好的,所以我正在尝试制作一个内存附加程序(只是一个记录器,它记录到 ArrayList 而不是控制台或文件)但现在我想禁用它,使其无法打印到控制台.
问题和网站,到目前为止我已经阅读(但我仍然无法弄清楚)..
(来源:iforce.co.nz)解决方案行
不起作用,您不能将附加程序设置为 false.
在你的情况下最好做这样的事情:
在您的一个示例中使用的记录器是
<小时>nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender
并且应该映射到log4j.properties 中的记录器
或只是像nz.ac.massey.cs.sdc.log4jassignment
这样的包部分.看来你在这里混了很多.
Log4jMemoryAppender
是你的MEMORY_APPENDER
吗?为什么要调用
<小时>BasicConfigurator.configure()
?你不想使用log4j.properties
吗?通常在课堂上你这样做是为了得到一个记录器:
记录器名称将是完全限定的类名
com.mycompany.MyClass
.然后你可以有一个像这样的
log4j.properties
:<小时>
好的,从头开始.一个非常简单的例子.
src/main/java/Log4JTest.java
src/main/resources/log4j.properties
现在编译并确保
log4j.properties
与log4j.jar
以及运行时您自己的类一起在您的类路径中.你会看到这个:
从这里您可以尝试添加文件附加程序或您自己的内存附加程序.
Okay so I'm trying to make a memory appender (simply a logger, that logs to an ArrayList instead of the console or to a file) but for now I want to disable it from printing to the console.
The questions and websites, I've read so far (but I still cant figure it out is)..
- StackOverFlow Question log4j: Log output of a specific class to a specific appender
- StackOverFlow Question log4j : Disable log4j console logging and enable file logging
- Coder Launch: log4j: stop logging to console
It has all the segments on what I'm trying to achieve, but I'm still kind of confused.
I also read this segment from Logback or Log4j Additivity Explained Which states..
If the aditivity flag of logger X however is set to false, or disabled, then calling x.debug() will only log to the file.
So in theory my
log4j.properties
fileShould only print
***Hello World
and exclude anything else from theMEMORY_APPENDER
,rootLogger
andconsole
.But it doesn't...
(source: iforce.co.nz)解决方案The line
will not work, you cannot set an appender to have the value false.
In you case better do something like this:
The Logger being used in one of your example is
nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender
and that should map to a logger in thelog4j.properties
or just the package part likenz.ac.massey.cs.sdc.log4jassignment
.
It seems like you are mixing a lot here. Is
Log4jMemoryAppender
yourMEMORY_APPENDER
or not?And why are you calling
BasicConfigurator.configure()
? Don't you want to use thelog4j.properties
?
Normally in a class you do this to get a logger:
The logger name will be the fully qualified classname
com.mycompany.MyClass
.Then you can have a
log4j.properties
like this:
OK, starting from beginning. A very simple example.
src/main/java/Log4JTest.java
src/main/resources/log4j.properties
Now compile and make sure that
log4j.properties
is in your classpath together with thelog4j.jar
and your own classes when running.You will see this:
From here you can try to add a file appender or your own memory appender.
这篇关于Log4j 如何阻止我的记录器打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!