log4j2详细配置Mybatis的SQL打印、日志文件保存路径、日志信息保存数据库和日志信息发送邮件等等

log4j2 配置

log4j2配置要求:
     1. 控制台打印系统错误级以上信息
     2. 控制台打印业务日志
     3. 系统及业务信息日志保存到日志文件
     4. 系统及业务警告日志保存到日志文件
     5. 系统及业务错误级以上日志保存到日志文件
     6. 业务日志保存到数据库
环境:
    Spring5、Servlet3和Mybaits3
其中邮件发送用到:
    javax.mail-1.4.7
    javax.activation-1.1
Tomcat配置的阿里巴巴Durid数据源(JNDI名称引用)
具体配置代码如下:







<configuration name="log4j2Configuration" status="WARN" monitorInterval="30" packages="org.apache.logging.log4j.core.layout">

    
    <Properties>
        
        <Property name="infoLogFile">${sys:catalina.home}/logs/info.logProperty>
        
        
        <Property name="infoLogFilePattern">${sys:catalina.home}/logs/${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.logProperty>
        
        <Property name="warnLogFile">${sys:catalina.home}/logs/warn.logProperty>
        
        <Property name="warnLogFilePattern">${sys:catalina.home}/logs/${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.logProperty>
        
        <Property name="errorLogFile">${sys:catalina.home}/logs/error.logProperty>
        
        <Property name="errorLogFilePattern">${sys:catalina.home}/logs/${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.logProperty>

        
        
        
        
        <Property name="LogLayoutPattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %level{DEBUG=详细, INFO=信息, WARN=警告, ERROR=错误, TRACE=严重} [%t] %c %m%nProperty>
    Properties>

    
    <appenders>
        
        <console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="${LogLayoutPattern}"/>
            
            <Filters>
                
                <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        console>

        
        <RollingFile name="RollingFileInfo" fileName="${infoLogFile}" filePattern="${infoLogFilePattern}">
            
            <PatternLayout pattern="${LogLayoutPattern}"/>
            
            <Filters>
                
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
                
            Filters>
            <Policies>
                
                <TimeBasedTriggeringPolicy/>
                
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="5"/>
        RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${warnLogFile}" filePattern="${warnLogFilePattern}">
            <PatternLayout pattern="${LogLayoutPattern}"/>
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="5"/>
        RollingFile>

        <RollingFile name="RollingFileError" fileName="${errorLogFile}" filePattern="${errorLogFilePattern}">
            <PatternLayout pattern="${LogLayoutPattern}"/>
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="50 MB"/>
            Policies>
            <DefaultRolloverStrategy max="3"/>
        RollingFile>

        
        <JDBC name="Database" tableName="user.syslog">
            
            <DataSource jndiName="java:comp/env/jdbc/OracleDataSource" />
            <Column name="requestTime" isEventTimestamp="true" />
            <Column name="logLevel" pattern="%level" />
            <column name="requestUser" pattern="%X{user}" />
            <Column name="requestResult" pattern="%message" />
        JDBC>

        
        <SMTP name="Mail" subject="System Error Log" to="[email protected]" from="[email protected]"
              replyTo="[email protected]" smtpProtocol="smtp" smtpHost="smtp.126.com"
              smtpPort="25" bufferSize="50" smtpDebug="false" smtpPassword="password"
              smtpUsername="[email protected]">
            
            <PatternLayout charset="UTF-8" pattern="%d %p [%c{1}:%L] %m%n"/>
            <Filters>
                
                <ThresholdFilter level="FATAL" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
        SMTP>

    appenders>
    
    <loggers>
        
        <logger name="org.springframework" level="DEBUG"/>
        <logger name="org.mybatis" level="DEBUG"/>
        
        <Logger name="pers.yanno.dessert.mapper" level="Debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
            <appender-ref ref="Mail"/>
        Logger>
        
        <logger name="pers.yanno.dessert.controller" level="INFO" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
            <appender-ref ref="Mail"/>
            <appender-ref ref="Database"/>
        logger>
        <root level="INFO">
            <appender-ref ref="Console" level="WARN"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
            <appender-ref ref="Mail"/>
        root>
    loggers>
configuration>

关于Spring5+Mybaits3以及JDNI配置此处不细说

你可能感兴趣的:(log4j)