No appenders could be found for logger(log4j)?

前端 未结 30 2262
[愿得一人]
[愿得一人] 2020-11-22 08:16

I have put log4j to my buildpath, but I get the following message when I run my application:

log4j:WARN No appenders could be found for logger (dao.hsqlmanag         


        
相关标签:
30条回答
  • 2020-11-22 08:48

    Add the following as the first code:

    Properties prop = new Properties();
    prop.setProperty("log4j.rootLogger", "WARN");
    PropertyConfigurator.configure(prop);
    
    0 讨论(0)
  • 2020-11-22 08:48

    Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:

    1. Run

    2. Run Configurations

    3. Classpath (tab)

    4. User Entries

    5. add jar on the right

    6. add log4j jar file

    7. Apply

    8. Run

    The error message should no longer appear.

    0 讨论(0)
  • 2020-11-22 08:51

    I faced the same issue when I tried to run the JUnit test class.

    The issue is resolved after I manually added the log4j.properties file in the src/test/resources folder.

    Adding the below code to the log4j.properties file solved the issue:

    # Root logger option
    log4j.rootLogger=INFO, file, stdout
    
    # Direct log messages to a log file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.File=C:\\logging.log
    log4j.appender.file.MaxFileSize=10MB
    log4j.appender.file.MaxBackupIndex=10
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    
    # 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
    
    0 讨论(0)
  • 2020-11-22 08:51

    My Eclipse installation could not find log4j.properties when running JUnit tests from Eclipse, even though the file was located at src/test/resources.

    The reason was that Eclipse (or the m2e connector) did not copy content from src/test/resources to the expected output folder target/test-classes - the root cause was that in the project's properties under Java Build Path -> Source tab -> Source folders on build path -> src/test/resources, somehow there was an Excluded: ** entry. I removed that excluded entry.

    Alternatively, I could have manually copied src/test/resources/log4j.properties to target/test-classes/log4j.properties.

    0 讨论(0)
  • 2020-11-22 08:52

    This Short introduction to log4j guide is a little bit old but still valid.

    That guide will give you some information about how to use loggers and appenders.


    Just to get you going you have two simple approaches you can take.

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

    BasicConfigurator.configure();
    

    Second approach is to add this standard log4j.properties (taken from the above mentioned guide) file to your classpath:

    # Set root logger level to DEBUG and its only appender to A1.
    log4j.rootLogger=DEBUG, A1
    
    # A1 is set to be a ConsoleAppender.
    log4j.appender.A1=org.apache.log4j.ConsoleAppender
    
    # A1 uses PatternLayout.
    log4j.appender.A1.layout=org.apache.log4j.PatternLayout
    log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n
    
    0 讨论(0)
  • 2020-11-22 08:52

    It looks like you need to add the location of your log4j.properties file to the Classpath in Eclipse.

    Make sure your project is open in Eclipse, then click on the "Run" menu at the top of Eclipse and click on the following:

    1. Run
    2. Run Configurations
    3. Classpath (tab)
    4. User Entries
    5. Advanced (button on the right)
    6. Add Folders
    7. then navigate to the folder that contains your log4j.properties file
    8. Apply
    9. Run

    The error message should no longer appear.

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