简体   繁体   English

log4j V2中log4j.properties的内容并格式化记录器输出(例如添加时间戳)

[英]the content of log4j.properties in log4j V2 and formating the logger output (for instance adding time stamp)

I am doing a Java project and I have already integrated the Log4j API Version 2 into my programme (CLearly for the first time and I have no idea how it works , thus if my question seems easy don't put the blame on me ) . 我正在做一个Java项目,我已经将Log4j API Version 2集成到了我的程序中(很显然是第一次,而且我不知道它是如何工作的,因此,如果我的问题似乎很简单,请不要怪我)。 The content of my log4j.properties is as follow : 我的log4j.properties的内容如下:

log4j.rootLogger=DEBUG,SAWAN
log4j.appender.SAWAN=org.apache.log4j.ConsoleAppender
log4j.appender.SAWAN.layout=org.apache.log4j.SimpleLayout

and then I imported the log4j library into my class and for debugging purpose I've written the following and the output is shown as well 然后将log4j库导入到我的类中,出于调试目的,我编写了以下内容,并显示了输出

    //My Code
    PropertyConfigurator.configure("log4j.properties");
    logger.debug("Sample debug message");
    logger.info("Sample info message");
    logger.warn("Sample warn message");
    logger.error("Sample error message");
    logger.fatal("Sample fatal message");


    //Output 
    DEBUG - Sample debug message
    INFO - Sample info message
    WARN - Sample warn message
    ERROR - Sample error message
    FATAL - Sample fatal message

which means that the log4j is working fine . 这意味着log4j运行正常。 How ever I'd like to change the format of the output and add time stamp to it . 我怎么想更改输出的格式并为其添加时间戳。 Based on my research on the other questions asked in this site and referring to https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html I know that I need to use something like : 根据我对本网站提出的其他问题的研究,并参考https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html,我知道我需要使用类似以下内容:

[%t] %-5p %c %x - %m%n

but once I added this to my log4j.properties I recived an error .My question is where should I specify the output format for my log outputs . 但是将其添加到log4j.properties后,我遇到了错误。我的问题是,应在哪里为日志输出指定输出格式。

Here are the libraries I've imported as well : 这也是我导入的库:

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import org.apache.log4j.PatternLayout;

Worth mentioning that I've tried adding the following to my log4j.properties : log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n 值得一提的是,我尝试将以下内容添加到我的log4j.properties中: log4j.appender.ConsoleAppender.layout.ConversionPattern=[%-5p] %d %c - %m%n

yet I am keep getting errors that some thing is wrong with my log4j.properties which is not the case when I remove it and it would work fine :) 但是我不断收到错误消息,说我的log4j.properties出了点问题,当我删除它时不是这种情况,它可以正常工作:)

You need a file named log4j2.properties on your classpath to be picked up automatically. 您需要在类路径上有一个名为log4j2.properties的文件,以自动进行拾取。

Documentation from Log4j2: Log4j2中的文档:

Log4j has the ability to automatically configure itself during initialization. Log4j能够在初始化期间自动进行自我配置。 When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. Log4j启动时,它将找到所有ConfigurationFactory插件,并按从高到低的加权顺序排列它们。 As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML. 交付时,Log4j包含四个ConfigurationFactory实现:一个用于JSON,一个用于YAML,一个用于属性,以及一个用于XML。

Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension. Log4j将检查“ log4j.configurationFile”系统属性,如果已设置,将尝试使用与文件扩展名匹配的ConfigurationFactory加载配置。

  1. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath. 如果未设置系统属性,则属性ConfigurationFactory将在类路径中查找log4j2-test.properties。
  2. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath. 如果找不到此类文件,则YAML ConfigurationFactory将在类路径中查找log4j2-test.yaml或log4j2-test.yml。
  3. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath. 如果找不到此类文件,则JSON ConfigurationFactory将在类路径中查找log4j2-test.json或log4j2-test.jsn。
  4. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath. 如果找不到这样的文件,XML ConfigurationFactory将在类路径中查找log4j2-test.xml。
  5. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath. 如果找不到测试文件,则属性ConfigurationFactory将在类路径上查找log4j2.properties。
  6. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath. 如果找不到属性文件,则YAML ConfigurationFactory将在类路径上查找log4j2.yaml或log4j2.yml。
  7. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath. 如果无法找到YAML文件,则JSON ConfigurationFactory将在类路径上查找log4j2.json或log4j2.jsn。
  8. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath. 如果无法找到JSON文件,则XML ConfigurationFactory将尝试在类路径上找到log4j2.xml。
  9. If no configuration file could be located the DefaultConfiguration will be used. 如果找不到配置文件,将使用DefaultConfiguration。 This will cause logging output to go to the console. 这将导致日志记录输出进入控制台。

You can find all documentation on log4j2 configuration here: https://logging.apache.org/log4j/2.x/manual/configuration.html 您可以在此处找到有关log4j2配置的所有文档: https ://logging.apache.org/log4j/2.x/manual/configuration.html

Thanks to DB for pointing out the correct answer. 感谢DB指出正确的答案。

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

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