1.引入包
configurations {
providedRuntime
// remove default logger
all*.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
dependencies {
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-data-rest')
compile('org.springframework.boot:spring-boot-starter-freemarker')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-web-services')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compile('org.springframework.boot:spring-boot-starter-log4j2:2.0.3.RELEASE')
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
compileOnly('org.projectlombok:lombok')
runtime('mysql:mysql-connector-java')
compile ('com.alibaba:fastjson:1.2.47')
testCompile('org.springframework.boot:spring-boot-starter-test')
} 2.在路径rescourse下配置log4j2.properties
name = PropertiesConfig
#property.filename = target/logs
property.filename = D:\\log #appenders = console, file
#配置值是appender的类型,并不是具体appender实例的name
appenders = rolling appender.rolling.type = RollingFile
appender.rolling.name = RollingLogFile
appender.rolling.fileName=${filename}/automationlogs.log
appender.rolling.filePattern = ${filename}/automationlogs-%d{MM-dd-yy-HH-mm-ss}-%i.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5 rootLogger.level = trace
rootLogger.appenderRef.rolling.ref = RollingLogFile #rootLogger.appenderRef.rolling.ref = rolling 3.java ,其中@Slf4j注解相当于声明变量,可以直接使用log.info()
@WebFilter(filterName = "RequestLog", urlPatterns = "/*")
@SpringBootApplication
//重点
@ServletComponentScan
public class DemoApplication {启动类}
这两个注解相当于配置过滤器
/*
* Project: somp.cusma
*
* File Created at 2017年11月17日
*
* Copyright 2016 CMCC Corporation Limited.
* All rights reserved.
*
* This software is the confidential and proprietary information of
* ZYHY Company. ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license.
*/
package com.sunreal.demo.recordcore.domain.login.filter; import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils; import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration; /**
* @Type RequestLog.java
* @Desc
* @version
*/
@Component("requestLog")
//重点
@WebFilter(filterName = "RequestLog", urlPatterns = "/*")
@Slf4j
public class RequestLog implements Filter { @Override
public void init(FilterConfig filterConfig) throws ServletException { } @Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain)
throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) servletRequest; log.info("<=========================== Request Url ===========================>"); log.info(request.getRequestURL().toString()); log.info("<========================= Header Attribute ========================>");
Enumeration<?> e = request.getHeaderNames();
while (e.hasMoreElements()) {
String name = (String) e.nextElement();
String value = request.getHeader(name);
log.info(name + " = " + value);
}
log.info("<========================= Header Attribute ========================>"); String queryString = request.getQueryString();
if(!StringUtils.isEmpty(queryString)){
log.info("<========================= Get String ========================>");
log.info(queryString);
} log.info("<========================= Request Param ===========================>");
Enumeration<?> eq = request.getParameterNames();
while (eq.hasMoreElements()) {
String name = (String) eq.nextElement();
String value = request.getParameter(name);
log.info(name + " = " + value);
}
log.info("<========================= Request Param ===========================>"); filterChain.doFilter(servletRequest, servletResponse); } @Override
public void destroy() { } } /**
* Revision history
* -------------------------------------------------------------------------
*
* Date Author Note
* -------------------------------------------------------------------------
*/
上述是日志的配置情况,
下面介绍aop切入日志的的方法
@Target({ ElementType.PARAMETER, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemControllerLog { String description() default ""; }
@RequestMapping(value = "/htgl/addNewRecruitment", method = RequestMethod.POST)
@SystemControllerLog(description="登出")
public SunrealResult addNewRecruitment(@RequestBody RecruitmentInformation recruitmentInformation) {
System.out.println("进入Sysmaintain--provider--addNewRecruitment");
try {
return recruitmentInformationService.addNewRecruitment(recruitmentInformation);
} catch (Exception e) {
e.printStackTrace();
return SunrealResultUtil.error(-1, "接口服务异常,请重新尝试!");
}
}
@Component //交予容器管理
@Aspect //代理
public class LoggerBefore {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(LoggerBefore.class); /**
* OperateLogService
*/
@Autowired
private SysuserOperatehistoryMapper logService; @Pointcut("@annotation(com.sunreal.sysmaintain_provider.util.SystemControllerLog)")
public void controllerAspect() { } @SuppressWarnings("rawtypes")
public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(SystemControllerLog.class).description();
break;
}
}
}
return description;
} /**
* @param
* @throws Throwable
*/
@After("controllerAspect()")
public void before(JoinPoint joinPoint) throws Throwable {
// EnterpriseOperator curUser = null;
// String userIp = null;
// String sessionId = null;
String clazzName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] objects = joinPoint.getArgs();
Class targetClass = Class.forName(clazzName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == objects.length) {
description = method.getAnnotation(SystemControllerLog.class).description();
break;
}
}
}
logger.info("clazzName=" + clazzName + ",method=" + methodName + ",args=" + objects);
// Subject subject = SecurityUtils.getSubject();
// userIp=subject.getSession().getHost();
// sessionId=subject.getSession().getId().toString();
// Object user = subject.getPrincipal();
// logger.info(user);
// if (user instanceof EnterpriseOperator) {
// curUser = (EnterpriseOperator) user;
// OperateLog log = new OperateLog();
// log.setUserId(String.valueOf(curUser.getId()));
// log.setUserName(curUser.getUsername());
// log.setOperateTime(new Date());
// log.setClassName(clazzName);
// log.setOperateMethod(methodName);
// log.setUserIp(userIp);
// log.setSessionId(sessionId);
// log.setOperateCnName(description);
// logService.addLog(log);
// }
Operateinfo operateinfo = new Operateinfo();
operateinfo.setOperatedate(new Date());
operateinfo.setOperateuserid(12212321);
operateinfo.setOperatedetail(methodName);
long logId = new IdWorker(1,1).nextId();
operateinfo.setOperateId(logId);
logService.insertLog(operateinfo);
}
}

