1
This commit is contained in:
@@ -1,77 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.didispace</groupId>
|
||||
<artifactId>Chapter4-2-6</artifactId>
|
||||
<version>1.0.0</version>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>Chapter4-2-6</name>
|
||||
<description>Spring Boot project</description>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.3.2.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-logging</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-log4j</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-aop</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.mongodb</groupId>
|
||||
<artifactId>mongodb-driver</artifactId>
|
||||
<version>3.2.2</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -1,13 +0,0 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.didispace.aspect;
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.aspectj.lang.JoinPoint;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/23 下午4:08.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
public class RequestLog {
|
||||
|
||||
public String requestURL;
|
||||
public String requestURI;
|
||||
public String queryString;
|
||||
public String remoteAddr;
|
||||
public String remoteHost;
|
||||
public Integer remotePort;
|
||||
public String localAddr;
|
||||
public String localName;
|
||||
public String method;
|
||||
|
||||
public Map<String, String[]> parameters;
|
||||
public Map<String, String> headers;
|
||||
|
||||
public String classMethod;
|
||||
public String args;
|
||||
|
||||
public RequestLog(){
|
||||
|
||||
}
|
||||
|
||||
public RequestLog(HttpServletRequest request, JoinPoint joinPoint) {
|
||||
// 基本信息
|
||||
this.requestURL = request.getRequestURL().toString();
|
||||
this.requestURI = request.getRequestURI();
|
||||
this.queryString = request.getQueryString();
|
||||
this.remoteAddr = request.getRemoteAddr();
|
||||
this.remoteHost = request.getRemoteHost();
|
||||
this.remotePort = request.getRemotePort();
|
||||
this.localAddr = request.getLocalAddr();
|
||||
this.localName = request.getLocalName();
|
||||
this.method = request.getMethod();
|
||||
// 头信息
|
||||
this.headers = getHeadersInfo(request);
|
||||
// 参数
|
||||
this.parameters = request.getParameterMap();
|
||||
// 切入点信息
|
||||
this.classMethod = joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName();
|
||||
this.args = Arrays.toString(joinPoint.getArgs());
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取头部信息
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
Enumeration headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = (String) headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
String r;
|
||||
try {
|
||||
r = mapper.writeValueAsString(this);
|
||||
} catch (JsonProcessingException e) {
|
||||
r = getClass() + "转换JSON失败";
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,86 +0,0 @@
|
||||
package com.didispace.aspect;
|
||||
|
||||
import com.mongodb.BasicDBObject;
|
||||
import org.apache.log4j.Logger;
|
||||
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.springframework.core.annotation.Order;
|
||||
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.util.Arrays;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Web层日志切面
|
||||
*
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/17 上午10:42.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@Aspect
|
||||
@Order(1)
|
||||
@Component
|
||||
public class WebLogAspect {
|
||||
|
||||
private Logger logger = Logger.getLogger("mongodb");
|
||||
|
||||
@Pointcut("execution(public * com.didispace.web..*.*(..))")
|
||||
public void webLog(){}
|
||||
|
||||
@Before("webLog()")
|
||||
public void doBefore(JoinPoint joinPoint) throws Throwable {
|
||||
// 获取HttpServletRequest
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
HttpServletRequest request = attributes.getRequest();
|
||||
// 获取要记录的日志内容
|
||||
BasicDBObject logInfo = getBasicDBObject(request, joinPoint);
|
||||
logger.info(logInfo);
|
||||
}
|
||||
|
||||
|
||||
private BasicDBObject getBasicDBObject(HttpServletRequest request, JoinPoint joinPoint) {
|
||||
// 基本信息
|
||||
BasicDBObject r = new BasicDBObject();
|
||||
r.append("requestURL", request.getRequestURL().toString());
|
||||
r.append("requestURI", request.getRequestURI());
|
||||
r.append("queryString", request.getQueryString());
|
||||
r.append("remoteAddr", request.getRemoteAddr());
|
||||
r.append("remoteHost", request.getRemoteHost());
|
||||
r.append("remotePort", request.getRemotePort());
|
||||
r.append("localAddr", request.getLocalAddr());
|
||||
r.append("localName", request.getLocalName());
|
||||
r.append("method", request.getMethod());
|
||||
r.append("headers", getHeadersInfo(request));
|
||||
r.append("parameters", request.getParameterMap());
|
||||
r.append("classMethod", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());
|
||||
r.append("args", Arrays.toString(joinPoint.getArgs()));
|
||||
return r;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取头信息
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
private Map<String, String> getHeadersInfo(HttpServletRequest request) {
|
||||
Map<String, String> map = new HashMap<>();
|
||||
Enumeration headerNames = request.getHeaderNames();
|
||||
while (headerNames.hasMoreElements()) {
|
||||
String key = (String) headerNames.nextElement();
|
||||
String value = request.getHeader(key);
|
||||
map.put(key, value);
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.didispace.web;
|
||||
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* @author 程序猿DD
|
||||
* @version 1.0.0
|
||||
* @date 16/5/19 下午1:27.
|
||||
* @blog http://blog.didispace.com
|
||||
*/
|
||||
@RestController
|
||||
public class HelloController {
|
||||
|
||||
@RequestMapping(value = "/hello", method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
public String hello(@RequestParam String name) {
|
||||
return "Hello " + name;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
# LOG4J配置
|
||||
log4j.rootCategory=INFO, stdout
|
||||
log4j.logger.mongodb=INFO, mongodb
|
||||
|
||||
# 控制台输出
|
||||
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
|
||||
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
|
||||
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n
|
||||
|
||||
# mongodb输出
|
||||
log4j.appender.mongodb=com.didispace.log.MongoAppender
|
||||
log4j.appender.mongodb.connectionUrl=mongodb://localhost:27017
|
||||
log4j.appender.mongodb.databaseName=logs
|
||||
log4j.appender.mongodb.collectionName=logs_request
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.didispace;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@SpringApplicationConfiguration(classes = Application.class)
|
||||
public class ApplicationTests {
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getHello() throws Exception {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user