KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > main > CmsLog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src/org/opencms/main/CmsLog.java,v $
3  * Date : $Date: 2006/03/27 14:52:27 $
4  * Version: $Revision: 1.26 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.main;
33
34 import org.opencms.configuration.CmsConfigurationManager;
35 import org.opencms.file.CmsResource;
36 import org.opencms.util.CmsFileUtil;
37
38 import java.io.File JavaDoc;
39 import java.net.URL JavaDoc;
40
41 import org.apache.commons.collections.ExtendedProperties;
42 import org.apache.commons.logging.Log;
43 import org.apache.commons.logging.LogFactory;
44 import org.apache.log4j.PropertyConfigurator;
45 import org.apache.log4j.helpers.Loader;
46
47 /**
48  * Provides the OpenCms logging mechanism.<p>
49  *
50  * The OpenCms logging mechanism is based on Apache Commons Logging.
51  * However, log4j is shipped with OpenCms and assumed to be used as default logging mechanism.
52  * Since apparently Commons Logging may cause issues in more complex classloader scenarios,
53  * we may switch the logging interface to log4j <code>UGLI</code> once the final release is available.<p>
54  *
55  * The log4j configuration file shipped with OpenCms is located
56  * in <code>${opencms.WEB-INF}/classes/log4j.properties</code>. OpenCms will auto-configure itself
57  * to write it's log file to <code>${opencms.WEB-INF}/logs/opencms.log</code>. This default behaviour
58  * can be supressed by either using a log4j configuration file from another location, or by setting the
59  * special property <code>${opencms.set.logfile}</code> in the log4j configuration file to <code>false</code>.
60  *
61  * @author Alexander Kandzior
62  *
63  * @version $Revision: 1.26 $
64  *
65  * @since 6.0.0
66  */

67 public final class CmsLog {
68
69     /** The name of the opencms.log file. */
70     public static final String JavaDoc FILE_LOG = "opencms.log";
71
72     /** Path to the "logs" folder relative to the "WEB-INF" directory of the application. */
73     public static final String JavaDoc FOLDER_LOGS = "logs" + File.separatorChar;
74
75     /** Log for initialization messages. */
76     public static final Log INIT = LogFactory.getLog("org.opencms.init");
77
78     /** The abolute path to the OpenCms log file (in the "real" file system). */
79     private static String JavaDoc m_logFileRfsPath;
80
81     /**
82      * Hides the public constructor.<p>
83      */

84     private CmsLog() {
85
86         // hides the public constructor
87
}
88
89     /**
90      * Initializes the OpenCms logger configuration.<p>
91      */

92     static {
93         try {
94             // look for the log4j.properties that shipped with OpenCms
95
URL JavaDoc url = Loader.getResource("log4j.properties");
96             if (url != null) {
97                 // found some log4j properties, let's see if these are the ones used by OpenCms
98
String JavaDoc path = CmsFileUtil.normalizePath(url, '/');
99                 // in a default OpenCms configuration, the following path would point to the OpenCms "WEB-INF" folder
100
String JavaDoc webInfPath = CmsResource.getParentFolder(CmsResource.getFolderPath(path));
101                 // check for the OpenCms configuration file
102
String JavaDoc configFilePath = webInfPath
103                     + CmsSystemInfo.FOLDER_CONFIG
104                     + CmsConfigurationManager.DEFAULT_XML_FILE_NAME;
105                 File JavaDoc configFile = new File JavaDoc(configFilePath);
106                 if (configFile.exists()) {
107                     // assume this is a default OpenCms log configuration
108
ExtendedProperties configuration = new ExtendedProperties(path);
109                     // check if OpenCms should set the log file environment variable
110
boolean setLogFile = configuration.getBoolean("opencms.set.logfile", false);
111                     if (setLogFile) {
112                         // set "opencms.log" variable
113
String JavaDoc logFilePath = CmsFileUtil.normalizePath(webInfPath + FOLDER_LOGS + FILE_LOG, '/');
114                         File JavaDoc logFile = new File JavaDoc(logFilePath);
115                         m_logFileRfsPath = logFile.getAbsolutePath();
116                         System.setProperty("opencms.logfile", m_logFileRfsPath);
117                         // re-read the configuration with the new environment variable available
118
PropertyConfigurator.configure(path);
119                     }
120                 }
121                 // can't localize this message since this would end in an endless logger init loop
122
INIT.info(". Log4j config file : " + path);
123             }
124         } catch (SecurityException JavaDoc e) {
125             // ignore, may be caused if environment can't be written
126
} catch (Exception JavaDoc e) {
127             // unexpected but nothing we can do about it, print stack trace and continue
128
e.printStackTrace(System.err);
129         }
130     }
131
132     /**
133      * Returns the log for the selected object.<p>
134      *
135      * If the provided object is a String, this String will
136      * be used as channel name. Otherwise the objects
137      * class name will be used as channel name.<p>
138      *
139      * @param obj the object channel to use
140      * @return the log for the selected object channel
141      */

142     public static Log getLog(Object JavaDoc obj) {
143
144         if (obj instanceof String JavaDoc) {
145             return LogFactory.getLog((String JavaDoc)obj);
146         } else if (obj instanceof Class JavaDoc) {
147             return LogFactory.getLog((Class JavaDoc)obj);
148         } else {
149             return LogFactory.getLog(obj.getClass());
150         }
151     }
152
153     /**
154      * Returns the filename of the logfile (in the "real" file system).<p>
155      *
156      * If the method returns <code>null</code>, this means that the log
157      * file is not managed by OpenCms.<p>
158      *
159      * @return the filename of the logfile (in the "real" file system)
160      */

161     protected static String JavaDoc getLogFileRfsPath() {
162
163         return m_logFileRfsPath;
164     }
165 }
Popular Tags