简体   繁体   English

无法在Log4j中设置日志记录级别和输出

[英]Unable to Set Logging Level and Output in Log4j

I am unable to set Root Logger Option through properties file in Log4j. 我无法通过Log4j中的属性文件设置Root Logger Option。 Below is how my properties file looks - 以下是我的属性文件的外观-

I have saved this file as log4j2.properties in the root of src folder. 我已将此文件另存为src文件夹根目录中的log4j2.properties。

# Root logger option
log4j.rootLogger=INFO, stdout

# Direct log messages to stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

I tried setting the rootLogger to ALL but, it doesn't work. 我尝试将rootLogger设置为ALL,但是它不起作用。 However; 然而; if I set the Level to ALL through the method in my program it works. 如果我通过程序中的方法将“级别”设置为“全部”,则它将起作用。 Example - 范例-

package com.nanwani;

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

public class LoggerTest {

    public static Logger log = Logger.getLogger(LoggerTest.class);

    public static void main(String[] args) {

        log.setLevel(Level.ALL);

    }

}

In addition, it seems it is not referring to my properties file at all however, if I delete this file then it does show ERROR message that - using default properties file as log4j2 file not found. 另外,似乎它根本不指向我的属性文件,但是,如果我删除此文件,则它确实显示错误消息-使用默认属性文件作为log4j2文件找不到。

I have found the answer to my question. 我找到了我问题的答案。 Log4j2 does not support configuration properties file as opposed to Log4j v1. 与Log4j v1相比,Log4j2不支持配置属性文件。 New formats are XML, JSON, and YAML, see the documentation 新格式为XML,JSON和YAML,请参阅文档

The file log4j2.properties must be named log4j.properties (if you are using Log4j version 1) . 文件log4j2.properties必须命名为log4j.properties(如果使用的是Log4j版本1)。 And you have to log something using log.debug("message"). 而且您必须使用log.debug(“ message”)记录某些内容。 What about considering moving the file to the src/main/resources folder? 考虑将文件移到src / main / resources文件夹怎么办?

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

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