1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.util.log4j;
16  
17  import com.liferay.portal.kernel.util.ServerDetector;
18  
19  import java.net.URL;
20  
21  import java.util.Enumeration;
22  import java.util.HashSet;
23  import java.util.Iterator;
24  import java.util.Set;
25  
26  import org.apache.log4j.Level;
27  import org.apache.log4j.LogManager;
28  import org.apache.log4j.Logger;
29  import org.apache.log4j.xml.DOMConfigurator;
30  
31  import org.dom4j.Document;
32  import org.dom4j.Element;
33  import org.dom4j.io.SAXReader;
34  
35  /**
36   * <a href="Log4JUtil.java.html"><b><i>View Source</i></b></a>
37   *
38   * @author Brian Wing Shun Chan
39   */
40  public class Log4JUtil {
41  
42      public static void configureLog4J(URL url) {
43          if (url == null) {
44              return;
45          }
46  
47          // See LPS-6029 and LPS-8865
48  
49          if (!ServerDetector.isJBoss()) {
50              DOMConfigurator.configure(url);
51          }
52  
53          Set<String> currentLoggerNames = new HashSet<String>();
54  
55          Enumeration<Logger> enu = LogManager.getCurrentLoggers();
56  
57          while (enu.hasMoreElements()) {
58              Logger logger = enu.nextElement();
59  
60              currentLoggerNames.add(logger.getName());
61          }
62  
63          try {
64              SAXReader reader = new SAXReader();
65  
66              Document doc = reader.read(url);
67  
68              Element root = doc.getRootElement();
69  
70              Iterator<Element> itr = root.elements("category").iterator();
71  
72              while (itr.hasNext()) {
73                  Element category = itr.next();
74  
75                  String name = category.attributeValue("name");
76                  String priority =
77                      category.element("priority").attributeValue("value");
78  
79                  setLevel(name, priority);
80              }
81          }
82          catch (Exception e) {
83              e.printStackTrace();
84          }
85      }
86  
87      public static void setLevel(String name, String priority) {
88          Logger logger = Logger.getLogger(name);
89  
90          logger.setLevel(Level.toLevel(priority));
91  
92          java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
93              name);
94  
95          jdkLogger.setLevel(_getJdkLevel(priority));
96      }
97  
98      private static java.util.logging.Level _getJdkLevel(String priority) {
99          if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
100             return java.util.logging.Level.FINE;
101         }
102         else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
103             return java.util.logging.Level.SEVERE;
104         }
105         else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
106             return java.util.logging.Level.WARNING;
107         }
108         else {
109             return java.util.logging.Level.INFO;
110         }
111     }
112 
113 }