简体   繁体   English

仅在使用 log4j 时记录致命和错误消息

[英]Only logging fatal and error messages while using log4j

I am new to log4j and trying to use log4j programmatically instead of using log4j.xml OR log4j.properties file. I am new to log4j and trying to use log4j programmatically instead of using log4j.xml OR log4j.properties file.

But when I am running the code, It is only logging Fatal and Error messages and skipping other messages.但是当我运行代码时,它只记录致命和错误消息并跳过其他消息。

I have tried changing "rootlogger.setLevel(Level.DEBUG);"我试过改变“rootlogger.setLevel(Level.DEBUG);” to "rootlogger.setLevel(Level.All);"到“rootlogger.setLevel(Level.All);” but it is giving same output.但它给出了相同的 output。

Could someone please help here?有人可以在这里帮忙吗?

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;

public class ProgrammaticLog4jExample {
    
    private static final Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class);

    public static void main(String[] args) {
        // creates pattern layout
        String conversionPattern = "%-7p %d [%t] %c %x - %m%n";
        PatternLayout layout = new PatternLayout(conversionPattern);
        
        // creates console appender
        ConsoleAppender consoleAppender = new ConsoleAppender();
        consoleAppender.setLayout(layout);
        consoleAppender.activateOptions();
        
        // configure the root logger
        Logger rootlogger = Logger.getRootLogger();
        rootlogger.setLevel(Level.DEBUG);
        rootlogger.addAppender(consoleAppender);
                
        
        // creates a custom logger and messages
        logger.debug("this is a debug log message");
        logger.info("this is a information message");
        logger.warn("this is a warning message");
        logger.fatal("this is the fatal message");
        logger.error("This is the error message");
    }
}

output: output:

19:07:58.003 [main] FATAL log4jExample.ProgrammaticLog4jExample - this is the fatal message 19:07:58.003 [main] FATAL log4jExample.ProgrammaticLog4jExample - 这是致命的消息

19:07:58.006 [main] ERROR log4jExample.ProgrammaticLog4jExample - This is the error message 19:07:58.006 [main] ERROR log4jExample.ProgrammaticLog4jExample - 这是错误消息

output Image: output 图片:

上述代码的输出

I am using below jar files for log4j in my project classpath:我在我的项目类路径中为 log4j 使用下面的 jar 文件:

项目中使用的 log4j Api

Aha...After doing some more experiments, I have got the answer.啊哈……做了一些实验后,我得到了答案。

Added "logger.setLevel(Level.DEBUG);"添加了“logger.setLevel(Level.DEBUG);” and I am able to get all the logs now.我现在可以获取所有日志。 :-) :-)

import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;

   
public class ProgrammaticLog4jExample {
    
    private static final Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class);

    public static void main(String[] args) {
        // creates pattern layout
        String conversionPattern = "%-7p %d [%t] %c %x - %m%n";
        PatternLayout layout = new PatternLayout(conversionPattern);
        
        // creates console appender
        ConsoleAppender consoleAppender = new ConsoleAppender();
        consoleAppender.setLayout(layout);
        consoleAppender.activateOptions();
        
        // configure the root logger
        Logger rootlogger = Logger.getRootLogger();
        rootlogger.setLevel(Level.DEBUG);
        rootlogger.addAppender(consoleAppender);
                
        logger.setLevel(Level.DEBUG);
        // creates a custom logger and messages
        // Logger logger = Logger.getLogger(ProgrammaticLog4jExample.class.getName());
        logger.debug("this is a debug log message");
        logger.info("this is a information message");
        logger.warn("this is a warning message");
        logger.fatal("this is the fatal message");
        logger.error("This is the error message");
    }
}

Output Image: Output 图片:

输出图像

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM