| Log4JUtil.java |
1 /**
2 * Copyright (c) 2000-2008 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 Logger logger = Logger.getLogger(name);
88
89 logger.setLevel(Level.toLevel(priority));
90 }
91 }
92 catch (Exception e) {
93 e.printStackTrace();
94 }
95 }
96 }
97
98 }