Log4J creates log file but does not write to it

前端 未结 3 1216
执笔经年
执笔经年 2021-01-02 07:50

I\'m trying to configure log4j to log messages to a file. Right now, the file does get created with the name I provide, but the logs are not written to the file. My code:

3条回答
  •  不知归路
    2021-01-02 08:45

    I had the same problem as you. File was created, but without any loggs in it just in console. And it was because of incorrect dependencies in maven project in my case.

    My log4j.properties file was:

    # Root logger option
    log4j.rootLogger=DEBUG, file
    
    # Direct log messages to a log file
    # configuration to print into file
    log4j.appender.file=org.apache.log4j.RollingFileAppender
    log4j.appender.file.MaxFileSize=1MB
    log4j.appender.file.MaxBackupIndex=10
    # Define the layout for file appender
    log4j.appender.file.layout=org.apache.log4j.PatternLayout
    #log4j.appender.file.layout.ConversionPattern=[%t] %-5p %c %x - %m%n
    log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
    # Set the name of the file
    log4j.appender.file.File=C:\\log\\logging.log
    # Set the append to false, overwrite
    log4j.appender.file.Append=false
    

    And I used in POM:

    
      org.slf4j
      slf4j-simple
      1.6.2
    
    
    
      log4j
      log4j
      1.2.17
    
    

    Yes, it created for me file where I needed it, but logs were in console. Then I changed it to another dependency like:

    
        org.slf4j
        slf4j-log4j12
        1.7.5
    
    

    And I finally got it in file instead of console. Even without any explicit commands like PropertyConfigurator.configure().

提交回复
热议问题