Java开发统一日志格式

一、日志配置

xml version="1.0" encoding="UTF-8"?>
<configuration>
    
    <property name="log.root.level" value="${log.root.level}" /> 
    <property name="log.other.level" value="${log.other.level}" /> 
    <property name="log.base" value="/usr/local/tomcat/logs" /> 
    <property name="log.moduleName" value="xxx" />  
    <property name="log.max.size" value="100MB" /> 

    
    <appender name="STDOUT-APPENDER" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%nPattern>
        encoder>
    appender>

    
    <appender name="DEFAULT-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-default.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">

            <FileNamePattern>${log.base}/history/default/${log.moduleName}- default-%d{yyyy-MM-dd}.%i.logFileNamePattern>

            
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>

        rollingPolicy>

        
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

    appender>

    
    <appender name="ERROR-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-error.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.base}/history/error/${log.moduleName}- error-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERRORlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

    appender>

    
    <appender name="WARN-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-warn.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.base}/history/warn/${log.moduleName}-warn-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>WARNlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

    appender>

    
    <appender name="DEBUG-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-debug.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.base}/history/debug/${log.moduleName}- debug-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>DEBUGlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

    appender>

    
    <appender name="INFO-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-info.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.base}/history/info/${log.moduleName}-info-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>INFOlevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

    appender>

    
    <appender name="TRACE-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">

        <File>${log.base}/${log.moduleName}-trace.logFile>

        
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <FileNamePattern>${log.base}/history/trace/${log.moduleName}- trace-%d{yyyy-MM-dd}.%i.logFileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>${log.max.size}maxFileSize>
            timeBasedFileNamingAndTriggeringPolicy>
        rollingPolicy>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{yyyy-MM-dd HH:mm:ss.SSS}] - %-5p - [%c]:%L.%method:%L - %msg%npattern>
        layout>

        
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>TRACElevel>
            <onMatch>ACCEPTonMatch>
            <onMismatch>DENYonMismatch>
        filter>

    appender>

    
    
    
    <appender name="ASYNC-DEFAULT-APPENDER" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0discardingThreshold>
        <queueSize>256queueSize>
        <includeCallerData>trueincludeCallerData>
        <appender-ref ref="DEFAULT-APPENDER" />
    appender>

    
    <logger name="com.xinniu" additivity="false">
        <level value="${log.other.level}" />
        <appender-ref ref="STDOUT-APPENDER" />
        <appender-ref ref="ASYNC-DEFAULT-APPENDER" />
        <appender-ref ref="ERROR-APPENDER" />
        <appender-ref ref="WARN-APPENDER" />
        ${log.appender.xniniu}
        <appender-ref ref="DEFAULT-APPENDER" />
    logger>

    
    <root level="${log.root.level}">
        <appender-ref ref="STDOUT-APPENDER" />
        <appender-ref ref="ASYNC-DEFAULT-APPENDER" />
        <appender-ref ref="ERROR-APPENDER" />
        <appender-ref ref="WARN-APPENDER" />
        ${log.appender.root}
        <appender-ref ref="DEFAULT-APPENDER" />
    root>

configuration>

PS:此配置文件除了会按照日志级别分别打印到${log.moduleName}-error.log、${log.moduleName}-info.log等日志文件中,同时为了方便线下环境控制台查看日志,定位问题,该配置也会在控制台打印一份日志,建议线上环境关闭掉控制台日志。

 

二、日志使用

1、日志打印统一使用logback

2、开发环境、测试环境和灰度环境日志级别可以根据需要配置成info或debug级别,线上环境需要配置成info级别。

3、将不同级别的日志分别打印到不同的日志文件中${log.moduleName}-error.log、${log.moduleName}-warn.log、${log.moduleName}-info.log、${log.moduleName}-debug.log等。

4、本系统对外的接口入参、出参必须打印出来,日志级别为info。

5、本系统中调用本系统外接口时必须打印入参、出参,日志级别为info。

6、系统中关键逻辑、容易出错的逻辑处需要打印日志,方便开发和线上环境排查问题,日志级别为info。

7、所有try…catch的地方都需要打印错误日志。

8、错误日志打印时一定要把堆栈打印出来(没有堆栈时可不打印,另外自定义的异常不要打印堆栈信息),方便排查问题时定位原因。例如

LOGGER.error("服务端接口: 根据合同ID获取交易流水列表getListByContractId,params:{} 异常为:", contractId, e);

9、日志打印时要将敏感字段脱敏或加密。

10、除错误日志以外(错误日志请参考第7、8条),其他日志打印用花括号{}占位符,不要用加号+的形式拼接字符串。例如

LOGGER.info("服务端接口: 根据合同ID获取交易流水列表getListByContractId,params:{}", contractId);

11、不要使用e.printStackTrace();

12、除测试代码外严禁使用System.out.println()、System.gc()

你可能感兴趣的:(java,日志)