引入:
这篇文章主要快速的研究下opencms所采用的日志系统以及各自的log文件。
分析:
(1)安装opencms时的log
因为在opencms安装时候会自动调用$CATALINA_HOME/opencms/WEB-INF目录下的setup.bat(如果是UNIX机器则是setup.sh)
而这个执行脚本中,会配置真实执行的命令是
...rem Set standard command for invoking Java.set _RUNJAVA="%JAVA_HOME%\bin\java.exe"set MAINCLASS=org.opencms.setup.CmsAutoSetup...rem execute OPENCMS%_RUNJAVA% %JAVA_OPTS% -classpath "%OPENCMS_HOME%\classes%CLASSPATH%%OPENCMS_CP%%TOMCAT_LIB%" %MAINCLASS% %CMD_LINE_ARGS%
所以,它其实会去执行CmsAutoSetup类,而这个执行向导(wizard)实际是通过CmsAutoSetupBean来完成的:
public CmsAutoSetup(CmsAutoSetupProperties props) { m_props = props; m_bean = new CmsSetupBean(); m_bean.init(props.getSetupWebappPath(), props.getServeltMapping(), props.getSetupDefaultWebappName()); }
而CmsAutoSetupBean中定义了安装向导中的日志文件位置:
/** Location for log file. */ protected String m_logFile = CmsSystemInfo.FOLDER_WEBINF + CmsLog.FOLDER_LOGS + "setup.log"; /** Location for logs relative to the webapp folder. */ protected String m_logsFolder = CmsSystemInfo.FOLDER_WEBINF + CmsLog.FOLDER_LOGS;
可以看出这个m_logFile的位置是在 $CATALINA_HOME/webapps/opencms/WEB-INF/logs目录下的setup.log.
(2)运行时的Log
因为org.opencms.main.OpenCms类提供了Opencms系统的运行时,它的getLog()方法会调用CmsLog类的静态方法getLog()方法。我们查看了CmsLog类,可以发现它的静态初始块中有如下代码:
/** * Initializes the OpenCms logger configuration.*/ static { try { // look for the log4j.properties that shipped with OpenCms URL url = Loader.getResource("log4j.properties"); if (url != null) { // found some log4j properties, let's see if these are the ones used by OpenCms String path = CmsFileUtil.normalizePath(url, '/'); // in a default OpenCms configuration, the following path would point to the OpenCms "WEB-INF" folder String webInfPath = CmsResource.getParentFolder(CmsResource.getFolderPath(path)); // check for the OpenCms configuration file // check for the OpenCms tld file String tldFilePath = webInfPath + CmsSystemInfo.FILE_TLD; File tldFile = new File(tldFilePath); if (tldFile.exists()) { // assume this is a default OpenCms log configuration CmsParameterConfiguration configuration = new CmsParameterConfiguration(path); // check if OpenCms should set the log file environment variable boolean setLogFile = configuration.getBoolean("opencms.set.logfile", false); if (setLogFile) { // set "opencms.log" variable String logFilePath = CmsFileUtil.normalizePath(webInfPath + FOLDER_LOGS + FILE_LOG, '/'); File logFile = new File(logFilePath); m_logFileRfsPath = logFile.getAbsolutePath(); m_logFileRfsFolder = CmsFileUtil.normalizePath(logFile.getParent() + '/', File.separatorChar); System.setProperty("opencms.logfile", m_logFileRfsPath); System.setProperty("opencms.logfolder", m_logFileRfsFolder); // re-read the configuration with the new environment variable available PropertyConfigurator.configure(path); } } // can't localize this message since this would end in an endless logger init loop INIT.info(". Log4j config file : " + path); } ... }
这就说明,它会在配置文件log4j.properties中检查key为opencms.set.logfile对应的value,如果是true,则会把运行时的log文件设置在WEB-INF/logs目录下的opencms.log文件。
而log4j.properties中已经把这个开关设为true 了:
# OpenCms provides a special variable ${opencms.logfile} to the environment, which contains# the log file path. The location of this file is calculated relative to this # "log4j.properties" file on OpenCms startup. If this file is located in the folder "${classes}",# then the log is written to "${classes}../logs/opencms.log". # To disable this mechanism, you must set ${opencms.set.logfile} to "false". In this case # you must configure the log output file manually.opencms.set.logfile=true
所以我们明白,运行时的日志,都存在了$CATALINA_HOME/webapps/opencms/WEB-INF/logs目录下的opencms.log.