首页 > 其他分享 >日志根据业务和等级拆分

日志根据业务和等级拆分

时间:2022-11-08 17:48:58浏览次数:41  
标签:CHANNEL LOG static channel 拆分 日志 log4j 等级

日志根据业务和等级拆分

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

相关文章

  • k8s+log-pilot日志收集
    github地址:https://github.com/AliyunContainerService/log-pilot介绍log-pilot是一个很棒的docker日志工具。可以从dockerlog-pilot主机收集日志并将它们发送到您的......
  • SpringBoot日志
    SpringBoot使用的slf4j+logback:官网文档:https://docs.spring.io/spring-boot/docs/current/reference/html/features.html#features.logging.shutdown-hook日志输......
  • AccessLog日志配置
    server.tomcat.accesslog.prefix=accessserver.tomcat.accesslog.suffix=.logserver.tomcat.accesslog.file-date-format=-yyyyMMddHH 有的时候我们要使用日志......
  • Serilog + Seq 搭建日志平台
    Docker部署Seq日志平台Dokce部署镜像地址:https://hub.docker.com/r/datalust/seqpull镜像dockerpulldatalust/seq设置seqAdmin密码,起站点用到的密码必须要Hash......
  • 腾讯TKE容器日志托管详细教程
    一、 腾讯云CLS介绍日志服务(CloudLogService,CLS)提供一站式的日志数据解决方案。您无需关注扩缩容等资源问题,五分钟快速便捷接入,即可享受日志的采集、存储、加工、检索分......
  • 【单片机/嵌入式】【梁山派】学习日志09:按键
    一、独立按键1.1独立按键基础知识1.1.1独立按键结构组成独立按键实际上是一个非自锁的轻触开关,有左右两个触点,当按下时左右两个触点闭合,当松开时左右两个触点断开。1.1......
  • 计算机等级考试二级C语言上机题集(第96~100套)
    第96套1.程序填空题给定程序中,函数fun的功能是:将形参s所指字符串中的数字字符转换成对应的数值,计算出这些数值的累加和作为函数值返回。例如,形参s所指的字符串为:abs5def1......
  • 计算机等级考试二级C语言模拟试卷(二)
    一、选择题(每小题1分,共40分)(1)下列叙述中,正确的是A)线性表的链式存储结构与顺序存储结构所需要的存储空间是相同的B)线性表的链式存储结构所需要的存储空......
  • 计算机等级考试二级C语言模拟试卷(一)
    一、选择题(每小题1分,共40分)(1)数据的存储结构是指    A)存储在外存中的数据B)数据所占的存储空间量C)数据在计算机中的顺序存储方式D)数据的逻辑结构在计算机中的表示(2......
  • 计算机等级考试二级C语言上机题集(第91~95套)
    第91套1.程序填空题给定程序中,函数fun的功能是:将形参n中各位上为偶数的数取出,并按原来从高位到低位相反的顺序组成一个新的数,并作为函数值返回。例如,若n=27638496,得到的......