| 1 | package org.docx4j.utils; |
|---|
| 2 | |
|---|
| 3 | import java.util.Enumeration; |
|---|
| 4 | |
|---|
| 5 | import org.apache.log4j.BasicConfigurator; |
|---|
| 6 | import org.apache.log4j.Level; |
|---|
| 7 | import org.apache.log4j.LogManager; |
|---|
| 8 | import org.apache.log4j.Logger; |
|---|
| 9 | import org.docx4j.Docx4jProperties; |
|---|
| 10 | |
|---|
| 11 | //From http://wiki.apache.org/logging-log4j/UsefulCode |
|---|
| 12 | public class Log4jConfigurator { |
|---|
| 13 | |
|---|
| 14 | public synchronized static void configure() { |
|---|
| 15 | |
|---|
| 16 | boolean disabled = Boolean.parseBoolean( |
|---|
| 17 | Docx4jProperties.getProperties().getProperty("docx4j.Log4j.Configurator.disabled", "false")); |
|---|
| 18 | if (disabled) return; |
|---|
| 19 | |
|---|
| 20 | if (!isConfigured()) { |
|---|
| 21 | |
|---|
| 22 | BasicConfigurator.configure(); |
|---|
| 23 | |
|---|
| 24 | // So now we should have a ConsoleAppender |
|---|
| 25 | // we don't want debug level logging. |
|---|
| 26 | Logger.getRootLogger().setLevel(Level.INFO); |
|---|
| 27 | Logger log = Logger.getLogger(Log4jConfigurator.class); |
|---|
| 28 | log.info("Since your log4j configuration (if any) was not found, docx4j has configured log4j automatically."); |
|---|
| 29 | |
|---|
| 30 | try { |
|---|
| 31 | org.docx4j.convert.out.pdf.viaXSLFO.Conversion.log.setLevel(Level.DEBUG); |
|---|
| 32 | } catch (NoClassDefFoundError n) { |
|---|
| 33 | // If FOP jar is not available |
|---|
| 34 | } |
|---|
| 35 | } |
|---|
| 36 | } |
|---|
| 37 | |
|---|
| 38 | /** |
|---|
| 39 | Returns true if it appears that log4j have been previously configured. This code |
|---|
| 40 | checks to see if there are any appenders defined for log4j which is the |
|---|
| 41 | definitive way to tell if log4j is already initialized */ |
|---|
| 42 | private static boolean isConfigured() { |
|---|
| 43 | Enumeration appenders = Logger.getRootLogger().getAllAppenders(); |
|---|
| 44 | if (appenders.hasMoreElements()) { |
|---|
| 45 | return true; |
|---|
| 46 | } |
|---|
| 47 | else { |
|---|
| 48 | Enumeration loggers = LogManager.getCurrentLoggers() ; |
|---|
| 49 | while (loggers.hasMoreElements()) { |
|---|
| 50 | Logger c = (Logger) loggers.nextElement(); |
|---|
| 51 | if (c.getAllAppenders().hasMoreElements()) |
|---|
| 52 | return true; |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | return false; |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | } |
|---|