Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
372 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8" inherit-compiler-output="false"> | ||
<output url="file://$MODULE_DIR$/target/classes" /> | ||
<output-test url="file://$MODULE_DIR$/target/test-classes" /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" /> | ||
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" /> | ||
<excludeFolder url="file://$MODULE_DIR$/target" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.16" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-log4j:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.13" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.13" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.13" level="project" /> | ||
<orderEntry type="library" name="Maven: org.slf4j:slf4j-log4j12:1.7.13" level="project" /> | ||
<orderEntry type="library" name="Maven: log4j:log4j:1.2.17" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.0.30" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.0.30" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-logging-juli:8.0.30" level="project" /> | ||
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.0.30" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-validation:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.2.2.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.0.Final" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.1.0" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.6.5" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.6.5" level="project" /> | ||
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.6.5" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: aopalliance:aopalliance:1.0" level="project" /> | ||
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.8" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:1.3.2.RELEASE" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" /> | ||
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.2.4.RELEASE" level="project" /> | ||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver:3.2.2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.mongodb:mongodb-driver-core:3.2.2" level="project" /> | ||
<orderEntry type="library" name="Maven: org.mongodb:bson:3.2.2" level="project" /> | ||
</component> | ||
</module> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
<?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>log4j-mongodb</artifactId> | ||
<version>1.0.0</version> | ||
<packaging>jar</packaging> | ||
|
||
<name>log4j-mongodb</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> |
13 changes: 13 additions & 0 deletions
13
16-log4j-mongodb/src/main/java/com/didispace/Application.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
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); | ||
} | ||
|
||
} |
86 changes: 86 additions & 0 deletions
86
16-log4j-mongodb/src/main/java/com/didispace/aspect/WebLogAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
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; | ||
} | ||
|
||
} | ||
|
78 changes: 78 additions & 0 deletions
78
16-log4j-mongodb/src/main/java/com/didispace/log/MongoAppender.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.didispace.log; | ||
|
||
import com.mongodb.BasicDBObject; | ||
import com.mongodb.MongoClient; | ||
import com.mongodb.MongoClientURI; | ||
import com.mongodb.client.MongoCollection; | ||
import com.mongodb.client.MongoDatabase; | ||
import org.apache.log4j.AppenderSkeleton; | ||
import org.apache.log4j.spi.LoggingEvent; | ||
|
||
/** | ||
* 日志插入MongoDB | ||
* | ||
* @author 程序猿DD | ||
* @version 1.0.0 | ||
* @date 16/5/23 下午4:53. | ||
* @blog http://blog.didispace.com | ||
*/ | ||
public class MongoAppender extends AppenderSkeleton { | ||
|
||
private MongoClient mongoClient; | ||
private MongoDatabase mongoDatabase; | ||
private MongoCollection<BasicDBObject> logsCollection; | ||
|
||
private String connectionUrl; | ||
private String databaseName; | ||
private String collectionName; | ||
|
||
@Override | ||
protected void append(LoggingEvent loggingEvent) { | ||
|
||
if(mongoDatabase == null) { | ||
MongoClientURI connectionString = new MongoClientURI(connectionUrl); | ||
mongoClient = new MongoClient(connectionString); | ||
mongoDatabase = mongoClient.getDatabase(databaseName); | ||
logsCollection = mongoDatabase.getCollection(collectionName, BasicDBObject.class); | ||
} | ||
logsCollection.insertOne((BasicDBObject) loggingEvent.getMessage()); | ||
|
||
} | ||
|
||
@Override | ||
public void close() { | ||
if(mongoClient != null) { | ||
mongoClient.close(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean requiresLayout() { | ||
return false; | ||
} | ||
|
||
public String getConnectionUrl() { | ||
return connectionUrl; | ||
} | ||
|
||
public void setConnectionUrl(String connectionUrl) { | ||
this.connectionUrl = connectionUrl; | ||
} | ||
|
||
public String getDatabaseName() { | ||
return databaseName; | ||
} | ||
|
||
public void setDatabaseName(String databaseName) { | ||
this.databaseName = databaseName; | ||
} | ||
|
||
public String getCollectionName() { | ||
return collectionName; | ||
} | ||
|
||
public void setCollectionName(String collectionName) { | ||
this.collectionName = collectionName; | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
16-log4j-mongodb/src/main/java/com/didispace/web/HelloController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
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; | ||
} | ||
|
||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 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 |
25 changes: 25 additions & 0 deletions
25
16-log4j-mongodb/src/test/java/com/didispace/ApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
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 { | ||
|
||
} | ||
|
||
} |