简体   繁体   中英

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 ) . The content of my log4j.properties is as follow :

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

    //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 . 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 :

[%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 .

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

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

You need a file named log4j2.properties on your classpath to be picked up automatically.

Documentation from Log4j2:

Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for 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.

  1. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  2. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  3. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  4. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  5. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  6. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  7. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  8. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  9. If no configuration file could be located the DefaultConfiguration will be used. 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

Thanks to DB for pointing out the correct answer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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