How to initialize log4j properly?

前端 未结 24 3015
太阳男子
太阳男子 2020-11-22 06:49

After adding log4j to my application I get the following output every time I execute my application:

log4j:WARN No appenders could be found for logger (slideselec         


        
相关标签:
24条回答
  • 2020-11-22 06:59

    As explained earlier there are 2 approaches

    First one is to just add this line to your main method:

    BasicConfigurator.configure();
    

    Second approach is to add this standard log4j.properties file to your classpath:

    While taking second approach you need to make sure you initialize the file properly.

    Eg.

    Properties props = new Properties();
    
    props.load(new FileInputStream("log4j property file path"));
    
    props.setProperty("log4j.appender.File.File", "Folder where you want to store log files/" + "File Name");
    

    Make sure you create required folder to store log files.

    0 讨论(0)
  • 2020-11-22 07:00

    You can set up the log level by using setLevel().

    The levels are useful to easily set the kind of informations you want the program to display.

    For example:

    Logger.getRootLogger().setLevel(Level.WARN); //will not show debug messages
    

    The set of possible levels are:

    TRACE,

    DEBUG,

    INFO,

    WARN,

    ERROR and

    FATAL

    According to Logging Services manual

    0 讨论(0)
  • 2020-11-22 07:01

    If you are having this error on Intellij IDEA even after adding the log4j.properties or log4j.xml file on your resources test folder, maybe the Intellij IDEA is not aware yet about the existence of the file.

    So, after add the file, right click on the file and choose Recompile log4j.xml.

    0 讨论(0)
  • 2020-11-22 07:02

    I've created file log4j.properties in resources folder next to hibernate.cfg.xml file and filled it with text below:

    log4j.rootLogger=INFO, CONSOLE
    
    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{ABSOLUTE} %-5p [%c{1}:%L] %m%n
    

    now I got rid of warnings and errors

    0 讨论(0)
  • 2020-11-22 07:04

    What are you developing in? Are you using Apache Tomcat?

    log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
    log4j.appender.CONSOLE.target=System.out
    log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
    log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyyMMdd HH:mm:ss.SSS} [[%5p] %c{1} [%t]] %m%n
    

    I have a properties like this in a Java app of mine.

    0 讨论(0)
  • 2020-11-22 07:05

    Another way to do it without putting the property file on the classpath, is to set the property from the java code directly. Here is the sample code.

    public class Log4JSample {
    
    public static void main(String[] args) {
        Properties properties=new Properties();
        properties.setProperty("log4j.rootLogger","TRACE,stdout,MyFile");
        properties.setProperty("log4j.rootCategory","TRACE");
    
        properties.setProperty("log4j.appender.stdout",     "org.apache.log4j.ConsoleAppender");
        properties.setProperty("log4j.appender.stdout.layout",  "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.stdout.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");
    
        properties.setProperty("log4j.appender.MyFile", "org.apache.log4j.RollingFileAppender");
        properties.setProperty("log4j.appender.MyFile.File", "my_example.log");
        properties.setProperty("log4j.appender.MyFile.MaxFileSize", "100KB");
        properties.setProperty("log4j.appender.MyFile.MaxBackupIndex", "1");
        properties.setProperty("log4j.appender.MyFile.layout",  "org.apache.log4j.PatternLayout");
        properties.setProperty("log4j.appender.MyFile.layout.ConversionPattern","%d{yyyy/MM/dd HH:mm:ss.SSS} [%5p] %t (%F) - %m%n");
    
        PropertyConfigurator.configure(properties);
    
        Logger logger = Logger.getLogger("MyFile");
    
        logger.fatal("This is a FATAL message.");
        logger.error("This is an ERROR message.");
        logger.warn("This is a WARN message.");
        logger.info("This is an INFO message.");
        logger.debug("This is a DEBUG message.");
        logger.trace("This is a TRACE message.");
    }
    

    }

    0 讨论(0)
提交回复
热议问题