好的,所以我正在尝试创建一个内存appender(只是一个记录器,记录到ArrayList而不是控制台或文件),但是现在我想禁止它打印到控制台.
我到目前为止所读过的问题和网站(但我仍然无法弄明白)..
> 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
它具有我想要实现的所有细分,但我仍然有点困惑.
我还从Logback or Log4j Additivity Explained 开始阅读这段内容.
If the aditivity flag of logger X however is set to false,or disabled,then calling x.debug() will only log to the file.
所以理论上我的log4j.properties文件
log4j.rootLogger=ERROR,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d %5p %c (%F:%L) - %m%n
#hide the Log4jMemoryAppender from console
log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender= ERROR,MEMORY_APPENDER
log4j.appender.MEMORY_APPENDER=nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender.Log4jMemoryAppender
log4j.additivity.rootLogger = false
log4j.additivity.console = false
log4j.additivity.MEMORY_APPENDER=false
应该只打印*** Hello World并从MEMORY_APPENDER,rootLogger和console中排除其他任何内容.
package nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender;
import java.util.ArrayList;
import org.apache.log4j.AppenderSkeleton;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
public class Log4jMemoryAppender extends AppenderSkeleton {
ArrayList
但它不……
MEMORY_APPENDER=false
不起作用,你不能设置一个appender值false.
在你的情况下,最好做这样的事情:
log4j.rootLogger=ERROR,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = ERROR,MEMORY_APPENDER
log4j.additivity.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = false
你的一个例子中使用的记录器是nz.ac.massey.cs.sdc.log4jassignment.s
log4j.rootLogger = ERROR,console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
586.MemoryAppender,它应该映射到log4j.properties中的记录器或者只是像nz.ac.massey.cs这样的包部件.sdc.log4jassignment.
看起来你在这里混合很多. Log4jMemoryAppender是你的MEMORY_APPENDER吗?
你为什么要调用BasicConfigurator.configure()?你不想使用log4j.properties吗?
通常在课堂上你这样做是为了得到一个记录器:
package com.mycompany;
public class MyClass {
private static final Logger log = Logger.getLogger(MyClass.class);
...
}
记录器名称将是完全限定的类名com.mycompany.MyClass.
然后你可以有一个像这样的log4j.properties:
log4j.rootLogger=ERROR,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.logger.com.mycompany=INFO,file
log4j.additivity.com.mycompany=false
logfj.appender.file =
好的,从头开始.一个非常简单的例子.
的src /主/爪哇/ Log4JTest.java
package org.stackoverflow;
import org.apache.log4j.Logger;
/**
* @author maba,2012-08-22
*/
public class Log4JTest {
public static final Logger log = Logger.getLogger(Log4JTest.class);
public static void main(String[] args) {
log.error("Error in main");
}
}
SRC /主/资源/ log4j.properties
log4j.rootLogger = ERROR,console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
现在编译并确保log4j.properties在运行时与log4j.jar和您自己的类一起在您的类路径中.
你会看到这个:
0 [main] ERROR org.stackoverflow.Log4JTest - Error in main