log4j 日志路径配置

时间:2022-12-22 18:24:58

log4j.properties代码:

# Set root category priority to INFO and its only appender to CONSOLE.
#log4j.rootCategory=ERROR, LOGFILE
log4j.rootCategory=INFO, CONSOLE, LOGFILE
log4j.debug=true

# Set the enterprise logger category to FATAL and its only appender to CONSOLE.
log4j.logger.org.apache.axis.enterprise=INFO, CONSOLE

# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.Threshold=INFO
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=- %m%n

# LOGFILE is set to be a File appender using a PatternLayout.
log4j.appender.LOGFILE=org.apache.log4j.RollingFileAppender
log4j.appender.LOGFILE.File=${webappRoot}/log/AllLog.log

# \u5728windows\u5e73\u53f0\u53ef\u4ee5\u4e0d\u52a0\u540e\u7f00\u65e5\u5fd7\u4e5f\u80fd\u751f\u6210\uff0c
# \u4f46\u5728linix\u5e73\u53f0\u4e0b\u9700\u8981\u52a0\u540e\u7f00\u3002

log4j.appender.LOGFILE.Append=true
log4j.appender.LOGFILE.Threshold=ERROR
log4j.appender.LOGFILE.MaxFileSize=10000KB
log4j.appender.LOGFILE.MaxBackupIndex=1000
log4j.appender.LOGFILE.layout=org.apache.log4j.PatternLayout
log4j.appender.LOGFILE.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss} [%c]-[%p] %m%n

web.xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

	<display-name>csgbi</display-name>
	<description>csgbi</description>

	<!-- 设置Spring组配置文件 -->
	<context-param>
		<param-name>
	         contextConfigLocation
		</param-name>
		<param-value>
			/WEB-INF/config/spring/applicationContext.xml
		</param-value>
	</context-param>

    <!-- 服务信息初始化监听器 -->
	<listener>
		<listener-class>com.comtop.csgbi.jj.common.listener.EnviromentInfoListener</listener-class>
	</listener>

	<!-- Spring容器初始化监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

   <!-- struts2置文件 -->
	<filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
	</filter>
	<filter-mapping>
	        <filter-name>struts2</filter-name>
	        <url-pattern>/*</url-pattern>
	</filter-mapping>

    <servlet>
		<servlet-name>log4j config file</servlet-name>
		<servlet-class>com.comtop.csgbi.jj.common.listener.Log4jInitServlet</servlet-class>
		<init-param>
			<param-name>configfile</param-name>
			<param-value>/WEB-INF/log4j.properties</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

    <!-- session失效时间 -->
	<session-config>
		<session-timeout>60</session-timeout>
	</session-config>

	<jsp-config>
		<taglib>
			<taglib-uri>http://www.apache.org/struts-tags</taglib-uri>
			<taglib-location>/WEB-INF/config/struts/struts-tags.tld</taglib-location>
		</taglib>
	</jsp-config>

	<!-- 错误/异常页面 -->
	<error-page>
		<error-code>500</error-code>
		<location>/csgbi/component/common/error.jsp</location>
	</error-page>
	<error-page>
		<error-code>404</error-code>
		<location>/csgbi/component/common/404.jsp</location>
	</error-page>
	<error-page>
		<exception-type>java.lang.Throwable</exception-type>
		<location>/csgbi/component/common/error.jsp</location>
	</error-page>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

Log4jInitServlet.java代码:

package com.test;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.apache.log4j.PropertyConfigurator;

public class Log4jInitServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void init() throws ServletException {
		System.setProperty("webappRoot", getServletContext().getRealPath("/"));
		PropertyConfigurator.configure(getServletContext().getRealPath("/")
				+ getInitParameter("configfile"));
	}
}