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