欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

log4j自定义日志

程序员文章站 2022-05-24 15:28:07
...

log4j自定义日志,输出内容加IP地址

log4j.properties

log4j.rootLogger=stdout,error,info

#\u8F93\u51FA\u5230\u63A7\u5236\u53F0
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p [%t] %C.%M(%L) | %m%n

#\u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230\u6587\u4EF6
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %p [%t] %C.%M(%L) | %m%n
log4j.appender.debug.File=d:/log/debug.txt
log4j.appender.debug.DatePattern=','yyyy-MM-dd
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.Append=true
log4j.appender.debug.Encoding=UTF-8

#\u8F93\u51FADEBUG\u7EA7\u522B\u4EE5\u4E0A\u7684\u65E5\u5FD7\u5230\u6587\u4EF6
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS}[%X{ip}] %p [%t] %C.%M(%L) | %m%n
log4j.appender.error.File=d:/log/error.log
log4j.appender.error.DatePattern=','yyyy-MM-dd
log4j.appender.error.Threshold=ERROR
log4j.appender.error.Append=true
log4j.appender.error.Encoding=UTF-8


log4j.logger.mail_gm=info,mail_gm
log4j.additivity.mail_gm = false
log4j.appender.mail_gm=org.apache.log4j.DailyRollingFileAppender
log4j.appender.mail_gm.layout=org.apache.log4j.PatternLayout
log4j.appender.mail_gm.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss}[%X{ip}] - %m%n
log4j.appender.mail_gm.File=d:/log/gm.log
log4j.appender.mail_gm.datePattern='.'yyyy-MM-dd  
log4j.appender.mail_gm.Threshold = INFO   
log4j.appender.mail_gm.append=true 
log4j.appender.mail_gm.Encoding=UTF-8




类中调用

private static Logger loggerGM = Logger.getLogger("gm");

loggerGM.info("123123");

内容中输出ip地址

log4j自定义日志
1.web.xml文件中配置filter

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
	version="3.0">
	<display-name>centralOperations</display-name>
	<!-- Spring和mybatis的配置文件 -->
	<context-param>  
    <param-name>webAppRootKey</param-name>  
    <param-value>account.root</param-value>  
	</context-param>  
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-mybatis.xml</param-value>
	</context-param>
	<context-param>
		<param-name>log4jConfigLocation</param-name>
		<param-value>classpath:log4j.properties</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
	</listener>
	<!-- 编码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- Log4jFilter -->
	  <filter>
         <filter-name>Log4jFilter</filter-name>
        <filter-class>com.full.interceptor.Log4jFilter</filter-class>
     </filter>
     <filter-mapping>
         <filter-name>Log4jFilter</filter-name>
                  <url-pattern>/*</url-pattern>
    </filter-mapping>
	
	<!-- Spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止Spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>

	<!-- Spring MVC servlet -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<!-- 此处可以可以配置成*.do,对应struts的后缀习惯 -->
		<url-pattern>*.action</url-pattern>
	</servlet-mapping>
	<welcome-file-list>
		<welcome-file>/index.jsp</welcome-file>
	</welcome-file-list>

</web-app>

2.过滤器类

package com.full.interceptor;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.MDC;

public class Log4jFilter implements Filter{

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
			throws IOException, ServletException {
		String ip = request.getRemoteAddr();
		MDC.put("ip", ip);
		chain.doFilter(request, response);
		MDC.remove("ip");
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

}

打印日志格式
log4j自定义日志

相关标签: log4j