SpringBoot开发二十三-统一记录日志
统一记录日志
AlphaAspect
package com.nowcoder.community.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; //@Component
//@Aspect
public class AlphaAspect { @Pointcut("execution(* com.nowcoder.community.service.*.*(..))")
public void pointcut() { } @Before("pointcut()")
public void before() {
System.out.println("before");
} @After("pointcut()")
public void after() {
System.out.println("after");
} @AfterReturning("pointcut()")
public void afterRetuning() {
System.out.println("afterRetuning");
} @AfterThrowing("pointcut()")
public void afterThrowing() {
System.out.println("afterThrowing");
} @Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("around before");
Object obj = joinPoint.proceed();
System.out.println("around after");
return obj;
} }
ServiceLogAspect
package com.nowcoder.community.aspect; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date; @Component
@Aspect
public class ServiceLogAspect { private static final Logger logger = LoggerFactory.getLogger(ServiceLogAspect.class); @Pointcut("execution(* com.nowcoder.community.service.*.*(..))")
public void pointcut() { } @Before("pointcut()")
public void before(JoinPoint joinPoint) {
// 用户[1.2.3.4],在[xxx],访问了[com.nowcoder.community.service.xxx()].
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
String ip = request.getRemoteHost();
String now = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
String target = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
logger.info(String.format("用户[%s],在[%s],访问了[%s].", ip, now, target));
} }
SpringBoot开发二十三-统一记录日志的更多相关文章
- SpringBoot开发二十-Redis入门以及Spring整合Redis
安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis 的常用命 ...
- SpringBoot开发二十二-统一处理异常
需求介绍 首先服务端分为三层:表现层,业务层,数据层. 请求过来先到表现层,表现层调用业务层,然后业务层调用数据层. 那么数据层出现异常它会抛出异常,那异常肯定是抛给调用者也就是业务层,那么业务层会再 ...
- SpringBoot开发二十四-Redis入门以及Spring整合Redis
需求介绍 安装 Redis,熟悉 Redis 的命令以及整合Redis,在Spring 中使用Redis. 代码实现 Redis 内置了 16 个库,索引是 0-15 ,默认选择第 0 个 Redis ...
- SpringBoot开发二十一-发送私信
发送私信功能开发: 功能开发 数据访问层 message-mapper.xml 增加 <insert id="insertMessage" parameterType=&qu ...
- SpringBoot开发二十-私信列表
私信列表功能开发. 发送私信功能开发 首先创建一个实体类:Message package com.nowcoder.community.entity; import java.util.Date; p ...
- python运维开发(二十三)---tornado框架
内容目录: 路由系统 模板引擎 cookie 加密cookie 自定义api 自定义session 自定义form表单验证 异步非阻塞 web聊天室实例 路由系统 路由系统其实就是 url 和 类 的 ...
- SpringBoot开发二
需求介绍-Spring入门 主要是理解IOC,理解容器和Bean 代码 在Test里面利用getBean方法帮助我们看一下容器的创建: 那我首先要写一个Bean对象,假设是写一个访问数据库类. Alp ...
- spring-boot-route(二十三)开发微信公众号
在讲微信公众号开发之前,先来大概了解一下微信公众号.微信公众号大体上可以分为服务号和订阅号,订阅号和服务号的区别如下: 服务号可以申请微信支付功能. 服务号只能由企业申请,订阅号可以有企业或个人申请. ...
- Solon Web 开发,十二、统一的渲染控制
Solon Web 开发 一.开始 二.开发知识准备 三.打包与运行 四.请求上下文 五.数据访问.事务与缓存应用 六.过滤器.处理.拦截器 七.视图模板与Mvc注解 八.校验.及定制与扩展 九.跨域 ...
随机推荐
- 在使用XStream时没有processAnnotations方法
https://stackoverflow.com/questions/28770909/xstream-processannotations 我遇到这个问题的原因是xstream.jar的版本问题 ...
- php-高级计算器
HTML代码: <!doctype html><html lang="en"><head> <meta charset="UTF ...
- redis集群环境配置
为什么需要集群 redis是一个开源的 key->value 高速存储系统,但是由于redis单线程运行,在系统中,只能利用单核的性能 当redis的调用越来越频繁时,可能会出现redis过于繁 ...
- elf文件结构解读以及plt节got节的理解
前言: 熟悉elf文件结构是一件很不错的事,因为安卓中的so加固以及修复都是需要这些知识的,包括pwn里面的rop之类的,也都是 和got节,plt节息息相关的,个人建议是在搞懂elf文件结构后,自己 ...
- 《PHP设计模式大全》系列分享专栏
<PHP设计模式大全>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201739.html 文章 php设计模式介绍之编程惯用法第 ...
- 深入理解Java并发类——AQS
目录 什么是AQS 为什么需要AQS AQS的核心思想 AQS的内部数据和方法 如何利用AQS实现同步结构 ReentrantLock对AQS的利用 尝试获取锁 获取锁失败,排队竞争 参考 什么是AQ ...
- Linux | 通配符 & 转义符
通配符 我们在查看文件的时候,可能会出现只记得开头几个字母的情况,并且相似名称的文件还非常多的情况.还有小编想要删除一些开头一样的文件,这种情况下都可以使用通配符号: # 查找vcs文件 ls /de ...
- C语言:GB2312编码和GBK编码,将中文存储到计算机
计算机是一种改变世界的发明,很快就从美国传到了全球各地,得到了所有国家的认可,成为了一种不可替代的工具.计算机在广泛流行的过程中遇到的一个棘手问题就是字符编码,计算机是美国人发明的,它使用的是 ASC ...
- c语言:scanf(" %c",&bla); scanf("%c",&bla); 语句差别
%前有空格,%没有空格 scanf("%c",&c) 与 scanf(" %c",&c),后者只是在%前多了个空格,似乎没有什么区别,但使用起来 ...
- ELK多索引配置(filebeat)
nginx日志收集: #-------------------------输入 filebeat.inputs: #----------json日志---------- - type ...