001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.util.log4j;
016    
017    import com.liferay.portal.kernel.util.PropsKeys;
018    import com.liferay.portal.kernel.util.PropsUtil;
019    import com.liferay.portal.kernel.util.ServerDetector;
020    import com.liferay.portal.kernel.util.StreamUtil;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.StringUtil;
023    
024    import java.io.InputStream;
025    import java.io.Reader;
026    import java.io.StringReader;
027    
028    import java.net.URL;
029    
030    import java.util.Enumeration;
031    import java.util.HashMap;
032    import java.util.HashSet;
033    import java.util.List;
034    import java.util.Map;
035    import java.util.Set;
036    
037    import org.apache.log4j.Level;
038    import org.apache.log4j.LogManager;
039    import org.apache.log4j.Logger;
040    import org.apache.log4j.xml.DOMConfigurator;
041    
042    import org.aspectj.util.FileUtil;
043    
044    import org.dom4j.Document;
045    import org.dom4j.Element;
046    import org.dom4j.io.SAXReader;
047    
048    /**
049     * @author Brian Wing Shun Chan
050     */
051    public class Log4JUtil {
052    
053            public static void configureLog4J(URL url) {
054                    if (url == null) {
055                            return;
056                    }
057    
058                    String urlContent = _getURLContent(url);
059    
060                    if (urlContent == null) {
061                            return;
062                    }
063    
064                    // See LPS-6029 and LPS-8865
065    
066                    if (!ServerDetector.isJBoss()) {
067                            DOMConfigurator domConfigurator = new DOMConfigurator();
068    
069                            Reader urlReader = new StringReader(urlContent);
070    
071                            domConfigurator.doConfigure(
072                                    urlReader, LogManager.getLoggerRepository());
073                    }
074    
075                    Set<String> currentLoggerNames = new HashSet<String>();
076    
077                    Enumeration<Logger> enu = LogManager.getCurrentLoggers();
078    
079                    while (enu.hasMoreElements()) {
080                            Logger logger = enu.nextElement();
081    
082                            currentLoggerNames.add(logger.getName());
083                    }
084    
085                    try {
086                            SAXReader saxReader = new SAXReader();
087    
088                            Reader urlReader = new StringReader(urlContent);
089    
090                            Document document = saxReader.read(urlReader, url.toExternalForm());
091    
092                            Element rootElement = document.getRootElement();
093    
094                            List<Element> categoryElements = rootElement.elements("category");
095    
096                            for (Element categoryElement : categoryElements) {
097                                    String name = categoryElement.attributeValue("name");
098    
099                                    Element priorityElement = categoryElement.element("priority");
100    
101                                    String priority = priorityElement.attributeValue("value");
102    
103                                    setLevel(name, priority);
104                            }
105                    }
106                    catch (Exception e) {
107                            e.printStackTrace();
108                    }
109            }
110    
111            public static void setLevel(String name, String priority) {
112                    Logger logger = Logger.getLogger(name);
113    
114                    logger.setLevel(Level.toLevel(priority));
115    
116                    java.util.logging.Logger jdkLogger = java.util.logging.Logger.getLogger(
117                            name);
118    
119                    jdkLogger.setLevel(_getJdkLevel(priority));
120            }
121    
122            private static java.util.logging.Level _getJdkLevel(String priority) {
123                    if (priority.equalsIgnoreCase(Level.DEBUG.toString())) {
124                            return java.util.logging.Level.FINE;
125                    }
126                    else if (priority.equalsIgnoreCase(Level.ERROR.toString())) {
127                            return java.util.logging.Level.SEVERE;
128                    }
129                    else if (priority.equalsIgnoreCase(Level.WARN.toString())) {
130                            return java.util.logging.Level.WARNING;
131                    }
132                    else {
133                            return java.util.logging.Level.INFO;
134                    }
135            }
136    
137            private static String _getURLContent(URL url) {
138                    Map<String, String> variables = new HashMap<String, String>();
139    
140                    variables.put("liferay.home", PropsUtil.get(PropsKeys.LIFERAY_HOME));
141    
142                    String urlContent = null;
143    
144                    InputStream inputStream = null;
145    
146                    try {
147                            inputStream = url.openStream();
148    
149                            byte[] bytes = FileUtil.readAsByteArray(inputStream);
150    
151                            urlContent = new String(bytes, StringPool.UTF8);
152                    }
153                    catch (Exception e) {
154                            e.printStackTrace();
155    
156                            return null;
157                    }
158                    finally {
159                            StreamUtil.cleanUp(inputStream);
160                    }
161    
162                    for (Map.Entry<String, String> variable : variables.entrySet()) {
163                            urlContent = urlContent.replaceAll(
164                                    "@" + variable.getKey() + "@", variable.getValue());
165                    }
166    
167                    if (ServerDetector.getServerId() != null) {
168                            return urlContent;
169                    }
170    
171                    int x = urlContent.indexOf("<appender name=\"FILE\"");
172    
173                    int y = urlContent.indexOf("</appender>", x);
174    
175                    if (y != -1) {
176                            y = urlContent.indexOf("<", y + 1);
177                    }
178    
179                    if ((x != -1) && (y != -1)) {
180                            urlContent = urlContent.substring(0, x) + urlContent.substring(y);
181                    }
182    
183                    urlContent = StringUtil.replace(
184                            urlContent, "<appender-ref ref=\"FILE\" />", StringPool.BLANK);
185    
186                    return urlContent;
187            }
188    
189    }