1 /** 2 * This file Copyright (c) 2003-2011 Magnolia International 3 * Ltd. (http://www.magnolia-cms.com). All rights reserved. 4 * 5 * 6 * This file is dual-licensed under both the Magnolia 7 * Network Agreement and the GNU General Public License. 8 * You may elect to use one or the other of these licenses. 9 * 10 * This file is distributed in the hope that it will be 11 * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the 12 * implied warranty of MERCHANTABILITY or FITNESS FOR A 13 * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT. 14 * Redistribution, except as permitted by whichever of the GPL 15 * or MNA you select, is prohibited. 16 * 17 * 1. For the GPL license (GPL), you can redistribute and/or 18 * modify this file under the terms of the GNU General 19 * Public License, Version 3, as published by the Free Software 20 * Foundation. You should have received a copy of the GNU 21 * General Public License, Version 3 along with this program; 22 * if not, write to the Free Software Foundation, Inc., 51 23 * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 24 * 25 * 2. For the Magnolia Network Agreement (MNA), this file 26 * and the accompanying materials are made available under the 27 * terms of the MNA which accompanies this distribution, and 28 * is available at http://www.magnolia-cms.com/mna.html 29 * 30 * Any modifications to this file must keep this entire header 31 * intact. 32 * 33 */ 34 package info.magnolia.logging; 35 36 import info.magnolia.cms.core.SystemProperty; 37 import info.magnolia.cms.util.ConfigUtil; 38 import org.apache.commons.io.IOUtils; 39 import org.apache.commons.lang.StringUtils; 40 import org.apache.log4j.LogManager; 41 import org.apache.log4j.PropertyConfigurator; 42 import org.apache.log4j.xml.DOMConfigurator; 43 import org.w3c.dom.Document; 44 45 import java.io.IOException; 46 import java.util.Collections; 47 import java.util.Map; 48 import java.util.Properties; 49 50 /** 51 * <p> 52 * Log4j initializer. Loads the file specified using the <code>log4j.config</code> init parameter and optionally set a 53 * system property containing the magnolia web application root directory with the name specified by the 54 * <code>magnolia.root.sysproperty</code> init parameter. 55 * </p> 56 * <p> 57 * If <code>magnolia.root.sysproperty</code> is empty no system variable will be set; if <code>log4j.config</code> 58 * is empty no log4j initialization will be performed. 59 * </p> 60 * <p> 61 * You can easily specify relative paths for log4j configuration files using the magnolia root system property, for 62 * example using <code>${magnolia.root}logs/magnolia-debug.log</code> 63 * </p> 64 * <p> 65 * Note: if you drop multiple magnolia wars in a container which doesn't isolate system properties (e.g. tomcat) you 66 * could need to change the name of the <code>magnolia.root.sysproperty</code> variable in web.xml and in log4j 67 * configuration files. 68 * </p> 69 * <p> 70 * <em>Some ideas and snippets borrowed from the more complex Spring implementation http://www.springframework.org</em> 71 * </p> 72 * @author Fabrizio Giustina 73 * @version $Id$ 74 */ 75 public class Log4jConfigurer { 76 77 /** 78 * Init parameter specifying the location of the Log4J config file. 79 */ 80 public static final String LOG4J_CONFIG = "log4j.config"; //$NON-NLS-1$ 81 82 /** 83 * Initialize Log4J, including setting the web app root system property. 84 */ 85 public static void initLogging() { 86 87 // can't use log4j yet 88 log("Initializing Log4J"); //$NON-NLS-1$ 89 90 String log4jFileName = SystemProperty.getProperty(LOG4J_CONFIG); 91 if (StringUtils.isNotEmpty(log4jFileName)) { 92 boolean isXml = log4jFileName.toLowerCase().endsWith(".xml"); //$NON-NLS-1$ 93 94 log("Initializing Log4J from [" + log4jFileName + "]"); //$NON-NLS-1$ //$NON-NLS-2$ 95 96 final String config; 97 try { 98 config = ConfigUtil.getTokenizedConfigFile(log4jFileName); 99 } 100 catch (IOException e) { 101 log("Unable to initialize Log4J from [" + log4jFileName + "], got a IOException " + e.getMessage()); 102 return; 103 } 104 105 // classpath? 106 if (isXml) { 107 try { 108 final Map dtds = Collections.singletonMap("log4j.dtd", "/org/apache/log4j/xml/log4j.dtd"); 109 final Document document = ConfigUtil.string2DOM(config, dtds); 110 DOMConfigurator.configure(document.getDocumentElement()); 111 } catch (Exception e) { 112 log("Unable to initialize Log4J from [" + log4jFileName + "], got an Exception during reading the xml file : " + e.getMessage()); 113 } 114 } else { 115 try { 116 final Properties properties = new Properties(); 117 properties.load(IOUtils.toInputStream(config)); 118 PropertyConfigurator.configure(properties); 119 } catch (IOException e) { 120 log("Unable to initialize Log4J from [" + log4jFileName + "], got an Exception during reading the properties file : " + e.getMessage()); 121 } 122 } 123 124 } 125 } 126 127 /** 128 * Shuts down Log4J. 129 */ 130 public static void shutdownLogging() { 131 log("Shutting down Log4J"); //$NON-NLS-1$ 132 LogManager.shutdown(); 133 } 134 135 /** 136 * Handy System.out method to use when logging isn't configured yet. 137 * @param message log message 138 */ 139 private static void log(String message) { 140 System.out.println(message); 141 } 142 143 }