source: trunk/docx4j/src/main/java/org/docx4j/utils/Log4jConfigurator.java @ 1779

Revision 1779, 1.9 KB checked in by jharrop, 6 weeks ago (diff)

Fix suggested by Pierre for getAllAppenders issues (slf4j).

Line 
1package org.docx4j.utils;
2
3import java.util.Enumeration;
4
5import org.apache.log4j.BasicConfigurator;
6import org.apache.log4j.Level;
7import org.apache.log4j.LogManager;
8import org.apache.log4j.Logger;
9import org.docx4j.Docx4jProperties;
10
11//From http://wiki.apache.org/logging-log4j/UsefulCode
12public 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}
Note: See TracBrowser for help on using the repository browser.