1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util.log4j;
24  
25  import java.net.URL;
26  
27  import java.util.Enumeration;
28  import java.util.HashSet;
29  import java.util.Iterator;
30  import java.util.Set;
31  
32  import org.apache.log4j.Level;
33  import org.apache.log4j.LogManager;
34  import org.apache.log4j.Logger;
35  import org.apache.log4j.helpers.NullEnumeration;
36  import org.apache.log4j.xml.DOMConfigurator;
37  
38  import org.dom4j.Document;
39  import org.dom4j.Element;
40  import org.dom4j.io.SAXReader;
41  
42  /**
43   * <a href="Log4JUtil.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   *
47   */
48  public class Log4JUtil {
49  
50      public static void configureLog4J(URL url) {
51          if (url == null) {
52              return;
53          }
54  
55          if (Logger.getRootLogger().getAllAppenders() instanceof
56                  NullEnumeration) {
57  
58              DOMConfigurator.configure(url);
59          }
60          else {
61              Set<String> currentLoggerNames = new HashSet<String>();
62  
63              Enumeration<Logger> enu = LogManager.getCurrentLoggers();
64  
65              while (enu.hasMoreElements()) {
66                  Logger logger = enu.nextElement();
67  
68                  currentLoggerNames.add(logger.getName());
69              }
70  
71              try {
72                  SAXReader reader = new SAXReader();
73  
74                  Document doc = reader.read(url);
75  
76                  Element root = doc.getRootElement();
77  
78                  Iterator<Element> itr = root.elements("category").iterator();
79  
80                  while (itr.hasNext()) {
81                      Element category = itr.next();
82  
83                      String name = category.attributeValue("name");
84                      String priority =
85                          category.element("priority").attributeValue("value");
86  
87                      setLevel(name, priority);
88                  }
89              }
90              catch (Exception e) {
91                  e.printStackTrace();
92              }
93          }
94      }
95  
96      public static void setLevel(String name, String priority) {
97          Logger logger = Logger.getLogger(name);
98  
99          logger.setLevel(Level.toLevel(priority));
100 
101         java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
102             name);
103 
104         jdkLogger.setLevel(_getJdkLevel(priority));
105     }
106 
107     private static java.util.logging.Level _getJdkLevel(String priority) {
108         if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
109             return java.util.logging.Level.FINE;
110         }
111         else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
112             return java.util.logging.Level.SEVERE;
113         }
114         else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
115             return java.util.logging.Level.WARNING;
116         }
117         else {
118             return java.util.logging.Level.INFO;
119         }
120     }
121 
122 }