Log4j如何停止记录器打印到控制台? [英] Log4j How do I stop my logger from printing to the console?

查看:118
本文介绍了Log4j如何停止记录器打印到控制台?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,所以我试图制作一个内存附加器(简单来说就是一个记录器,它记录到ArrayList而不是控制台或文件上),但现在我想使其不能打印到控制台上.

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)..

它包含了我想要实现的所有细分,但是我仍然有些困惑.

It has all the segments on what I'm trying to achieve, but I'm still kind of confused.

我还从 Logback或Log4j可加性说明中阅读了此部分哪个状态..

但是如果记录器X的aditivity标志设置为false或被禁用,则调用x.debug()只会记录到该文件.

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文件

So in theory my log4j.properties file

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_APPENDERrootLoggerconsole中排除其他任何内容.

Should only print ***Hello World and exclude anything else from the MEMORY_APPENDER, rootLogger and 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<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;
}
}

但这不是...


(来源: iforce.co.nz )


(source: iforce.co.nz)

推荐答案

MEMORY_APPENDER=false

将不起作用,您不能将附加器的值设置为false.

will not work, you cannot set an appender to have the value false.

在这种情况下,最好执行以下操作:

In you case better do something like this:

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这样的软件包部分.

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 the log4j.properties or just the package part like nz.ac.massey.cs.sdc.log4jassignment.

似乎您在这里混了很多. Log4jMemoryAppender是不是您的MEMORY_APPENDER?

It seems like you are mixing a lot here. Is Log4jMemoryAppender your MEMORY_APPENDER or not?

为什么要呼叫BasicConfigurator.configure()?您不想使用log4j.properties吗?

And why are you calling BasicConfigurator.configure()? Don't you want to use the 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);
    ...
}

记录器名称将是完全限定的类名称com.mycompany.MyClass.

The logger name will be the fully qualified classname 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 = <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

现在编译,并确保在运行时将log4j.propertieslog4j.jar和您自己的类一起放在您的类路径中.

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

您将看到以下内容:

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

您可以在此处尝试添加文件追加器或您自己的内存追加器.

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

这篇关于Log4j如何停止记录器打印到控制台?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