spring boot整合log4j2
POM配置
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.roncoo.education</groupId> <artifactId>spring-boot-demo-05-2</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>spring-boot-demo-05-2</name> <description>日志配置-log4j2</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.4.0.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <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> <!-- 使用log4j2 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/org.postgresql/postgresql --> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.1.4</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
log4j2配置
<?xml version="1.0" encoding="utf-8"?> <configuration> <properties> <!-- 文件输出格式 --> <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property> </properties> <appenders> <Console name="CONSOLE" target="system_out"> <PatternLayout pattern="${PATTERN}" /> </Console> <File name="log" fileName="log/test.log" append="false"> <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" /> </File> <!-- 添加过滤器ThresholdFilter,可以有选择的输出某个级别以上的类别 onMatch="ACCEPT" onMismatch="DENY"意思是匹配就接受,否则直接拒绝 --> <File name="ERROR" fileName="logs/error.log"> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" /> <PatternLayout pattern="%d{yyyy.MM.dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> </File> <!-- 这个会打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 --> <RollingFile name="RollingFile" fileName="logs/web.log" filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n" /> <SizeBasedTriggeringPolicy size="1K" /> </RollingFile> <!-- 配置日志写入postgresql --> <JDBC name="databaseAppender" tableName="logger_info"> <ConnectionFactory class="com.roncoo.education.database.ConnectionFactory" method="getDatabaseConnection"/> <Column name="thread_name" pattern="%t"/> <Column name="class_name" pattern="%C"/> <Column name="method_name" pattern="%method"/> <Column name="begin_time" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}"/> <Column name="logger_level" pattern="%level"/> <Column name="logger_name" pattern="%c"/> <Column name="logger_message" pattern="%m"/> <Column name="logger_throwable" pattern="%throwable{3}"/> <Column name="create_time" pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}"/> </JDBC> <!-- 异步写入数据库 --> <Async name="AsyncDatabaseAppender"> <appender-ref ref="databaseAppender"/> </Async> </appenders> <loggers> <logger name="com.roncoo.education" level="debug" /> <root level="info"> <appenderref ref="CONSOLE" /> <appenderref ref="log" /> <appenderref ref="ERROR" /> <appenderref ref="RollingFile" /> <appenderref ref="databaseAppender" /> </root> </loggers> </configuration>
注意点
暂不支持postgresql,所以使用oracle替代