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.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  public class Log4JUtil {
48  
49      public static void configureLog4J(URL url) {
50          if (url == null) {
51              return;
52          }
53  
54          if (Logger.getRootLogger().getAllAppenders() instanceof
55                  NullEnumeration) {
56  
57              DOMConfigurator.configure(url);
58          }
59          else {
60              Set<String> currentLoggerNames = new HashSet<String>();
61  
62              Enumeration<Logger> enu = LogManager.getCurrentLoggers();
63  
64              while (enu.hasMoreElements()) {
65                  Logger logger = enu.nextElement();
66  
67                  currentLoggerNames.add(logger.getName());
68              }
69  
70              try {
71                  SAXReader reader = new SAXReader();
72  
73                  Document doc = reader.read(url);
74  
75                  Element root = doc.getRootElement();
76  
77                  Iterator<Element> itr = root.elements("category").iterator();
78  
79                  while (itr.hasNext()) {
80                      Element category = itr.next();
81  
82                      String name = category.attributeValue("name");
83                      String priority =
84                          category.element("priority").attributeValue("value");
85  
86                      setLevel(name, priority);
87                  }
88              }
89              catch (Exception e) {
90                  e.printStackTrace();
91              }
92          }
93      }
94  
95      public static void setLevel(String name, String priority) {
96          Logger logger = Logger.getLogger(name);
97  
98          logger.setLevel(Level.toLevel(priority));
99  
100         java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
101             name);
102 
103         jdkLogger.setLevel(_getJdkLevel(priority));
104     }
105 
106     private static java.util.logging.Level _getJdkLevel(String priority) {
107         if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
108             return java.util.logging.Level.FINE;
109         }
110         else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
111             return java.util.logging.Level.SEVERE;
112         }
113         else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
114             return java.util.logging.Level.WARNING;
115         }
116         else {
117             return java.util.logging.Level.INFO;
118         }
119     }
120 
121 }