Springboot下的log4j2配置
2018-02-02 本文已影响0人
cschen
去掉内置的logback
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
加入log4j2
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
加入 log4j2.xml
只用RollingRandomAccessFile ,其它基本不用
<Configuration status="WARN" monitorInterval="300">
<properties>
<property name="LOG_HOME">/logs</property>
<property name="FILE_NAME">log</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m %l%n" />
</Console>
<RollingRandomAccessFile name="RollingFile"
fileName="${LOG_HOME}/${FILE_NAME}.log"
filePattern="${LOG_HOME}/${FILE_NAME}-%d{yyyy-MM-dd}-%i.log">
<PatternLayout
pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m %l%n" />
<Policies>
<TimeBasedTriggeringPolicy interval="1" />
<SizeBasedTriggeringPolicy size="20 MB" />
</Policies>
<DefaultRolloverStrategy max="20" />
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<!-- 全局level 设为 info -->
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="RollingFile" />
</Root>
<!-- 自己项目的 level 设为debug -->
<Logger name="com.myapp" level="trace" additivity="false">
<AppenderRef ref="RollingFile" />
<AppenderRef ref="Console" />
</Logger>
</Loggers>
</Configuration>
springboot自动会扫描class目录下的配置, 扫到就会启用。