What MDC that?
First contact with the MDC, very Mongolian circle. Take a look at the package import org.slf4j.Logger imported; import org.slf4j.LoggerFactory; import org.slf4j.MDC; it is clearly relevant and log management.
slf4j common interface to log what? trace, debug, info, warn, error, coupled with the MDC.
The most common log management will in this way:
private static final Logger log = LoggerFactory.getLogger(类名.class);
log.info();
log.debug();
log.trace();
How to configure the log management application
Output log format:
<Property name = "the PATTERN">
% {X-LogID}, [% - 5level],% D {the MM-dd-YYYY HH: mm: ss.SSS},% T, C {%}. 1 :%% n-MSG
</ Property>
Analysis:
- % X {logId} ---- for MDC.put ( "", "") method.
- Level [% -5level] ---- logs
- % D {yyyy-MM-dd HH: mm: ss.SSS} ----- Time Format
- % T ----- thread
- % C {1} --- class name
- % Msg --- news
- % N ------ newline
See profile
<? Xml Version = "1.0" encoding = "UTF-8" ?> <-! Status = "OFF", can be removed, whether it means to record information log4j2 event itself, the default is OFF -> < the Configuration > < Properties > <-! file output format -> < Property name = "the PATTERN" > %X{logId},[%-5level],%d{yyyy-MM-dd HH:mm:ss.SSS},%t,%c{1}: %msg%n </property> <! - log root path -> < Property name = "BASE_PATH" > /applog/xxxlog </property> <! - Application name -> < Property name = "APP_NAME" > xxx-core </property> <! - log file name -> < Property name = "LOG_FILE_NAME" > ${BASE_PATH}/${APP_NAME}.log </property> <! - rolling log file generation rules -> < Property name = "LOG_FILE_PATTERN" > ${BASE_PATH}/${APP_NAME}_%d{yyyyMMddHHmm}-%i.log </property> </properties> <!-- xx报文日志级别 --> <DynamicThresholdFilter key="traceOpen" defaultThreshold="ERROR" onMatch="ACCEPT" onMismatch="NEUTRAL"> <KeyValuePair key="yes" value="trace" /> </DynamicThresholdFilter> <! - log output defined -> < the appenders in > <! - the rolling Log -> < RollingFile name = "rollingFileLog" filename = "$ {} LOG_FILE_NAME" filePattern = "$ {} LOG_FILE_PATTERN" the append = "to true" > < Filters > Filter out the log does not meet the level of the log <!-- <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" /> --> Only output level and higher-level information (onMatch), other direct denial (onMismatch) <!-- <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY" /> --> </Filters> Log output format <PatternLayout pattern="${PATTERN}" /> <Policies> <TimeBasedTriggeringPolicy /> <SizeBasedTriggeringPolicy size="20 MB" /> </Policies> Log rolling strategy < DefaultRolloverStrategy max = "50" > < the Delete the basePath = "$ {BASE_PATH} /" maxDepth = "2" > < IfFileName glob =. "$ {_} * Log APP_NAME" /> <-! ! Here must Age filePattern and coordination, which is accurate to HH, there have to be written xH, xd would be ineffective In addition, the best digital> 2, otherwise it may result in deletion of time, the most recent file is still in an occupied state, leading to delete unsuccessful! -> <! - 7 days -> < IfLastModified Age = "7D" /> < IfAny > < IfAccumulatedFileSize Exceeds = "GB 2" /> < IfAccumulatedFileCount Exceeds = "100" /> </ IfAny > </ the Delete > </ DefaultRolloverStrategy > </ RollingFile > <-! console -> <Console name="console" target="SYSTEM_OUT"> <PatternLayout pattern="${PATTERN}" /> </Console> </appenders> <!-- 日志对象定义 --> <loggers> <!-- xx日志 --> <logger name="CoreframeInteractiveLogger" level="info" additivity="true"> <AppenderRef ref="interactiveAppender" /> </logger> <!-- xx日志 --> <logger name="CoreframeServiceMetricsLogger" level="info" additivity="true"> <AppenderRef ref="serviceMetricsAppender" /> </logger> <-! Services Framework -> < Logger name = "CoreFrame" Level = "info" /> <! - Application logs -> < Logger name = "-Service" Level = "Debug" /> < Logger name = "" Level = "Debug" /> <!-- 测试日志 --> <logger name="test" level="trace" /> <logger name="" level="trace" /> <root level="info"> <AppenderRef ref="console" /> <AppenderRef ref="rollingFileLog" /> </root> </loggers> </configuration>