01: /**
02: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
03: *
04: * Permission is hereby granted, free of charge, to any person obtaining a copy
05: * of this software and associated documentation files (the "Software"), to deal
06: * in the Software without restriction, including without limitation the rights
07: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
08: * copies of the Software, and to permit persons to whom the Software is
09: * 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: */package com.liferay.util.log4j;
22:
23: import java.net.URL;
24:
25: import java.util.Enumeration;
26: import java.util.HashSet;
27: import java.util.Iterator;
28: import java.util.Set;
29:
30: import org.apache.log4j.Level;
31: import org.apache.log4j.LogManager;
32: import org.apache.log4j.Logger;
33: import org.apache.log4j.helpers.NullEnumeration;
34: import org.apache.log4j.xml.DOMConfigurator;
35:
36: import org.dom4j.Document;
37: import org.dom4j.Element;
38: import org.dom4j.io.SAXReader;
39:
40: /**
41: * <a href="Log4JUtil.java.html"><b><i>View Source</i></b></a>
42: *
43: * @author Brian Wing Shun Chan
44: *
45: */
46: public class Log4JUtil {
47:
48: public static void configureLog4J(URL url) {
49: if (url == null) {
50: return;
51: }
52:
53: if (Logger.getRootLogger().getAllAppenders() instanceof NullEnumeration) {
54:
55: DOMConfigurator.configure(url);
56: } else {
57: Set currentLoggerNames = new HashSet();
58:
59: Enumeration enu = LogManager.getCurrentLoggers();
60:
61: while (enu.hasMoreElements()) {
62: Logger logger = (Logger) enu.nextElement();
63:
64: currentLoggerNames.add(logger.getName());
65: }
66:
67: try {
68: SAXReader reader = new SAXReader();
69:
70: Document doc = reader.read(url);
71:
72: Element root = doc.getRootElement();
73:
74: Iterator itr = root.elements("category").iterator();
75:
76: while (itr.hasNext()) {
77: Element category = (Element) itr.next();
78:
79: String name = category.attributeValue("name");
80: String priority = category.element("priority")
81: .attributeValue("value");
82:
83: Logger logger = Logger.getLogger(name);
84:
85: logger.setLevel(Level.toLevel(priority));
86: }
87: } catch (Exception e) {
88: e.printStackTrace();
89: }
90: }
91: }
92:
93: }
|