3

I'm currently trying and failing to lower the logging level for the PDFBox 1.8.6 library I'm using in my java project. Based on this previous question, I have the following log4j.properties file located in the /src/ directory.

log4j.rootLogger=ERROR, stdout

log4j.logger.org.apache.pdfbox=ERROR

# 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.file.layout.ConversionPattern=%d{ISO8601} %-5p [%c{2}]: %m%n

I'm not sure what else I'm missing, because I'm still being spammed with messages like the following in console.

Jul 10, 2014 10:19:23 AM org.apache.pdfbox.pdfparser.PDFParser parseXrefTable
WARNING: Count in xref table is 0 at offset 223265
Jul 10, 2014 10:19:23 AM org.apache.pdfbox.pdfparser.PDFParser parseXrefTable
WARNING: Count in xref table is 0 at offset 223265
Jul 10, 2014 10:19:23 AM org.apache.pdfbox.pdfparser.PDFParser parseXrefTable
WARNING: Count in xref table is 0 at offset 223265

If there's a way to change the log level programmatically, I'm not attached to the properties file at all since I don't use log4j anywhere else. I don't know if it's even using the log4j.properties file at all. I'm using Eclipse, I'm not certain how to confirm that the log4j.properties file is on the classpath as intended.

Community
  • 1
  • 1
user3670023
  • 61
  • 1
  • 3

2 Answers2

3

I was having the same problem until I figured it out.

The PDFBox 1.8.6 library doesn't use Log4j directly. Instead it uses Apache Commons Logging Framework (http://commons.apache.org/proper/commons-logging/guide.html).

The commons logging framework has it's own property file to configure logging and only uses log4j as a fallback if it is on the classpath.

To fix this you can either configure the apache commons logging to ignore it or add log4j to your classpath. I found some instructions here, but I wasn't able to get them to work (http://cyntech.wordpress.com/2009/01/09/how-to-use-commons-logging/)

What I finally ended up doing was to add the log4j.jar to my classpath and then the instructions above (add a log4j.properties file in your classpath, with contents log4j.rootLogger=ERROR, stdout) finally worked.

  • 2
    In case someone finds this answer and is tortured in the same way that I was: log4j is no longer distributed as a single JAR in log4j 2. In addition to the core and api JARs, you also need the shim JAR which connects Commons logging to log4j. Without that in your classpath, the configuration file will continue to be ignored. This doesn't appear to be documented anywhere on the Commons logging site; the proper nugget is here: https://logging.apache.org/log4j/2.x/faq.html#which_jars – Sam Bayer May 09 '19 at 14:11
1

The log4j.properties file should be in the same folder as your .class files. Right now it doesn't appear to be getting loaded. You have properly set the logging level in the first line:

log4j.rootLogger=ERROR, stdout

This should set the root logger in log4j to use the ERROR logging level, meaning it will only display logging messages of severity ERROR or higher.

Also, If you only want to affect PDFBox, and you know the library's package, then you can selectively set the logging level of a package by adding something like this in your properties file:

log4j.logger.com.thepackage.of.pdfbox=ERROR
Christian Wilkie
  • 3,693
  • 5
  • 34
  • 49
  • To clarify about "in the same folder as your .class files", if my main class were my.package.Main, should I place the properties files in /src/log4j.properties or /src/my/package/log4j.properties? Where I have it right now, it's landing in the top-level directory of the .jar file. – user3670023 Jul 10 '14 at 17:57
  • I'm not sure how your project is setup in Eclipse, but it should be in a resources folder. I follow the Maven convention of putting my properties file in `src/main/resources` and my source code in `src/main/java`. When the project is built, the log4j.properties file is put into the output folder /target/classes and the root directory of my .jar file. – Christian Wilkie Jul 10 '14 at 18:03
  • Ah. I'm not using Maven. I attempted to put it into the resources folder before, but log4j wasn't recognizing it there either, even after I put the resource folder onto the classpath. – user3670023 Jul 10 '14 at 18:08
  • Weird, are you sure the resource folder was setup properly and that the properties file is getting copied to the correct location in the output? You can also try `log4j.rootLogger=OFF, stdout` but `ERROR` should be a higher level than `WARN`. – Christian Wilkie Jul 10 '14 at 18:15
  • I put the properties file back into the /src/ folder for simplicity, and this is how the jar looks when opened up in WinRAR: http://imgur.com/SFdWRFU I set the logging level to off, but it didn't do anything because it's not loading it properly. – user3670023 Jul 10 '14 at 18:38
  • Did you include the log4j jar file in your project? – Tilman Hausherr Jul 11 '14 at 06:32