spring boot使用log4j2将日志写入mysql数据库
log4j2官方例子在spring boot中报错而且还是用的是org.apache.commons.dbcp包
我给改了一下使用org.apache.commons.dbcp2包
1.log4j2.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="error">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<JDBC name="DBLogger" tableName="MicroServiceLogHandle">
<ConnectionFactory class="com.malls.common.tool.LogConnectionFactory"
method="getDatabaseConnection" />
<Column name="RequestKey" pattern="${sd:RequestKey}" />
<Column name="LogType" pattern="${sd:LogType}" />
<Column name="RequestUrl" pattern="${sd:RequestUrl}" />
<Column name="UserName" pattern="${sd:UserName}" />
<Column name="OrderNo" pattern="${sd:OrderNo}" />
<Column name="Content" pattern="${sd:Content}" />
<Column name="Keyword" pattern="${sd:Keyword}" />
<Column name="ClientIP" pattern="${sd:ClientIP}" />
<Column name="TimeLong" pattern="${sd:TimeLong}" />
<Column name="LogTime" pattern="${sd:LogTime}" />
<Column name="DBCreateTime" literal="now()" />
<Column name="ServerDesc" pattern="${sd:ServerDesc}" />
<Column name="LogLevel" pattern="${sd:LogLevel}" />
<Column name="RequestServerIP" pattern="${sd:RequestServerIP}" />
<Column name="ServerIP" pattern="${sd:ServerIP}" />
<Column name="CurrentApiRequestKey" pattern="${sd:CurrentApiRequestKey}" />
</JDBC>
</Appenders>
<Loggers>
<AsyncLogger name="AsyncDBLogger" level="debug"
includeLocation="true">
<AppenderRef ref="DBLogger" />
</AsyncLogger>
<Root level="info">
<AppenderRef ref="Console" />
</Root>
</Loggers>
</Configuration>
AsyncLogger 表示是异步插入.需要在pom.xml中插入disruptor引用
<dependency>
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId><!-- log4j异步插入用到 -->
<version>3.4.1</version>
</dependency>
2.创建LogConnectionFactory类:
package com.malls.common.tool; import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; import javax.sql.DataSource; import org.apache.commons.dbcp2.ConnectionFactory;
import org.apache.commons.dbcp2.DriverManagerConnectionFactory;
import org.apache.commons.dbcp2.PoolableConnection;
import org.apache.commons.dbcp2.PoolableConnectionFactory;
import org.apache.commons.dbcp2.PoolingDataSource;
import org.apache.commons.pool2.ObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPool; import com.malls.common.model.DBConfig; public class LogConnectionFactory { private static interface Singleton {
final LogConnectionFactory INSTANCE = new LogConnectionFactory();
} private DataSource dataSource; private LogConnectionFactory() { } private void initDataSource() { try {
//
// First, we'll create a ConnectionFactory that the
// pool will use to create Connections.
// We'll use the DriverManagerConnectionFactory,
// using the connect string passed in the command line
// arguments.
DBConfig dbConfig = ApplicationConfig.GetDbConfig("dblog");
ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(dbConfig.getUrl(),
dbConfig.getUserName(), dbConfig.getPassword()); //
// Next we'll create the PoolableConnectionFactory, which wraps
// the "real" Connections created by the ConnectionFactory with
// the classes that implement the pooling functionality.
//
PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory,
null); //
// Now we'll need a ObjectPool that serves as the
// actual pool of connections.
//
// We'll use a GenericObjectPool instance, although
// any ObjectPool implementation will suffice.
//
ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory); // Set the factory's pool property to the owning pool
poolableConnectionFactory.setPool(connectionPool); //
// Finally, we create the PoolingDriver itself,
// passing in the object pool we created.
//
dataSource = new PoolingDataSource<>(connectionPool);
} catch (Exception e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
dataSource = null;
} } int i = 0;
private static Lock lock = new ReentrantLock(); public static Connection getDatabaseConnection() throws SQLException {
if (Singleton.INSTANCE.i == 0) {
//这儿如果第一次直接返回连接池的话会报错
//因为PoolableConnectionFactory也使用了log4j记录日志
//这儿是重点
Singleton.INSTANCE.i++;
DBConfig dbConfig = ApplicationConfig.GetDbConfig("dblog");
return DriverManager.getConnection(dbConfig.getUrl(), dbConfig.getUserName(), dbConfig.getPassword());
}
if (Singleton.INSTANCE.dataSource == null) {
lock.lock();
try {
if (Singleton.INSTANCE.dataSource == null) {
Singleton.INSTANCE.initDataSource();
}
} finally {
lock.unlock();
}
}
return Singleton.INSTANCE.dataSource.getConnection();
}
}
要注意注释的地方,第二次才返回连接池
3.创建DBLog类:
package com.malls.common.tool; import java.time.Duration;
import java.time.LocalDateTime;
import java.util.UUID; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.StructuredDataMessage;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder; import com.malls.common.model.RequestModel; public class DBLog {
private static final Logger LOGGER = LogManager.getLogger("AsyncDBLogger"); public static void process(String logType, String content) {
process(logType, content, "");
} public static void process(String logType, String content, String keyWord) {
StructuredDataMessage msg = getataMessage(logType, content, keyWord);
addMsg(msg, "logLevel", "Process");
LOGGER.info(msg);
} public static void error(String logType, String content) {
error(logType, content, "");
} public static void error(String logType, String content, String keyWord) {
StructuredDataMessage msg = getataMessage(logType, content, keyWord);
addMsg(msg, "logLevel", "Error");
LOGGER.error(msg);
} public static void handle(String logType, String content) {
handle(logType, content, "");
} public static void handle(String logType, String content, String keyWord) {
StructuredDataMessage msg = getataMessage(logType, content, keyWord);
addMsg(msg, "LogLevel", "Handle");
LOGGER.info(msg);
} private static StructuredDataMessage getataMessage(String logType, String content, String keyWord) {
String confirm = UUID.randomUUID().toString().replace("-", "");
StructuredDataMessage msg = new StructuredDataMessage(confirm, "", "transfer");
RequestAttributes req = RequestContextHolder.currentRequestAttributes();
RequestModel requestModel = null;
if (req != null) {
requestModel = (RequestModel) req.getAttribute("RequestModel", RequestAttributes.SCOPE_REQUEST);
}
if (requestModel == null) {
requestModel = new RequestModel();
}
addMsg(msg, "RequestKey", requestModel.getRequestKey());
addMsg(msg, "RequestUrl", requestModel.getRequestUrl());
addMsg(msg, "UserName", String.valueOf(requestModel.getCurrentUserId()));
addMsg(msg, "OrderNo", requestModel.getOrderNo());
addMsg(msg, "LogType", logType);
addMsg(msg, "Content", content);
addMsg(msg, "Keyword", keyWord);
addMsg(msg, "ClientIP", requestModel.getClientIP());
long timeLong = Duration.between(requestModel.getBeginRequestTime(), LocalDateTime.now()).toMillis();
addMsg(msg, "TimeLong", String.valueOf(timeLong));
addMsg(msg, "ServerDesc", "777");
addMsg(msg, "RequestServerIP", requestModel.getRequestServerIP());
addMsg(msg, "ServerIP", requestModel.getServerIP());
addMsg(msg, "CurrentApiRequestKey", requestModel.getCurrentApiRequestKey());
addMsg(msg, "LogTime", LocalDateTime.now().toString());
return msg;
} private static void addMsg(StructuredDataMessage msg, String key, String val) {
if (val == null) {
msg.put(key, "");
} else {
msg.put(key, val);
}
}
}
这样就可以了.
spring boot使用log4j2将日志写入mysql数据库的更多相关文章
- Spring Boot入门(2)使用MySQL数据库
介绍 本文将介绍如何在Spring项目中连接.处理MySQL数据库. 该项目使用Spring Data JPA和Hibernate来连接.处理MySQL数据库,当然,这仅仅是其中一种方式,你也 ...
- Spring Boot项目中使用jdbctemplate 操作MYSQL数据库
不废话,先来代码 pom文件: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http ...
- Spring Boot系列一:默认日志logback配置解析
前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...
- Spring Boot 学习摘要--关于日志框架
date: 2020-01-05 16:20:00 updated: 2020-01-08 15:50:00 Spring Boot 学习摘要--关于日志框架 学习教程来自:B站 尚硅谷 1. 关于日 ...
- spring boot使用slf4j输出日志
spring boot使用slf4j输出日志 https://blog.csdn.net/qq442270636/article/details/79406346 Spring Boot SLF4J日 ...
- Spring Boot Logback几种日志详解
日志对于应用程序来说是非常重要的,Spring框架本身集成了不少其他工具,我们自身的应用也会使用到第三方库,所以我们推荐在Spring应用中使用SLF4J/Logback来记录日志. SLF4J与Lo ...
- Spring Boot中使用logback日志框架
说明:Spring Boot在最新的版本中默认使用了logback框架.一般来说使用时只需在classpath下创建logback.xml即可,而官方推荐使用logback-spring.xml替代, ...
- Spring boot使用log4j打印日志
先将maven中spring-boot-starter的日志spring-boot-starter-logging去掉 <dependency> <groupId>org.sp ...
- spring boot配置druid连接池连接mysql
Spring Boot 集成教程 Spring Boot 介绍 Spring Boot 开发环境搭建(Eclipse) Spring Boot Hello World (restful接口)例子 sp ...
随机推荐
- FastDFS是纯C语言实现,只支持Linux,适合以中小文件为载体的在线服务,还可以冗余备份和负载均衡
一.理论基础 FastDFS比较适合以中小文件为载体的在线服务,比如跟NGINX(APACHE)配合搭建图片服务器. 分布式文件系统FastDFS FastDFS是纯C语言实现,只支持Linux.Fr ...
- WPF里的一些Effect特效
原文:WPF里的一些Effect特效 Blend的特效都在Microsoft.Expression.Media.Effects里,用之前添加一下引用. 可以在前台选中对象后直接点击Effect新建一种 ...
- C++使用libcurl做HttpClient(业务观摩,用C++封装过程式代码,post和get的数据,最好url编码,否则+会变成空格)good
当使用C++做HTTP客户端时,目前通用的做法就是使用libcurl.其官方网站的地址是http://curl.haxx.se/,该网站主要提供了Curl和libcurl.Curl是命令行工具,用于完 ...
- jquery权限选择
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- WPF数据验证方式
WPF有两种数据验证的方式: 1 在数据对象上进行验证:普通属性验证或者实现IDataErrorInfo接口 2 可以再绑定规则上进行验证:ExceptionValidationRule异常验证规则 ...
- vxworks下libpcap的移植
linux下的libpcap应用能够成熟的使用在第三方的应用中,但基于vxworks开发的项目中需要使用libpcap的部分功能则无相应的实现. 研究了下libpcap向vxworks的移植,并且小有 ...
- 改善C#程序的建议7:正确停止线程
原文:改善C#程序的建议7:正确停止线程 开发者总尝试对自己的代码有更多的控制.“让那个还在工作的线程马上停止下来”就是诸多要求中的一种.然而事与愿违,这里面至少存在两个问题: 第一个问题是:正如线程 ...
- Android零基础入门第77节:Activity任务栈和启动模式
通过前面的学习,Activity的基本使用都已掌握,接下来一起来学习更高级的一些内容. Android采用任务栈(Task)的方式来管理Activity的实例.当启动一个应用时,Android就会为之 ...
- Wolf RPG Editor游戏解包
前言 使用arc_conv_r53进行解包 使用touhouSE进行解包 使用DXEXTRACT进行解包 前言 Wolf RPG Editor由于其需要翻来覆去的转码,脚本名称等问题算是解包跟汉化中比 ...
- 用 eric6 与 PyQt5 实现python的极速GUI编程(35篇PyQT和200多篇Python)
[题记] 我是一个菜鸟,这个系列是我的学习笔记. PyQt5 出来有一段时间了, PyQt5 较之 PyQt4 有一些变化,而网上流传的几乎都是 PyQt4 的教程,照搬的话大多会出错. eric6 ...