日志根据业务和等级拆分
1 导入依赖
<dependencyManagement> <dependencies> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.17.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.17.2</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-slf4j-impl</artifactId> <version>2.17.2</version> </dependency> </dependencies> </dependencyManagement>
2 添加配置文件 log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- status : log4j本身日志级别 ; --> <Configuration status="ERROR"> <Properties> <!-- 日志存放的位置 --> <property name="LOG_HOME">logs</property> <!-- 项目名称 --> <property name="PROJECT_NAME">serena-channel-service</property> <!-- 所有业务Error日志名字 --> <property name="ERROR_FILE_NAME">channel-error</property> <!-- 所有业务Info日志名字 --> <property name="INFO_FILE_NAME">channel-info</property> <!-- 所有业务Debug日志名字 --> <property name="DEBUG_FILE_NAME">channel-debug</property> <!-- 日志输出格式 --> <property name="PATTERN">[%d{yyyy-MM-dd HH:mm:ss.SSS}] [%thread] [%c.%M,%L] %-5p - %m%n</property> <!-- 控制台日志级别 --> <property name="CONSOLE_LEVEL">info</property> <!-- 历史日志保存路径 --> <property name="HistoryDir">history</property> <!-- 日志保存天数 --> <property name="maxHistory">30d</property> <!-- 通用日志名字 --> <property name="CHANNEL_COMMON_INFO_FILE_NAME">channel-common-info</property> <property name="CHANNEL_COMMON_DEBUG_FILE_NAME">channel-common-debug</property> <property name="CHANNEL_COMMON_ERROR_FILE_NAME">channel-common-error</property>
<!-- 短信业务日志名字 -->
<property name="CHANNEL_SMS_INFO_FILE_NAME">channel-sms-info</property> <property name="CHANNEL_SMS_DEBUG_FILE_NAME">channel-sms-debug</property> <property name="CHANNEL_SMS_ERROR_FILE_NAME">channel-sms-error</property>
<!-- 富媒体日志名字 -->
<property name="CHANNEL_VMS_INFO_FILE_NAME">channel-vms-info</property> <property name="CHANNEL_VMS_DEBUG_FILE_NAME">channel-vms-debug</property> <property name="CHANNEL_VMS_ERROR_FILE_NAME">channel-vms-error</property> </Properties> <Appenders> <!--控制台 --> <Console name="CONSOLE" target="SYSTEM_OUT"> <ThresholdFilter level="${CONSOLE_LEVEL}" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${PATTERN}"/> </Console> <!-- Info File --> <RollingFile name="INFO_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${INFO_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${INFO_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <!-- Debug File --> <RollingFile name="DEBUG_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${DEBUG_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${DEBUG_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <!-- Error File --> <RollingFile name="ERROR_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${ERROR_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${ERROR_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile>
<RollingFile name="CHANNEL_COMMON_INFO_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_COMMON_INFO_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_COMMON_INFO_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_COMMON_DEBUG_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_COMMON_DEBUG_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_COMMON_DEBUG_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_COMMON_ERROR_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_COMMON_ERROR_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_COMMON_ERROR_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_SMS_INFO_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_SMS_INFO_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_SMS_INFO_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_SMS_DEBUG_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_SMS_DEBUG_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_SMS_DEBUG_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_SMS_ERROR_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_SMS_ERROR_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_SMS_ERROR_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_VMS_INFO_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_VMS_INFO_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_VMS_INFO_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_VMS_DEBUG_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_VMS_DEBUG_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_VMS_DEBUG_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <!-- 按照配置的yyyy-MM-dd精度来执行分割,interval为单位,此处为1天 --> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> <RollingFile name="CHANNEL_VMS_ERROR_LOG_FILE" fileName="${LOG_HOME}/${PROJECT_NAME}/${CHANNEL_VMS_ERROR_FILE_NAME}.log" filePattern="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}/${CHANNEL_VMS_ERROR_FILE_NAME}.%d{yyyy-MM-dd}.log" append="true"> <PatternLayout pattern="${PATTERN}"/> <Policies> <TimeBasedTriggeringPolicy interval="1" modulate="true"/> </Policies> <Filters> <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <DefaultRolloverStrategy> <Delete basePath="${LOG_HOME}/${PROJECT_NAME}/${HistoryDir}"> <IfFileName glob="*.log" /> <IfLastModified age="${maxHistory}" /> </Delete> </DefaultRolloverStrategy> </RollingFile> </Appenders> <loggers> <logger name="logger.ChannelCommonDetail" additivity="true" level="DEBUG"> <appender-ref ref="CHANNEL_COMMON_INFO_LOG_FILE"/> <appender-ref ref="CHANNEL_COMMON_DEBUG_LOG_FILE"/> <appender-ref ref="CHANNEL_COMMON_ERROR_LOG_FILE"/> </logger> <logger name="logger.ChannelSmsDetail" additivity="true" level="DEBUG"> <appender-ref ref="CHANNEL_SMS_INFO_LOG_FILE"/> <appender-ref ref="CHANNEL_SMS_DEBUG_LOG_FILE"/> <appender-ref ref="CHANNEL_SMS_ERROR_LOG_FILE"/> </logger> <logger name="logger.ChannelVmsDetail" additivity="true" level="DEBUG"> <appender-ref ref="CHANNEL_VMS_INFO_LOG_FILE"/> <appender-ref ref="CHANNEL_VMS_DEBUG_LOG_FILE"/> <appender-ref ref="CHANNEL_VMS_ERROR_LOG_FILE"/> </logger> <root level="TRACE" additivity="false"> <appender-ref ref="CONSOLE"/> <appender-ref ref="INFO_LOG_FILE"/> <appender-ref ref="ERROR_LOG_FILE"/> <appender-ref ref="DEBUG_LOG_FILE"/> </root> <logger name="org.hibernate" additivity="false" level="INFO"> <appender-ref ref="INFO_LOG_FILE"/> <appender-ref ref="ERROR_LOG_FILE"/> </logger> <logger name="org.springframework" additivity="false" level="INFO"> <appender-ref ref="INFO_LOG_FILE"/> <appender-ref ref="ERROR_LOG_FILE"/> </logger> <logger name="io.netty" additivity="false" level="INFO"> <appender-ref ref="INFO_LOG_FILE"/> <appender-ref ref="ERROR_LOG_FILE"/> </logger> <logger name="org.apache" additivity="false" level="INFO"> <appender-ref ref="INFO_LOG_FILE"/> <appender-ref ref="ERROR_LOG_FILE"/> </logger> </Configuration>
3 添加常量LogConstante.class
常量值前面部分与第二步配置文件中<logger>标签中的 name保持一致,常量值最后添加.分割
public class LogConstants { //常量值前面部分与第二步配置文件中<logger>标签中的 name保持一致,常量值最后添加.分割 public static final String CHANNEL_COMMON_LOG = "logger.ChannelCommonDetail."; public static final String CHANNEL_SMS_LOG = "logger.ChannelSmsDetail."; public static final String CHANNEL_VMS_LOG = "logger.ChannelVmsDetail."; }
4 使用log时添加业务前缀
//通用业务日志打印 private static Logger log = LoggerFactory.getLogger(LogConstants.CHANNEL_COMMON_LOG + UserService.class);
//短信业务日志打印 private static Logger log = LoggerFactory.getLogger(LogConstants.CHANNEL_SMS_LOG + SmsCheckTask.class);
//富媒体业务日志打印 private static Logger log = LoggerFactory.getLogger(LogConstants.CHANNEL_VMS_LOG + VmsMessageTask.class);
标签:CHANNEL,LOG,static,channel,拆分,日志,log4j,等级 From: https://www.cnblogs.com/serena25/p/16870542.html