标签:resource enc artifact follow logger int 官网文档 log4j tar
spring cloud 版本Greenwich.SR2
spring boot 版本2.1.8.RELEASE
** http://logging.apache.org/log4j/2.x/manual/configuration.html **
以下每个步骤不可缺失
pom.xml配置
需要排除spring-boot-starter自带的logback依赖,不然日志无法记录在日志文件里
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<!-- 排除自带的logback依赖 -->
<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>
log4j配置
新建log4j.xml放在resources目录下
设置console,InfoLog,ErrorLog的输出配置以及日志目录
Configuration status="INFO" 设置的是console的输出级别
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
<Appenders>
<!--添加一个控制台追加器-->
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout>
<pattern>[%-5p] %d %c - %m%n</pattern>
</PatternLayout>
</Console>
<!-- info log -->
<RollingFile name="InfoLog" fileName="/opt/logs/scm-warehouse/info.log"
filePattern="/opt/logs/scm-warehouse/info-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} %-5level %class{36}.%M[%L] - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<Filters>
<ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
<ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
</Filters>
</RollingFile>
<!-- error log -->
<RollingFile name="ErrorLog" fileName="/opt/logs/scm-warehouse/error.log"
filePattern="/opt/logs/scm-warehouse/error-%d{yyyy-MM-dd}.log">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%-5level] %class{36}.%M[%L] - %msg%xEx%n"/>
<Policies>
<TimeBasedTriggeringPolicy modulate="true" interval="1"/>
</Policies>
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
</RollingFile>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console" />
<AppenderRef ref="InfoLog" />
<AppenderRef ref="ErrorLog" />
</Root>
</Loggers>
</Configuration>
yml配置,指定配置
logging:
config: classpath:log4j.xml
level:
root: info
java代码内使用
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
private static Logger logger = LoggerFactory.getLogger(XXXclass.class);
logger.info("xxx);
标签:resource enc artifact follow logger int 官网文档 log4j tar
原文地址:https://www.cnblogs.com/cuiyf/p/12120989.html