自定义日志输出格式


package com.abc.xxk.common.config; //import ch.qos.logback.classic.spi.ILoggingEvent; import ch.qos.logback.core.CoreConstants; import ch.qos.logback.core.LayoutBase; import com.abc.xxk.common.utils.UUIDUtils; import com.abc.xxk.common.utils.thread.ThreadLocalUtil; import java.time.LocalDateTime; import java.time.LocalTime; import java.time.format.DateTimeFormatter; import java.time.format.FormatStyle; import java.util.UUID; public class LoggingLayoutConfig extends LayoutBase {private static final String space = " "; private static final String enter = "\r\n"; @Override public String doLayout(ILoggingEvent event) { StringBuilder sb = new StringBuilder(); if (null != event && null != event.getMDCPropertyMap()) { if (ThreadLocalUtil.getUid() == null) { ThreadLocalUtil.setUid(UUIDUtils.generateUUID()); } //2020-02-26 11:39:28.015 [http-nio-8001-exec-3] INFOc.sdworan.xxk.common.interceptor.CommonInterceptor [50] - /shiro/authenticationError; JSESSIONID=f277d4a62b9344ac918ab32340b29a0e: 进入的时间:2020-02-26 11:39:28 String nowDate = DateTimeFormatter.ofPattern(CoreConstants.ISO8601_PATTERN).format(LocalDateTime.now()); sb.append(CoreConstants.CURLY_LEFT); sb.append("workOrder"); sb.append(CoreConstants.DASH_CHAR); sb.append(nowDate); sb.append(CoreConstants.DASH_CHAR); sb.append(ThreadLocalUtil.getUid()); sb.append(CoreConstants.CURLY_RIGHT); sb.append(space); sb.append("["); sb.append(event.getThreadName()); sb.append("]"); sb.append(space); sb.append(event.getLevel()); sb.append(space); sb.append(event.getLoggerName()); sb.append(space); sb.append(CoreConstants.DASH_CHAR); sb.append(space); sb.append(event.getFormattedMessage()); sb.append(enter); }return sb.toString(); } }

【自定义日志输出格式】参照 http://logback.qos.ch/manual/layouts.html#conversionWord

    推荐阅读