Use MDC to add user or tracking code to logs __MDC

Source: Internet
Author: User
Tags cos system log log4j
Log4j.rootlogger=error,console

Log4j.appender.console=org.apache.log4j.consoleappender
Log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=%d [%t]%-5p-%c#%m%x-%m (%r ms)%n

Log4j.logger.com.company.project=debug

#SqlMap Logging configuration ...
Log4j.logger.com.ibatis=debug
Log4j.logger.com.ibatis.common.jdbc.simpledatasource=debug
Log4j.logger.com.ibatis.sqlmap.engine.cache.cachemodel=debug
Log4j.logger.com.ibatis.sqlmap.engine.impl.sqlmapclientimpl=debug
Log4j.logger.com.ibatis.sqlmap.engine.builder.xml.sqlmapparser=debug
Log4j.logger.com.ibatis.common.util.stopwatch=debug
Log4j.logger.java.sql.connection=debug
Log4j.logger.java.sql.statement=debug
Log4j.logger.java.sql.preparedstatement=debug
Log4j.logger.java.sql.resultset=debug

Log4j.logger.org.apache.struts.action.requestprocessor=error
#log4j. Logger.org.apache.commons.beanutils.propertyutils=debug
#log4j. Logger.org.apache.commons.beanutils.beanutils=debug
#log4j. Logger.org.tuckey.web.filters.urlrewrite.urlrewritefilter=debug
#log4j. Logger.org.apache.struts.action=debug
#log4j. Logger.org.apache.commons.beanutils=debug
#log4j. Logger.org.springframework.orm.ibatis=debug
#log4j. Logger.org.springframework.jdbc=debug

How to use log4j to record the operator's login ID:When logging, we may have this requirement, for each record in addition to know the log information, but also the current logged-in user information. Can: Use the log4j MDC to solve this problem.
1, through the filter or by itself in the action through the Command Mdc.put ("UserId", Default_userid), set the value to the MDC. 2, the user-set value is extracted from [%x{userid}] in the log4j configuration file and exported to the log, as follows log4j.appender.layout.conversionpattern=%d [%t]%-5p-%c#%m%x-%m (%r ms)%n [%x{userid}]

Slf4j

When the service enters

Mdc.put ("Tracenum", Costlogtrackdto.gettrackno ());

When the service exits

Mdc.remove ("Tracenum");

Log configuration

<?xml version= "1.0" encoding= "UTF-8"?>
<configuration debug= "false" >
<property name= "Log.base" value= "."/>
<appender name= "Connect.stdout" class= "Ch.qos.logback.core.ConsoleAppender" >
<encoder charset= "UTF-8" > <!--encoder can specify the character set, which is meaningful for Chinese output-->
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%level] [%x{tracenum}] [%logger:%line]-%msg%n
</pattern>
</encoder>
</appender>
<appender name= "Connect.file"
class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<file>log/connect.log</file>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>DEBUG</level>
</filter>
<rollingpolicy class= "Ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
<fileNamePattern>log/connect.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringpolicy
class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder charset= "UTF-8" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%level] [%x{tracenum}] [%logger:%line]-%msg%n</pattern>
</encoder>
</appender>
<appender name= "Connect.exception.file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<file>log/connectException.log</file>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>WARN</level>
</filter>
<rollingpolicy class= "Ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
<fileNamePattern>log/connectException.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringpolicy
class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder charset= "UTF-8" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%level] [%x{tracenum}] [%logger:%l]-%msg%n</pattern>
</encoder>
</appender>
<appender name= "Eaibf.file" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<file>log/eaibapi.log</file>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>DEBUG</level>
</filter>>
<rollingpolicy class= "Ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
<fileNamePattern>log/eaibapi.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>12</maxIndex>
</rollingPolicy>
<triggeringpolicy class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder charset= "UTF-8" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%level][%x{tracenum}][%logger]-%msg%n</pattern>
</encoder>
</appender>

<appender name= "Cos_wtc.file"
class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
<file>log/cos_wtc.log</file>
<filter class= "Ch.qos.logback.classic.filter.ThresholdFilter" >
<level>DEBUG</level>
</filter>
<rollingpolicy class= "Ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
<fileNamePattern>log/cos_wtc.log.%i</fileNamePattern>
<minIndex>1</minIndex>
<maxIndex>10</maxIndex>
</rollingPolicy>
<triggeringpolicy
class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
<maxFileSize>50MB</maxFileSize>
</triggeringPolicy>
<encoder charset= "UTF-8" >
<pattern>%d{yyyy-mm-dd HH:mm:ss. SSS} [%level] [%x{tracenum}] [%logger]-%msg%n</pattern>
</encoder>
</appender>
<logger name= "Com.bizprocess.main.cosh.handler" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Connect.file"/>
</logger>
<logger name= "Com.common.eaib.handler" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Connect.file"/>
</logger>
<logger name= "com.eai.security" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Eaibf.file"/>
</logger>
<logger name= "Com.eai.api.EAIBDataProcessor" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Eaibf.file"/>
</logger>
<logger name= "Com.eai.pack" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Eaibf.file"/>
</logger>
<logger name= "Java.sql.PreparedStatement" additivity= "false" >
<level value= "WARN"/>
</logger>
<logger name= "Java.sql.Connection" additivity= "false" >
<level value= "WARN"/>
</logger>
<logger name= "Java.sql.Statement" additivity= "false" >
<level value= "WARN"/>
</logger>
<logger name= "Org.springframework" additivity= "false" >
<level value= "WARN"/>
</logger>

<!--COS_WTC log output-->
<logger name= "COM.WTC.EJB" level= "DEBUG" additivity= "false" >
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Cos_wtc.file"/>
</logger>

<!--output COS system log-->
<logger name= "Com.connect" level= "INFO" additivity= "false" >
<!--make sure to remove the connect.stdout configuration, otherwise the log will hit the console-->
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Connect.file"/>
<appender-ref ref= "Connect.exception.file"/>
</logger>

<!--output COS system log-->
<logger name= "com" level= "INFO" additivity= "false" >
<!--make sure to remove the connect.stdout configuration, otherwise the log will hit the console-->
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Connect.file"/>
<appender-ref ref= "Connect.exception.file"/>
</logger>
<root>
<level value= "INFO"/>
<!--make sure to remove the connect.stdout configuration, otherwise the log will hit the console-->
<appender-ref ref= "Connect.stdout"/>
<appender-ref ref= "Connect.file"/>
<appender-ref ref= "Connect.exception.file"/>
</root>
</configuration>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.