(005)Java>logback 日志按小时分割时,忽略夏令时

使用 MDC 线程变量

使用 MDC 线程变量,可以将时间作为参数传递 logback。但是通过默认的 discriminator 目前暂且只会定义一个自定义变量。

 public void SendUserObject(AwsLog awsLog) {
      if (awsLog.uid <= 0)
           return;

       String content = String.format("%s_%s_%s_%s\n%s", awsLog.code, awsLog.uid, awsLog.logType, awsLog.date, JSON.toJSONString(awsLog));
       MDC.put("AwsLogType", awsLog.logType.toString());
       MDC.put("test", DateUtils.getPatternHH_Without_DaylightSaving());
       itemLoggerService.log(content);
       if (CurrencyCode.JPY.toString().equals(awsLog.code) && awsLog.logType == AwsLogType.Buy) {
           jpyItemLoggerService.log(content);
       }
       MDC.remove("AwsLogType");
//        MDC.remove("curHour");
    }
<discriminator>
	<key>AwsLogTypekey>
	<defaultValue>nonedefaultValue>
discriminator>

用时区设置

  1. 时区的设置:

(005)Java>logback 日志按小时分割时,忽略夏令时_第1张图片

  1. 代码配置 GMT-08:00 :








<configuration scan="true" scanPeriod="10 seconds" debug="false">
	
	<property name="log_dir" value="logs" />
	
	<property name="maxHistory" value="8640" />

	...
	<property name="pay_log_dir" value="paylogs" />
	<appender name="PAY_HISTORY" class="ch.qos.logback.classic.sift.SiftingAppender">
		
		<discriminator>
			<key>AwsLogTypekey>
			<defaultValue>nonedefaultValue>
		discriminator>
		<sift>
			<appender name="FILE-${AwsLogType}" class="ch.qos.logback.core.rolling.RollingFileAppender">
				
				<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
					<fileNamePattern>${pay_log_dir}/%d{yyyy-MM-dd,aux}/%d{HH,GMT-08:00}/%d{yyyyMMdd,aux}_%d{HH,GMT-08:00,aux}_${AwsLogType}.logfileNamePattern>
					<maxHistory>8640maxHistory>
				rollingPolicy>
				<encoder>
					<charset>UTF-8charset>
					<pattern>%msg%npattern>
				encoder>
			appender>
		sift>
	appender>

	<appender name="PAY_HISTORY_JPY" class="ch.qos.logback.classic.sift.SiftingAppender">
		<discriminator>
			<key>AwsLogTypekey>
			<defaultValue>nonedefaultValue>
		discriminator>
		<sift>
			<appender name="FILE-${AwsLogType}" class="ch.qos.logback.core.rolling.RollingFileAppender">
				<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
					<fileNamePattern>${pay_log_dir}/%d{yyyy-MM-dd,aux}/%d{HH,GMT-08:00}/JPY_%d{yyyyMMdd,aux}_%d{HH,GMT-08:00,aux}_${AwsLogType}.logfileNamePattern>
					<maxHistory>8640maxHistory>
				rollingPolicy>
				<encoder>
					<charset>UTF-8charset>
					<pattern>%msg%npattern>
				encoder>
			appender>
		sift>
	appender>

	...
configuration>

附录

[1] MDC
[2] SimpleDateFormat 简介
[3] Chapter 6: Layouts
[4] Dates ISO_8601
[5] AkkaSourceDiscriminator.java

你可能感兴趣的:(Java,java,logback)