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. 海思Hi3519A MPP从入门到精通(二 系统控制)

    系统控制根据 Hi35xx 芯片特性,完成硬件各个部件的复位.基本初始化工作,同时负责完成 MPP(Media Process Platform 媒体处理平台)系统各个业务模块的初始化.去初始化以及管 ...

  2. emacs 窗口控制

    1,调整窗口大小 c-c ^ 窗口变高 c-c } 窗口变宽 c-c { 窗口变窄 2,窗口间移动 ;;这一条语句的作用是让 windmove 在边缘的窗口也能正常运作.举个例子,当前窗口已\\ 经是 ...

  3. 基于tiny4412的Linux内核移植 --- aliases节点解析【转】

    转自:https://www.cnblogs.com/pengdonglin137/p/5252348.html 阅读目录(Content) 作者信息 平台简介 正文 回到顶部(go to top) ...

  4. fiddler---Fiddler查看get和post请求

    前几篇写了Fiddler的一些功能介绍,今天我们一起学习下如何通过fiddler查看get请求和post请求和get,post区别 get请求 1.启动fiddler,抓取安静博客地址 2.通过fid ...

  5. ScratchJr是什么,有什么作用

    什么是ScratchJr? ScratchJr是一个入门级的编程语言,可以让5到7岁的小朋友去创建他们的互动故事和游戏.孩子们使用图形化的程序积木让角色移动.跳跃.舞蹈.唱歌.孩子们可以利用绘图编辑器 ...

  6. CF1178 F1 Short Colorful Strip

    题目链接 题意 有个长度为\(m\)公分的布,要在上面每公分都染上颜色,整块布染恰好\(n(n=m)\)种颜色.颜色标号从\(1\)到\(n\).染色需遵循: 1.从颜色\(1\)到颜色\(n\)依次 ...

  7. Codeforces Round #596 (Div. 2, based on Technocup 2020 Elimination Round 2) F. Tree Factory 构造题

    F. Tree Factory Bytelandian Tree Factory produces trees for all kinds of industrial applications. Yo ...

  8. Luogu P5022 旅行

    开始写复赛题了 先放张图纪念我惨烈的卡常之路 不说了,简直悲伤 题目链接 思路么..不想写了 Code //不要在意四十行超级加速,卡常用的 #include<bits/stdc++.h> ...

  9. 【前端知识体系-JS相关】ES6专题系列总结

    1.如何搭建ES6的webpack开发环境? 安装Node环境 node -v // 10.14.1 安装NPM环境 npm -v // 6.4.1 安装babel npm install @babe ...

  10. Installing on Kubernetes with NATS Operator

    https://github.com/nats-io/nats-operator https://hub.helm.sh/charts/bitnami/nats https://github.com/ ...