后端项目开发:集成日志

SpringBoot 默认选择的是slf4j做日志门面,logback做日志实现。由于log4j有性能问题,不建议使用。
由于log4j2的表现性能更好,我们需要集成log4j2,同时排除旧的日志实现包。


<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-webartifactId>
    
    <exclusions>
        <exclusion>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-loggingartifactId>
        exclusion>
    exclusions>
dependency>

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-log4j2artifactId>
dependency>

添加log4j2.xml配置文件



<Configuration status="WARN" monitorInterval="600">
    <Properties>
        
        <Property name="LOG_HOME">D:\logsProperty>
    Properties>
    <Appenders>
        
        <Console name="console_out_appender" target="SYSTEM_OUT">
            
            <ThresholdFilter level="warn" onMatch="DENY" onMismatch="ACCEPT"/>
            
            <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/>
        Console>
        
        <Console name="console_err_appender" target="SYSTEM_ERR">
            
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%n"/>
        Console>

        
        
        <RollingRandomAccessFile name="trace_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/trace.log"
                                 filePattern="${LOG_HOME}/trace/trace - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>

                
                <SizeBasedTriggeringPolicy size="2MB"/>

            Policies>
            <Filters>
                
                <ThresholdFilter level="debug" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        RollingRandomAccessFile>

        
        
        <RollingRandomAccessFile name="debug_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/debug.log"
                                 filePattern="${LOG_HOME}/debug/debug - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>

                
                <SizeBasedTriggeringPolicy size="2MB"/>

                
                

            Policies>
            <Filters>
                
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="info_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/info.log"
                                 filePattern="${LOG_HOME}/info/info - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="warn_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/warn.log"
                                 filePattern="${LOG_HOME}/warn/warn - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        RollingRandomAccessFile>

        
        <RollingRandomAccessFile name="error_appender"
                                 immediateFlush="true" fileName="${LOG_HOME}/error.log"
                                 filePattern="${LOG_HOME}/error/error - %d{yyyy-MM-dd HH_mm_ss}.log.gz">
            <PatternLayout>
                <pattern>%5p [%t] %d{yyyy-MM-dd HH:mm:ss} (%F:%L) %m%npattern>
            PatternLayout>
            <Policies>
                <SizeBasedTriggeringPolicy size="2MB"/>
            Policies>
            <Filters>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        RollingRandomAccessFile>
    Appenders>

    <Loggers>
        
        <root level="info">
            <appender-ref ref="console_out_appender"/>
            <appender-ref ref="console_err_appender"/>
            <appender-ref ref="trace_appender"/>
            <appender-ref ref="debug_appender"/>
            <appender-ref ref="info_appender"/>
            <appender-ref ref="warn_appender"/>
            <appender-ref ref="error_appender"/>
        root>

        
        <logger name="org.springframework.core" level="info"/>
        <logger name="org.springframework.beans" level="info"/>
        <logger name="org.springframework.context" level="info"/>
        <logger name="org.springframework.web" level="info"/>
        <logger name="org.jboss.netty" level="warn"/>
        <logger name="org.apache.http" level="warn"/>

    Loggers>

Configuration>

你可能感兴趣的:(软件项目开发最佳实践,java,spring,boot,单元测试)