1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.util.log4j;
16  
17  import java.net.URL;
18  
19  import java.util.Enumeration;
20  import java.util.HashSet;
21  import java.util.Iterator;
22  import java.util.Set;
23  
24  import org.apache.log4j.Level;
25  import org.apache.log4j.LogManager;
26  import org.apache.log4j.Logger;
27  import org.apache.log4j.xml.DOMConfigurator;
28  
29  import org.dom4j.Document;
30  import org.dom4j.Element;
31  import org.dom4j.io.SAXReader;
32  
33  /**
34   * <a href="Log4JUtil.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   */
38  public class Log4JUtil {
39  
40      public static void configureLog4J(URL url) {
41          if (url == null) {
42              return;
43          }
44  
45          DOMConfigurator.configure(url);
46  
47          Set<String> currentLoggerNames = new HashSet<String>();
48  
49          Enumeration<Logger> enu = LogManager.getCurrentLoggers();
50  
51          while (enu.hasMoreElements()) {
52              Logger logger = enu.nextElement();
53  
54              currentLoggerNames.add(logger.getName());
55          }
56  
57          try {
58              SAXReader reader = new SAXReader();
59  
60              Document doc = reader.read(url);
61  
62              Element root = doc.getRootElement();
63  
64              Iterator<Element> itr = root.elements("category").iterator();
65  
66              while (itr.hasNext()) {
67                  Element category = itr.next();
68  
69                  String name = category.attributeValue("name");
70                  String priority =
71                      category.element("priority").attributeValue("value");
72  
73                  setLevel(name, priority);
74              }
75          }
76          catch (Exception e) {
77              e.printStackTrace();
78          }
79      }
80  
81      public static void setLevel(String name, String priority) {
82          Logger logger = Logger.getLogger(name);
83  
84          logger.setLevel(Level.toLevel(priority));
85  
86          java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
87              name);
88  
89          jdkLogger.setLevel(_getJdkLevel(priority));
90      }
91  
92      private static java.util.logging.Level _getJdkLevel(String priority) {
93          if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
94              return java.util.logging.Level.FINE;
95          }
96          else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
97              return java.util.logging.Level.SEVERE;
98          }
99          else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
100             return java.util.logging.Level.WARNING;
101         }
102         else {
103             return java.util.logging.Level.INFO;
104         }
105     }
106 
107 }