How to initialize log4j properly?

前端 未结 24 3359
太阳男子
太阳男子 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 07:07

    Find a log4j.properties or log4j.xml online that has a root appender, and put it on your classpath.

    ### 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.SimpleLayout
    log4j.rootLogger=debug, stdout
    

    will log to the console. I prefer logging to a file so you can investigate afterwards.

    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.maxFileSize=100KB
    log4j.appender.file.maxBackupIndex=5
    log4j.appender.file.File=test.log
    log4j.appender.file.threshold=debug
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
    log4j.rootLogger=debug,file
    

    although for verbose logging applications 100KB usually needs to be increased to 1MB or 10MB, especially for debug.

    Personally I set up multiple loggers, and set the root logger to warn or error level instead of debug.

提交回复
热议问题