How To Suppress Java Logging in JRuby
JRuby is a powerful language: it combines the pleasure of writing Ruby with the power and libraries of Java. Almost everything works perfectly, but it’s fundamentally a bastard language, and so there are a few corner cases that are quite awkward — like logging.
Most Ruby logging seems to be accomplished with simple `puts` statements or occasionally `STDERR.puts`. I’ve never seen `Logger` used. But Java has a quite complex system of a “basic” logger in the standard library (java.util.logging) and a handful of fancier systems like log4j that seem to be better suited to enterprise programming. Log4j, for instance, appears to be configured with a XML file in your classpath. This was well beyond my high-school Java skills.
My specific case was to suppress INFO statements output by the PDF parsing library PDFBox. PDFBox is the library that Tabula, a tool for extracting tabular data from PDFs, depends on to parse PDFs. For some reason, certain unimplemented PDF operators log tons of crap like:
org.apache.pdfbox.util.PDFStreamEngine processOperatorINFO: unsupported/disabled operation: EI
After a few hours of Googling, I found a solution to mute those log statements without integrating a log4j binary into the project. It may not be pretty or the “best” way to do it, but it sure works.
import 'java.util.logging.LogManager'
import 'java.util.logging.Level'
lm = LogManager.log_manager
lm.logger_names.each do |name|
if name == "" # "" is the rootlogger
# which is apparently the logger PDFBox talks to.
l = lm.get_logger(name)
l.level = Level::OFF
l.handlers.each do |h|
h.level = Level::OFF
end
end
end
I hope this solves a similar problem for you. It took me quite a while to find it, so I wrote it up to get the solution somewhere more easily findable.