FileAppender not writing anything in log4j

2019-07-15 10:14发布

I have a small problem using log4j and FileAppender. When I am using ConsoleAppender, everything is working well but FileAppender won't ever produce a file (or write into a file). Moreover, I have no errors concerning log4j in the osgi console.

Here's my configuration :

# main link (console or file, depending on what we want)
# log4j.category.com.foo=DEBUG, console
  log4j.category.com.foo=DEBUG, file
# log4j.category.com.foo=DEBUG, console, file

#  console logging
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-4r %-5p %c %x - %m%n

# file logging
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=C:\\Users\\foo\\some_file.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%-4r %-5p %c %x - %m%n

Note that the ConversionPattern attributes are exactly the same in the two appenders.

I have tried to change the File argument to write the log in the "run" directory but it didn't do anything. I also tried to add log4j.appender.file.ImmediateFlush=true without success.

Have I missed something (obvious ?) somewhere ? It sound strange that log4j isn't even able to write a simple log in a file....

Thanks a lot in advance for your help.

标签: log4j osgi
2条回答
别忘想泡老子
2楼-- · 2019-07-15 10:47

Replace :

 log4j.category.com.foo=DEBUG, console

By :

 log4j.category.com.foo=DEBUG, console, file
查看更多
太酷不给撩
3楼-- · 2019-07-15 11:09

I know this is old but for other Googlers... For me it was simply using wrong package. I used org.apache.logging.log4j instead org.apache.log4j If you are using Maven use this:

    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.17</version>
    </dependency>
查看更多
登录 后发表回答