Spring Cloud 项目整合Log4j2
Spring Cloud 项目整合Log4j2的步骤。
1、排除logback的默认集成。
因为Spring Cloud 默认集成了logback, 所以首先要排除logback的集成,在pom.xml文件:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</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-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
2、log4j2-test.xml配置文件,在这里不做赘述,需要注意的地方是,如果想把日志打印项目名称的目录下面,需要首先在你的git上configuration的properties文件中设置,日志输出的path:
logging.path=/opt/application-log/${spring.application.name}
${spring.application.name}这个可以读取到所在项目的工程名,在项目中bootstrap.properties中有定义,这样就可以适应任何工程
设置完这个path之后,需要在你的log4j2-test.xml中读取到这个path, 读取的方法是:${sys:LOG_PATH},在文件中可以这样设置:
<Property name="log_path">${sys:LOG_PATH}/${
date:yyyy-MM-dd}</Property>
而对应的如果想把info.log,warn.log等文件放到日志文件夹中,可以结合上面的设置,再做改进:
<RollingFile name="RollingFileInfo" fileName="${log_path}/info-${date:yyyy-MM-dd}.log" filePattern="${log_path}/$${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log">
还没有评论,来说两句吧...