使用log4j MDC实现日志追踪
log4j mdc实现日志追踪
mdc 中包含的可以被同一线程中执行的代码所访问内容。当前线程的子线程会继承其父线程中的 mdc 的内容。记录日志时,只需要从 mdc 中获取所需的信息即可。
作用:
使用mdc来记录日志,可以规范多开发下日志格式。
1、新建线程处理类 threadcontext
import java.io.serializable; import java.util.hashmap; import java.util.map; import java.util.optional; /** * 线程上下文 * * @date 2017年3月1日 * @since 1.0.0 */ public class threadcontext { /** * 线程上下文变量的持有者 */ private final static threadlocal<map<string, object>> ctx_holder = new threadlocal<map<string, object>>(); static { ctx_holder.set(new hashmap<string, object>()); } /** * traceid */ private final static string trace_id_key = "traceid"; /** * 会话id */ private final static string session_key = "token"; /** * 操作用户id */ private final static string visitor_id_key = "userid"; /** * 操作用户名 */ private final static string visitor_name_key = "username"; /** * 客户端ip */ private static final string client_ip_key = "clientip"; /** * 添加内容到线程上下文中 * * @param key * @param value */ public final static void putcontext(string key, object value) { map<string, object> ctx = ctx_holder.get(); if (ctx == null) { return; } ctx.put(key, value); } /** * 从线程上下文中获取内容 * * @param key */ @suppresswarnings("unchecked") public final static <t extends object> t getcontext(string key) { map<string, object> ctx = ctx_holder.get(); if (ctx == null) { return null; } return (t) ctx.get(key); } /** * 获取线程上下文 */ public final static map<string, object> getcontext() { map<string, object> ctx = ctx_holder.get(); if (ctx == null) { return null; } return ctx; } /** * 删除上下文中的key * * @param key */ public final static void remove(string key) { map<string, object> ctx = ctx_holder.get(); if (ctx != null) { ctx.remove(key); } } /** * 上下文中是否包含此key * * @param key * @return */ public final static boolean contains(string key) { map<string, object> ctx = ctx_holder.get(); if (ctx != null) { return ctx.containskey(key); } return false; } /** * 清空线程上下文 */ public final static void clean() { ctx_holder.remove(); } /** * 初始化线程上下文 */ public final static void init() { ctx_holder.set(new hashmap<string, object>()); } /** * 设置traceid数据 */ public final static void puttraceid(string traceid) { putcontext(trace_id_key, traceid); } /** * 获取traceid数据 */ public final static string gettraceid() { return getcontext(trace_id_key); } /** * 设置会话的用户id */ public final static void putuserid(integer userid) { putcontext(visitor_id_key, userid); } /** * 设置会话的用户id */ public final static int getuserid() { integer val = getcontext(visitor_id_key); return val == null ? 0 : val; } /** * 设置会话的用户名 */ public final static void putusername(string username) { putcontext(visitor_name_key, username); } /** * 获取会话的用户名称 */ public final static string getusername() { return optional.ofnullable(getcontext(visitor_name_key)) .map(name -> string.valueof(name)) .orelse(""); } /** * 取出ip * * @return */ public static final string getclientip() { return getcontext(client_ip_key); } /** * 设置ip * * @param ip */ public static final void putclientip(string ip) { putcontext(client_ip_key, ip); } /** * 设置会话id * * @param token */ public static void putsessionid(string token) { putcontext(session_key, token); } /** * 获取会话id * * @param token */ public static string getsessionid(string token) { return getcontext(session_key); } /** * 清空会话数据 */ public final static void removesessionid() { remove(session_key); } }
2、添加工具类traceutil
import java.util.uuid; import org.slf4j.mdc; import threadcontext; /** * trace工具 * * @date 2017年3月10日 * @since 1.0.0 */ public class traceutil { public static void tracestart() { threadcontext.init(); string traceid = generatetraceid(); mdc.put('traceid', traceid); threadcontext.puttraceid(traceid); } public static void traceend() { mdc.clear(); threadcontext.clean(); } /** * 生成跟踪id * * @return */ private static string generatetraceid() { return uuid.randomuuid().tostring(); } }
3、添加contextfilter
对于每个请求随机生成requestid并放入mdc
import java.io.ioexception; import javax.servlet.filterchain; import javax.servlet.servletexception; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import org.springframework.core.ordered; import org.springframework.core.annotation.order; import org.springframework.web.filter.onceperrequestfilter; import com.pingan.manpan.common.util.traceutil; import com.pingan.manpan.user.dto.threadcontext; import com.pingan.manpan.web.common.surpport.iputils; /** * 上下文filter * * @date 2017/3/10 * @since 1.0.0 */ //@order 标记组件的加载顺序 @order(ordered.highest_precedence) public class contextfilter extends onceperrequestfilter { @override protected void dofilterinternal(httpservletrequest request, httpservletresponse response, filterchain filterchain) throws servletexception, ioexception { try { threadcontext.putclientip(iputils.getclientip(request)); traceutil.tracestart(); filterchain.dofilter(request, response); } finally { traceutil.traceend(); } } }
4、在webconfiguriation注册filter
/** * 请求上下文,应该在最外层 * * @return */ @bean public filterregistrationbean requestcontextrepositoryfilterregistrationbean() { filterregistrationbean filterregistrationbean = new filterregistrationbean(); filterregistrationbean.setfilter(new contextfilter()); filterregistrationbean.addurlpatterns("/*"); return filterregistrationbean; }
5、修改log4j日志配置文件,设置日志traceid
<?xml version="1.0" encoding="utf-8"?> <configuration> <jmxconfigurator/> <property name="log_level_pattern" value="%x{traceid:-} %5p"/> <property name="log_file" value="${log_path}/web.logx"/> <property name="log_file_suffix" value=".logx"/> <include resource="org/springframework/boot/logging/logback/defaults.xml"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml"/> <appender name="file" class="ch.qos.logback.core.rolling.rollingfileappender"> <encoder> <pattern>${file_log_pattern}</pattern> </encoder> <file>${log_file}${log_file_suffix}</file> <rollingpolicy class="ch.qos.logback.core.rolling.timebasedrollingpolicy"> <filenamepattern>${log_file}.%d{yyyy-mm-dd}${log_file_suffix}</filenamepattern> </rollingpolicy> </appender> <appender name="syslog" class="ch.qos.logback.classic.net.syslogappender"> <sysloghost>127.0.0.1</sysloghost> <facility>local6</facility> <port>514</port> <suffixpattern>${file_log_pattern}</suffixpattern> </appender> <logger name="druid.sql" level="debug" /> <root level="info"> <appender-ref ref="console"/> <appender-ref ref="file"/> <appender-ref ref="syslog"/> </root> </configuration>
log4j2实现日志跟踪
日志跟踪
在每条日志前添加一个随机字符串并且确保同一个请求的字符串相同。如下:c6019df137174d2b98631474db4156b7为此次请求的标识。通过次标识可以查询到所有该请求的日志信息
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:204]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:204]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:205]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:205]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:209]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:214]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:223]-[http-nio-8803-exec-4]-
[traceid:c6019df137174d2b98631474db4156b7]-[2020-08-11 19:56:58:224]-[http-nio-8803-exec-4]-
同时可以将此标识返回给前端,便于问题查询。traceid: c6019df137174d2b98631474db4156b7
access-control-allow-credentials: true access-control-allow-origin: http://test.page.qingin.cn cache-control: max-age=30 connection: keep-alive content-type: application/json;charset=utf-8 date: tue, 11 aug 2020 12:02:19 gmt expires: tue, 11 aug 2020 12:02:49 gmt server: nginx/1.16.1 traceid: c6019df137174d2b98631474db4156b7 transfer-encoding: chunked vary: origin vary: access-control-request-method vary: access-control-request-headers
我们可以通过过滤器实现以上的功能
log4j2filter.java
package com.generator.admin.filter; import org.apache.logging.log4j.threadcontext; import org.slf4j.logger; import org.slf4j.loggerfactory; import javax.servlet.*; import javax.servlet.http.httpservletrequest; import javax.servlet.http.httpservletresponse; import java.io.ioexception; import java.util.uuid; /** * @calssname log4j2filter * @description 对用户的请求添加日志编号,并将此编号返回给前端,便于日志查询 */ @webfilter(filtername = "log4j2filter", urlpatterns = "/*", initparams = {@webinitparam(name = "description", value = "这是log4j2filter过滤器")}) public class log4j2filter implements filter { private string description; public static final string trace_id = "traceid"; private static final logger logger = loggerfactory.getlogger(log4j2filter.class); @override public void init(filterconfig filterconfig) throws servletexception { description = filterconfig.getinitparameter("description"); system.out.println("过滤器初始化:"+ description); } @override public void dofilter(servletrequest servletrequest, servletresponse servletresponse, filterchain filterchain) throws ioexception,servletexception { httpservletrequest req = (httpservletrequest) servletrequest; httpservletresponse resp = (httpservletresponse) servletresponse; // 生成一个随机数给到前端 string traceid = uuid.randomuuid().tostring().replace("-", ""); // 随机数放到此线程的上下文中,可以在每条日志前加入。具体看下面log4j2.xml threadcontext.put(trace_id, traceid); // 随机数放到header中,在response headers中可查看到此数据 resp.addheader(trace_id, traceid); filterchain.dofilter(req, resp); threadcontext.clearall(); } @override public void destroy() { system.out.println("过滤器,被销毁:"+ description); } }
log4j2.xml <patternlayout pattern="[traceid:%x{traceid}]-[%d{yyyy-mm-dd hh:mm:ss:sss}]-[%t]-[%p]-[%l]-%m%n"/>
<?xml version="1.0" encoding="utf-8"?> <!--设置log4j2的自身log级别为warn--> <!--日志级别以及优先级排序: off > fatal > error > warn > info > debug > trace > all --> <!--configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时,你会看到log4j2内部各种详细输出--> <!--monitorinterval:log4j能够自动检测修改配置 文件和重新配置本身,设置间隔秒数--> <configuration status="warn" monitorinterval="30"> <!--全局参数--> <properties> <property name="logpath">logs</property> </properties> <!--先定义所有的appender--> <appenders> <!--这个输出控制台的配置--> <console name="console" target="system_out"> <!-- traceid:就是在过滤器中生成的随机数 --> <patternlayout pattern="[traceid:%x{traceid}]-[%d{yyyy-mm-dd hh:mm:ss:sss}]-[%t]-[%p]-[%l]-%m%n"/> </console> </appenders> <!--然后定义logger,只有定义了logger并引入的appender,appender才会生效--> <loggers> <!--过滤掉spring和mybatis的一些无用的debug信息--> <logger name="org.springframework" level="info"></logger> <logger name="org.mybatis" level="info"></logger> <logger name="com.zaxxer" level="warn"></logger> <!-- com.generator开发/测试环境用debug,并用控制台输出即可 --> <logger name="com.generator" level="debug" additivity="false"> <appender-ref ref="console"/> </logger> <!-- 未指定的包都按此 level 打印日志 --> <root level="debug"> <appender-ref ref="console"/> </root> </loggers> </configuration>
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。
推荐阅读
-
实现Nginx中使用PHP-FPM时记录PHP错误日志的配置方法
-
使用HTML5 Geolocation实现一个距离追踪器
-
使用PHP实现蜘蛛访问日志统计
-
Java日志Log4j或者Logback的NDC和MDC功能
-
tp5框架使用composer实现日志记录功能示例
-
Laravel框架使用monolog_mysql实现将系统日志信息保存到mysql数据库的方法
-
Java学习笔记 DbUtils数据库查询和log4j日志输出 使用
-
Python使用logging结合decorator模式实现优化日志输出的方法
-
ASP.NET Core 2.0 使用NLog实现日志记录
-
Yii使用DbTarget实现日志功能的示例代码