| Log4JUtil.java |
1 /**
2 * Copyright (c) 2000-2009 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 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 package com.liferay.util.log4j;
21
22 import java.net.URL;
23
24 import java.util.Enumeration;
25 import java.util.HashSet;
26 import java.util.Iterator;
27 import java.util.Set;
28
29 import org.apache.log4j.Level;
30 import org.apache.log4j.LogManager;
31 import org.apache.log4j.Logger;
32 import org.apache.log4j.helpers.NullEnumeration;
33 import org.apache.log4j.xml.DOMConfigurator;
34
35 import org.dom4j.Document;
36 import org.dom4j.Element;
37 import org.dom4j.io.SAXReader;
38
39 /**
40 * <a href="Log4JUtil.java.html"><b><i>View Source</i></b></a>
41 *
42 * @author Brian Wing Shun Chan
43 *
44 */
45 public class Log4JUtil {
46
47 public static void configureLog4J(URL url) {
48 if (url == null) {
49 return;
50 }
51
52 if (Logger.getRootLogger().getAllAppenders() instanceof
53 NullEnumeration) {
54
55 DOMConfigurator.configure(url);
56 }
57 else {
58 Set<String> currentLoggerNames = new HashSet<String>();
59
60 Enumeration<Logger> enu = LogManager.getCurrentLoggers();
61
62 while (enu.hasMoreElements()) {
63 Logger logger = enu.nextElement();
64
65 currentLoggerNames.add(logger.getName());
66 }
67
68 try {
69 SAXReader reader = new SAXReader();
70
71 Document doc = reader.read(url);
72
73 Element root = doc.getRootElement();
74
75 Iterator<Element> itr = root.elements("category").iterator();
76
77 while (itr.hasNext()) {
78 Element category = itr.next();
79
80 String name = category.attributeValue("name");
81 String priority =
82 category.element("priority").attributeValue("value");
83
84 Logger logger = Logger.getLogger(name);
85
86 logger.setLevel(Level.toLevel(priority));
87 }
88 }
89 catch (Exception e) {
90 e.printStackTrace();
91 }
92 }
93 }
94
95 }