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                      setLevel(name, priority);
85                  }
86              }
87              catch (Exception e) {
88                  e.printStackTrace();
89              }
90          }
91      }
92  
93      public static void setLevel(String name, String priority) {
94          Logger logger = Logger.getLogger(name);
95  
96          logger.setLevel(Level.toLevel(priority));
97  
98          java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
99              name);
100 
101         jdkLogger.setLevel(_getJdkLevel(priority));
102     }
103 
104     private static java.util.logging.Level _getJdkLevel(String priority) {
105         if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
106             return java.util.logging.Level.FINE;
107         }
108         else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
109             return java.util.logging.Level.SEVERE;
110         }
111         else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
112             return java.util.logging.Level.WARNING;
113         }
114         else {
115             return java.util.logging.Level.INFO;
116         }
117     }
118 
119 }