@SpringBootApplication
//重点
@ServletComponentScan
public class DemoApplication {

spring boot的gradle整合日志的更多相关文章

  1. Spring Boot 2.x整合Redis

    最近在学习Spring Boot 2.x整合Redis,在这里和大家分享一下,希望对大家有帮助. Redis是什么 Redis 是开源免费高性能的key-value数据库.有以下的优势(源于Redis ...

  2. Spring Boot 2.X整合Spring-cache,让你的网站速度飞起来

    计算机领域有人说过一句名言:“计算机科学领域的任何问题都可以通过增加一个中间层来解决”,今天我们就用Spring-cache给网站添加一层缓存,让你的网站速度飞起来. 本文目录 一.Spring Ca ...

  3. Spring Boot 2.0 整合携程Apollo配置中心

    原文:https://www.jianshu.com/p/23d695af7e80 Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够 ...

  4. [转] 使用Spring Boot和Gradle创建项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  5. 使用Spring Boot和Gradle创建AngularJS项目

    Spring Boot 是由 Pivotal 团队提供的全新框架,其设计目的是用来简化新 Spring 应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的 ...

  6. spring boot 2.0 整合 elasticsearch6.5.3,spring boot 2.0 整合 elasticsearch NoNodeAvailableException

    原文地址:spring boot 2.0 整合 elasticsearch NoNodeAvailableException 原文说的有点问题,下面贴出我的配置: 原码云项目地址:https://gi ...

  7. Spring Boot入门 and Spring Boot与ActiveMQ整合

    1.Spring Boot入门 1.1什么是Spring Boot Spring 诞生时是 Java 企业版(Java Enterprise Edition,JEE,也称 J2EE)的轻量级代替品.无 ...

  8. Spring Boot系列一:默认日志logback配置解析

    前言 今天来介绍下Spring Boot如何配置日志logback,我刚学习的时候,是带着下面几个问题来查资料的,你呢 如何引入日志? 日志输出格式以及输出方式如何配置? 代码中如何使用? 正文 Sp ...

  9. Spring Boot和Dubbo整合

    provider端 POM依赖 <dependencies> <dependency> <groupId>org.springframework.boot</ ...

随机推荐

  1. windows linux 通过SSH X11Forwrding 使用图形化界面

    有时候,我们需要在命令行中使用远程的GUI程序,这样我们就需要x11转发的来进行访问: Linux平台下不需要特别的配置,假如我们要远程的机器是centos机器,只要做如下配置即可: #vi /etc ...

  2. [PHP] 新版本PHP7.4与新版本MySQL8认证问题

    mysql8的默认密码加密方式是caching_sha2_password,PHP7.4连接mysql的加密方式也为caching_sha2_password,这个地方要注意. 当为了兼容旧版的客户端 ...

  3. 【bzoj1997】[Hnoi2010]Planar(平面图+2-sat)

    传送门 几乎和这个题一样,就不说题意了,比较特殊的点就是,这里有个结论: 平面图的边数\(m<3n-6\),\(n\)为点数. 所以我们可以通过这个减枝,\(m\)较大时直接输出\(no\).小 ...

  4. 富文本编辑器Simditor

    文档地址:https://simditor.tower.im/docs/doc-usage.html 父组件: options: { placeHolder: 'this is placeHolder ...

  5. BASIC合集

    握手包 给你握手包,flag是Flag_is_here这个AP的密码,自己看着办吧. 提交格式:flag{WIFI密码} 破解wifi密码 丢到kali,用aircrack-ng kali有一个包含常 ...

  6. CentOs篇

    Advanced-高级配置.Security-安全.Boot-启动引导: 1.Removable Devices-移动设备 2.Hard Drive-本地硬盘 3.CD-ROM- Drive-光盘 4 ...

  7. CF1041C Coffee Break

    CF1041C Coffee Break 题目大意: 给定nn个数和一个kk,这nn个数都不超过mm 每次从没被去掉的数里面选一个数aa,去掉aa,然后可以任意一个b(b>a+k)b(b> ...

  8. 使用system V实现读者写者问题

    #include <stdio.h> #include <sys/sem.h> #include <sys/ipc.h> #include <string.h ...

  9. python运维开发常用模块(8)EXCEL操作模块XlsxWriter

    1.excel介绍 Excel是当今最流行的电子表格处理软件,支持丰富的计算函数及 图表,在系统运营方面广泛用于运营数据报表,比如业务质量.资源利 用.安全扫描等报表,同时也是应用系统常见的文件导出格 ...

  10. Java中的Object类的几个方法

    Object类被称为上帝类,也被称为祖宗类.在定义Java类时,如果没有指定父类,那么默认都会去继承Object类.配合Java的向上类型转换,借助Object类就可以完成很多工作了. 在Object ...