Log4j 如何阻止我的记录器打印到控制台?

2022-01-11 00:00:00 console java log4j

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

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


  • (来源:iforce.co.nz)

    解决方案

    MEMORY_APPENDER=false

    不起作用,您不能将附加程序设置为 false.

    在你的情况下最好做这样的事情:

    log4j.rootLogger=错误,控制台log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.logger.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = 错误,MEMORY_APPENDERlog4j.additivity.nz.ac.massey.cs.sdc.log4jassignment.s06005586.MemoryAppender = 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 {private static final Logger log = Logger.getLogger(MyClass.class);...}

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

    然后你可以有一个像这样的 log4j.properties:

    log4j.rootLogger=错误,控制台log4j.appender.console=org.apache.log4j.ConsoleAppenderlog4j.logger.com.mycompany=信息,文件log4j.additivity.com.mycompany=falselogfj.appender.file = <一些文件追加器>

    <小时>

    好的,从头开始.一个非常简单的例子.

    src/main/java/Log4JTest.java

    包 org.stackoverflow;导入 org.apache.log4j.Logger;/*** @作者马巴,2012-08-22*/公共类 Log4JTest {public static final Logger log = Logger.getLogger(Log4JTest.class);公共静态无效主要(字符串[]参数){log.error("main 中的错误");}}

    src/main/resources/log4j.properties

    log4j.rootLogger = 错误,控制台log4j.appender.console = org.apache.log4j.ConsoleAppenderlog4j.appender.console.layout=org.apache.log4j.PatternLayoutlog4j.appender.console.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

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

    你会看到这个:

    0 [main] 错误 org.stackoverflow.Log4JTest - 主错误

    从这里您可以尝试添加文件附加程序或您自己的内存附加程序.

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

    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;
    }
    }
    

    But it doesn't...


    (source: iforce.co.nz)

    解决方案

    The line

    MEMORY_APPENDER=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
    

    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.


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

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

    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.

相关文章