logback(推荐)logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="30 seconds">
<contextListener
class="ch.qos.logback.classic.jul.LevelChangePropagator" />
<property name="App_Name" value="app" />
<property name="Log_Path" value="logs/" />
<property name="File_Pattern"
value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level [%t] %logger - %msg%n" />
<property name="Console_pattern"
value="%d{yyyy-MM-dd HH:mm:ss:SSS} %-5level [%t] %logger:%-4line - %msg%n" />
<contextName>${App_Name}</contextName>
<appender name="console"
class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${Console_pattern}</pattern>
</encoder>
</appender>
<appender name="infoRollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${Log_Path}${App_Name}.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${Log_Path}${App_Name}-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>900MB</maxFileSize>
<maxHistory>90</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${File_Pattern}</pattern>
</encoder>
</appender>
<appender name="errorRollingFile"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>WARN</level>
</filter>
<file>${Log_Path}${App_Name}.error.log</file>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${Log_Path}${App_Name}.error-%d{yyyy-MM-dd}.%i.log
</fileNamePattern>
<maxFileSize>900MB</maxFileSize>
<maxHistory>90</maxHistory>
<totalSizeCap>10GB</totalSizeCap>
</rollingPolicy>
<encoder>
<pattern>${File_Pattern}</pattern>
</encoder>
</appender>
<springProfile name="dev,local,zhlocal">
<root level="INFO">
<appender-ref ref="console" />
<appender-ref ref="infoRollingFile" />
<appender-ref ref="errorRollingFile" />
</root>
</springProfile>
<springProfile name="test">
<root level="INFO">
<appender-ref ref="infoRollingFile" />
<appender-ref ref="errorRollingFile" />
</root>
</springProfile>
<springProfile name="demo,prod">
<logger name="com.baomidou" level="WARN" />
<root level="INFO">
<appender-ref ref="infoRollingFile" />
<appender-ref ref="errorRollingFile" />
</root>
</springProfile>
</configuration>
log4j2
依赖配置(spring-boot)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
配置文件
log4j2.xml或log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration monitorInterval="5">
<!--变量配置 -->
<Properties>
<property name="LOG_PATTERN"
value="%date{yyyy-MM-dd HH:mm:ss.SSS} [%-20.20thread] %-5level %-50.50logger{50} - %msg%n" />
<property name="FILE_PATH" value="./logs" />
<property name="FILE_NAME" value="zc-project" />
</Properties>
<appenders>
<console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}" />
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="DENY" />
</console>
<File name="Filelog" fileName="${FILE_PATH}/${FILE_NAME}.log"
append="false">
<PatternLayout pattern="${LOG_PATTERN}" />
</File>
<RollingFile name="RollingFileInfo"
fileName="${FILE_PATH}/${FILE_NAME}-info.log"
filePattern="${FILE_PATH}/${FILE_NAME}-info-%d{yyyy-MM-dd}_%i.log">
<PatternLayout pattern="${LOG_PATTERN}" />
<Filters>
<ThresholdFilter level="warn" onMatch="DENY"
onMismatch="NEUTRAL" />
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="info" onMatch="ACCEPT"
onMismatch="DENY" />
</Filters>
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="900MB" />
</Policies>
<DefaultRolloverStrategy max="15" />
</RollingFile>
<RollingFile name="RollingFileWarn"
fileName="${FILE_PATH}/${FILE_NAME}-warn.log"
filePattern="${FILE_PATH}/${FILE_NAME}-warn-%d{yyyy-MM-dd}_%i.log">
<Filters>
<ThresholdFilter level="error" onMatch="DENY"
onMismatch="NEUTRAL" />
<ThresholdFilter level="warn" onMatch="ACCEPT"
onMismatch="DENY" />
</Filters>
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="900MB" />
</Policies>
<!-- 最多同一文件夹下30个文件开始覆盖 -->
<DefaultRolloverStrategy max="30" />
</RollingFile>
<RollingFile name="RollingFileError"
fileName="${FILE_PATH}/${FILE_NAME}-error.log"
filePattern="${FILE_PATH}/${FILE_NAME}-error-%d{yyyy-MM-dd}_%i.log">
<!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch) -->
<ThresholdFilter level="error" onMatch="ACCEPT"
onMismatch="DENY" />
<PatternLayout pattern="${LOG_PATTERN}" />
<Policies>
<TimeBasedTriggeringPolicy />
<SizeBasedTriggeringPolicy size="900MB" />
</Policies>
<DefaultRolloverStrategy max="30" />
</RollingFile>
</appenders>
<loggers>
<logger name="org.mybatis" level="info" additivity="false">
<AppenderRef ref="Console" />
</logger>
<Logger name="org.springframework" level="info"
additivity="false">
<AppenderRef ref="Console" />
</Logger>
<root level="info">
<appender-ref ref="Console" />
<appender-ref ref="Filelog" />
<appender-ref ref="RollingFileInfo" />
<appender-ref ref="RollingFileWarn" />
<appender-ref ref="RollingFileError" />
</root>
</loggers>
</configuration>
全异步配置(启动脚本中增加一个系统变量)
-Dlog4j2.contextSelector=org.apache.logging.log4j.core.async.AsyncLoggerContextSelector