我想通过log4j2 JDBCAppender将日志文件导入数据库,但是我不知道如何在数据库表中添加一列来保存指定的值,例如,您可以手动指定ID日志记录,以便以后通过ID检索记录
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<!-- Remove the default configuration -->
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Introduce the log4j2 dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
<!-- database driver -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.2</version>
</dependency>
</dependencies>
log4j2-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<JDBC name="databaseAppender" tableName="wyy.logs">
<ConnectionFactory class="com.example.demo.config.ConnectionFactory" method="getDatabaseConnection" />
<!-- <Column name="EVENT_ID" literal="logs_event_id_pk" isUnicode="false" />-->
<Column name="EVENT_DATE" isEventTimestamp="true" />
<Column name="LEVEL" pattern="%level" isUnicode="false" />
<Column name="LOGGER" pattern="%logger" isUnicode="false" />
<Column name="MESSAGE" pattern="%message" isUnicode="false" />
<Column name="THROWABLE" pattern="%ex{full}" isUnicode="false" />
</JDBC>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="databaseAppender"/>
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
java
@RestController
public class HelloController {
private final static Logger logger = LoggerFactory.getLogger(HelloController.class);
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public String sayHello(){
logger.info("Inside HelloController creating instance"+DateFormat.getInstance().format(new Date()));
logger.error("error");
return "Hello,Spring boot!";
}
}
表结构
create table logs
(
event_id serial not null
constraint logs_event_id_pk
primary key,
event_date timestamp,
logger text,
level text,
message text,
throwable text,
id integer
);
将日志插入数据库时,我可以手动输入id列