作者:fionafongkaian | 来源:互联网 | 2023-10-11 20:08
问题:
我的日志打印结果是这样的:
[main] INFO Test - 接口入参===
我想得到一个类似于这样的日志:f9e287fad9e84cff8b2c2f2ed92adbe6[main] INFO Test - 接口入参===
1.我的测试类
import org.apache.log4j.MDC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Test {
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| static Logger logger = LoggerFactory.getLogger(Test.class);
public static void main(String[] args) {
MDC.clear();
MDC.put("logid", "f9e287fad9e84cff8b2c2f2ed92adbe6");
MDC.put("merchant_id", 1);
MDC.put("siteName", "北京");
MDC.put("userName", "userwyh");
MDC.put("mobile", "110");
MDC.put("mchId", 12);
MDC.put("mchName", "商户名称");
logger.info("接口入参===");
} |
2.我的log4j配置文件:
log4j.rootLogger=INFO, stdout
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{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n
log4j.logger.com.rong360.crawler=info,console,info,warn,error,fatal
logging.path=e:/logs/crawler-meituan/logs
log4j.appender.cOnsole=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.console.Threshold=INFO
log4j.appender.debug=com.rong360.crawler.log4j.Rong360DailyRollingFileAppender
log4j.appender.debug.layout=org.apache.log4j.PatternLayout
log4j.appender.debug.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.debug.Threshold = DEBUG
log4j.appender.debug.append=true
log4j.appender.debug.File=debug.log
log4j.appender.info=com.rong360.crawler.log4j.Rong360DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout
log4j.appender.info.append=true
log4j.appender.info.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.info.Threshold = INFO
log4j.appender.info.File=info.log
log4j.appender.warn=com.rong360.crawler.log4j.Rong360DailyRollingFileAppender
log4j.appender.warn.layout=org.apache.log4j.PatternLayout
log4j.appender.warn.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.warn.Threshold = WARN
log4j.appender.warn.append=true
log4j.appender.warn.File=warn.log
log4j.appender.error=com.rong360.crawler.log4j.Rong360DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout
log4j.appender.error.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.error.Threshold = ERROR
log4j.appender.error.append=true
log4j.appender.error.File=error.log
log4j.appender.fatal=com.rong360.crawler.log4j.Rong360DailyRollingFileAppender
log4j.appender.fatal.layout=org.apache.log4j.PatternLayout
log4j.appender.fatal.layout.COnversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%p] [%l] [%t:%r] [%X{logid}] [%X{ip}] [%X{uri}] [%X{merchant_id}] [%X{user_id}] %m%n
log4j.appender.fatal.Threshold = FATAL
log4j.appender.fatal.append=true
log4j.appender.fatal.File=fatal.log