9

好的,所以我正在尝试制作一个内存附加程序(只是一个记录器,它记录到 ArrayList 而不是控制台或文件)但现在我想禁用它打印到控制台。

到目前为止,我读过的问题和网站(但我仍然无法弄清楚)..

它包含了我想要实现的所有部分,但我仍然有点困惑。

我还从Logback 或 Log4j Additivity Explained which states 中读到了这个片段。

但是,如果 logger X 的 aditivity 标志设置为 false 或禁用,则调用 x.debug() 将仅记录到文件。

所以理论上我的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,rootLoggerconsole.

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<LoggingEvent> eventsList = new ArrayList();

public static void main (String [] args) {

    PropertyConfigurator.configure("Lib/log4j.properties");
    Log4jMemoryAppender app = new Log4jMemoryAppender();
    Logger logger = Logger.getLogger(Log4jMemoryAppender.class);
    logger.setLevel(Level.INFO);

    logger.addAppender(app);
    logger.info("Hello World");
    logger.debug("Level DEBUG Is SET");

    for (LoggingEvent le: app.eventsList) {
        System.out.println("***" + le.getMessage());
    }
} 

@Override
protected void append(LoggingEvent event) {
    eventsList.add(event);
}

public void close() {
}

public boolean requiresLayout() {
    return false;
}
}

但它不...

appender
(来源:iforce.co.nz

4

2 回答 2

5

线

MEMORY_APPENDER=false

将不起作用,您不能将附加程序设置为值为 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

在您的一个示例中使用的 Logger 是nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender并且应该映射到log4j.properties或只是包部分中的记录器,例如nz.ac.massey.cs.sdc.log4jassignment.


看起来你在这里混合了很多。是Log4jMemoryAppender你的MEMORY_APPENDER还是不是?

你为什么打电话BasicConfigurator.configure()?你不想用log4j.properties吗?


Normally in a class you do this to get a logger:

package com.mycompany;

public class MyClass {
    private static final Logger log = Logger.getLogger(MyClass.class);
    ...
}

The logger name will be the fully qualified classname com.mycompany.MyClass.

Then you can have a log4j.properties like this:

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 = <some file appender>

OK, starting from beginning. A very simple example.

src/main/java/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/main/resources/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

Now compile and make sure that log4j.properties is in your classpath together with the log4j.jar and your own classes when running.

You will see this:

0    [main] ERROR org.stackoverflow.Log4JTest  - Error in main

From here you can try to add a file appender or your own memory appender.

于 2012-08-22T06:34:55.350 回答
0

I've just fixed this issue by setting this at log4j.xml, replaced value="TRACE" to value="ERROR". Hope this can help some one :)

    <root>
        <priority value="ERROR" />
        <appender-ref ref="STDOUT"/>
    </root>
于 2020-07-09T01:14:39.290 回答