log4j2配置实例[按小时记录日志文件]

log4j2.xml文件配置实例:

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="off">
    <properties>
        <!-- File base path -->
        <property name="LOG_HOME">/logs</property>
        <!-- App server name -->
        <property name="SERVER_NAME">cloud</property>
        <!-- Backup file path -->
        <property name="BACKUP_HOME">{LOG_HOME}/backup</property>
    </properties>

    <appenders>
        <!-- Console -->
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36}.%M@%L - %msg%n"/>
            <!--<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %C{5} (%F:%L) - %msg%n"/>-->
        </Console>
        <!-- RollingFile -->
        <RollingRandomAccessFile name="RollingFile"
                                 fileName="${LOG_HOME}/${SERVER_NAME}"
                                 filePattern="${LOG_HOME}/${SERVER_NAME}.%d{yyyy-MM-dd-HH}.log">
            <PatternLayout pattern="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level %logger{36}.%M@%L - %msg%n"/>
            <Policies>
                <!--<TimeBasedTriggeringPolicy modulate="true" interval="24"/>-->
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="200 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="100"/>
        </RollingRandomAccessFile>
    </appenders>

    <loggers>
        <!-- Spring Loggers -->
        <logger name="org.springframework" level="info"/>
        <logger name="org.springframework.beans" level="info"/>
        <logger name="org.springframework.core" level="info"/>
        <logger name="org.springframework.context" level="info"/>
        <logger name="org.springframework.web" level="info"/>
        <logger name="org.springframework.batch" level="info"/>
        <logger name="org.springframework.integration" level="info"/>

        <!-- Apache Loggers -->
        <logger name="org.apache" level="info"/>
        <logger name="org.apache.http" level="info"/>
        <logger name="org.apache.shiro" level="info"/>
        <logger name="org.apache.solr" level="info"/>
        <logger name="org.apache.lucene" level="info"/>
        <logger name="org.apache.tomcat" level="info"/>
        <logger name="org.apache.commons" level="info"/>

        <!-- MyBatis Loggers -->
        <logger name="org.mybatis" level="info"/>
        <logger name="org.apache.ibatis" level="info"/>

        <!-- Alibaba Loggers -->
        <logger name="com.alibaba" level="info"/>
        <logger name="com.alibaba.druid" level="info"/>

        <!-- App Loggers -->
        <logger name="com.code.platform" level="debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </logger>

        <!-- Root Logger -->
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFile"/>
        </root>
    </loggers>
</configuration>


你可能感兴趣的:(java日志,log4j2,Log4j配置)