graylog添加自定义日志参数
1.logback.xml日志文件配置
<configuration>
<appender name="consoleLog" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>
%d - %msg%n
</pattern>
</layout>
</appender>
<appender name="GELF" class="de.siegmar.logbackgelf.GelfUdpAppender">
<graylogHost></graylogHost>
<graylogPort>12201</graylogPort>
<maxChunkSize>508</maxChunkSize>
<useCompression>true</useCompression>
<encoder class="de.siegmar.logbackgelf.GelfEncoder">
<!-- <originHost>localhost</originHost>-->
<includeRawMessage>false</includeRawMessage>
<includeMarker>true</includeMarker>
<includeMdcData>true</includeMdcData>
<includeCallerData>false</includeCallerData>
<includeRootCauseData>false</includeRootCauseData>
<includeLevelName>true</includeLevelName>
<shortPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d - %m%nopex</pattern>
</shortPatternLayout>
<fullPatternLayout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d - %m%n</pattern>
</fullPatternLayout>
<staticField>app_name:ad-server</staticField>
<staticField>os_arch:${os.arch}</staticField>
<staticField>os_name:${os.name}</staticField>
<staticField>os_version:${os.version}</staticField>
<staticField>uri:%X{uri}</staticField>
<staticField>uid:%X{uid}</staticField>
<staticField>ip:%X{ip}</staticField>
</encoder>
</appender>
<!-- logger 是用来设置某一个包或者具体某一个类的日志打印级别, 以及指定的 appender -->
<logger name="com.plp.boling" level="info" additivity="false">
<appender-ref ref="GELF" />
</logger>
</configuration>
添加自定义日志参数
<staticField>uri:%X{uri}</staticField>
<staticField>uid:%X{uid}</staticField>
<staticField>ip:%X{ip}</staticField>
2.在对应的类中使用slf4j中的MDC写入信息到日志
String requestURI = req.getRequestURI();
MDC.put("uri", requestURI);