Skip to content

Commit 91b01c6

Browse files
committed
Enable logging of API Calls and exceptions
1 parent f77f29b commit 91b01c6

9 files changed

+117
-88
lines changed

banq.iml

+1-80
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@
1616
</content>
1717
<orderEntry type="inheritedJdk" />
1818
<orderEntry type="sourceFolder" forTests="false" />
19-
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.1" level="project" />
20-
<orderEntry type="library" name="Maven: org.apache.httpcomponents.client5:httpclient5:5.0-alpha1" level="project" />
21-
<orderEntry type="library" name="Maven: org.apache.httpcomponents.core5:httpcore5:5.0-alpha1" level="project" />
22-
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
23-
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
2419
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE" level="project" />
2520
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.2.RELEASE" level="project" />
2621
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.2.RELEASE" level="project" />
@@ -41,8 +36,6 @@
4136
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.3" level="project" />
4237
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.7" level="project" />
4338
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.7.RELEASE" level="project" />
44-
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.7.RELEASE" level="project" />
45-
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.7.RELEASE" level="project" />
4639
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.7.RELEASE" level="project" />
4740
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.7.RELEASE" level="project" />
4841
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.7.RELEASE" level="project" />
@@ -86,81 +79,9 @@
8679
<orderEntry type="library" name="Maven: org.springframework:spring-tx:4.3.7.RELEASE" level="project" />
8780
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.24" level="project" />
8881
<orderEntry type="library" name="Maven: org.springframework:spring-aspects:4.3.7.RELEASE" level="project" />
89-
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.41" level="project" />
90-
<orderEntry type="library" name="Maven: com.github.briandilley.jsonrpc4j:jsonrpc4j:1.5.0" level="project" />
91-
<orderEntry type="library" scope="RUNTIME" name="Maven: net.iharder:base64:2.3.9" level="project" />
92-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-core:2.8.7" level="project" />
93-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-annotations:2.8.0" level="project" />
94-
<orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.1" level="project" />
95-
<orderEntry type="library" name="Maven: org.apache.httpcomponents.client5:httpclient5:5.0-alpha1" level="project" />
96-
<orderEntry type="library" name="Maven: org.apache.httpcomponents.core5:httpcore5:5.0-alpha1" level="project" />
97-
<orderEntry type="library" name="Maven: commons-logging:commons-logging:1.2" level="project" />
98-
<orderEntry type="library" name="Maven: commons-codec:commons-codec:1.10" level="project" />
99-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-web:1.5.2.RELEASE" level="project" />
100-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter:1.5.2.RELEASE" level="project" />
101-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot:1.5.2.RELEASE" level="project" />
102-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-autoconfigure:1.5.2.RELEASE" level="project" />
103-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-logging:1.5.2.RELEASE" level="project" />
104-
<orderEntry type="library" name="Maven: ch.qos.logback:logback-classic:1.1.11" level="project" />
105-
<orderEntry type="library" name="Maven: ch.qos.logback:logback-core:1.1.11" level="project" />
106-
<orderEntry type="library" name="Maven: org.slf4j:jul-to-slf4j:1.7.24" level="project" />
107-
<orderEntry type="library" name="Maven: org.slf4j:log4j-over-slf4j:1.7.24" level="project" />
108-
<orderEntry type="library" scope="RUNTIME" name="Maven: org.yaml:snakeyaml:1.17" level="project" />
109-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-tomcat:1.5.2.RELEASE" level="project" />
110-
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-core:8.5.11" level="project" />
111-
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-el:8.5.11" level="project" />
112-
<orderEntry type="library" name="Maven: org.apache.tomcat.embed:tomcat-embed-websocket:8.5.11" level="project" />
113-
<orderEntry type="library" name="Maven: org.hibernate:hibernate-validator:5.3.4.Final" level="project" />
114-
<orderEntry type="library" name="Maven: javax.validation:validation-api:1.1.0.Final" level="project" />
115-
<orderEntry type="library" name="Maven: org.jboss.logging:jboss-logging:3.3.0.Final" level="project" />
116-
<orderEntry type="library" name="Maven: com.fasterxml:classmate:1.3.3" level="project" />
117-
<orderEntry type="library" name="Maven: com.fasterxml.jackson.core:jackson-databind:2.8.7" level="project" />
118-
<orderEntry type="library" name="Maven: org.springframework:spring-web:4.3.7.RELEASE" level="project" />
11982
<orderEntry type="library" name="Maven: org.springframework:spring-aop:4.3.7.RELEASE" level="project" />
12083
<orderEntry type="library" name="Maven: org.springframework:spring-beans:4.3.7.RELEASE" level="project" />
121-
<orderEntry type="library" name="Maven: org.springframework:spring-context:4.3.7.RELEASE" level="project" />
122-
<orderEntry type="library" name="Maven: org.springframework:spring-webmvc:4.3.7.RELEASE" level="project" />
123-
<orderEntry type="library" name="Maven: org.springframework:spring-expression:4.3.7.RELEASE" level="project" />
124-
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-starter-test:1.5.2.RELEASE" level="project" />
125-
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test:1.5.2.RELEASE" level="project" />
126-
<orderEntry type="library" scope="TEST" name="Maven: org.springframework.boot:spring-boot-test-autoconfigure:1.5.2.RELEASE" level="project" />
127-
<orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" />
128-
<orderEntry type="library" scope="TEST" name="Maven: org.assertj:assertj-core:2.6.0" level="project" />
129-
<orderEntry type="library" scope="TEST" name="Maven: org.mockito:mockito-core:1.10.19" level="project" />
130-
<orderEntry type="library" scope="TEST" name="Maven: org.objenesis:objenesis:2.1" level="project" />
131-
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
132-
<orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-library:1.3" level="project" />
133-
<orderEntry type="library" scope="TEST" name="Maven: org.skyscreamer:jsonassert:1.4.0" level="project" />
134-
<orderEntry type="library" scope="TEST" name="Maven: com.vaadin.external.google:android-json:0.0.20131108.vaadin1" level="project" />
135-
<orderEntry type="library" name="Maven: org.springframework:spring-core:4.3.7.RELEASE" level="project" />
136-
<orderEntry type="library" scope="TEST" name="Maven: org.springframework:spring-test:4.3.7.RELEASE" level="project" />
137-
<orderEntry type="library" scope="TEST" name="Maven: com.jayway.jsonpath:json-path:2.2.0" level="project" />
138-
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:json-smart:2.2.1" level="project" />
139-
<orderEntry type="library" scope="TEST" name="Maven: net.minidev:accessors-smart:1.1" level="project" />
140-
<orderEntry type="library" scope="TEST" name="Maven: org.ow2.asm:asm:5.0.3" level="project" />
141-
<orderEntry type="library" name="Maven: org.slf4j:slf4j-api:1.7.24" level="project" />
142-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-data-jpa:1.5.2.RELEASE" level="project" />
143-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-aop:1.5.2.RELEASE" level="project" />
144-
<orderEntry type="library" name="Maven: org.aspectj:aspectjweaver:1.8.9" level="project" />
145-
<orderEntry type="library" name="Maven: org.springframework.boot:spring-boot-starter-jdbc:1.5.2.RELEASE" level="project" />
146-
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-jdbc:8.5.11" level="project" />
147-
<orderEntry type="library" name="Maven: org.apache.tomcat:tomcat-juli:8.5.11" level="project" />
148-
<orderEntry type="library" name="Maven: org.springframework:spring-jdbc:4.3.7.RELEASE" level="project" />
149-
<orderEntry type="library" name="Maven: org.hibernate:hibernate-core:5.0.12.Final" level="project" />
150-
<orderEntry type="library" name="Maven: org.hibernate.javax.persistence:hibernate-jpa-2.1-api:1.0.0.Final" level="project" />
151-
<orderEntry type="library" name="Maven: org.javassist:javassist:3.21.0-GA" level="project" />
152-
<orderEntry type="library" name="Maven: antlr:antlr:2.7.7" level="project" />
153-
<orderEntry type="library" name="Maven: org.jboss:jandex:2.0.0.Final" level="project" />
154-
<orderEntry type="library" name="Maven: dom4j:dom4j:1.6.1" level="project" />
155-
<orderEntry type="library" name="Maven: org.hibernate.common:hibernate-commons-annotations:5.0.1.Final" level="project" />
156-
<orderEntry type="library" name="Maven: org.hibernate:hibernate-entitymanager:5.0.12.Final" level="project" />
157-
<orderEntry type="library" name="Maven: javax.transaction:javax.transaction-api:1.2" level="project" />
158-
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-jpa:1.11.1.RELEASE" level="project" />
159-
<orderEntry type="library" name="Maven: org.springframework.data:spring-data-commons:1.13.1.RELEASE" level="project" />
160-
<orderEntry type="library" name="Maven: org.springframework:spring-orm:4.3.7.RELEASE" level="project" />
161-
<orderEntry type="library" name="Maven: org.springframework:spring-tx:4.3.7.RELEASE" level="project" />
162-
<orderEntry type="library" name="Maven: org.slf4j:jcl-over-slf4j:1.7.24" level="project" />
163-
<orderEntry type="library" name="Maven: org.springframework:spring-aspects:4.3.7.RELEASE" level="project" />
84+
<orderEntry type="library" name="Maven: org.aspectj:aspectjtools:1.8.9" level="project" />
16485
<orderEntry type="library" name="Maven: mysql:mysql-connector-java:5.1.41" level="project" />
16586
<orderEntry type="library" name="Maven: com.github.briandilley.jsonrpc4j:jsonrpc4j:1.5.0" level="project" />
16687
<orderEntry type="library" scope="RUNTIME" name="Maven: net.iharder:base64:2.3.9" level="project" />

pom.xml

+14-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
</parent>
1818

1919
<dependencies>
20+
<!-- Spring -->
2021
<dependency>
2122
<groupId>org.springframework.boot</groupId>
2223
<artifactId>spring-boot-starter-web</artifactId>
@@ -32,12 +33,24 @@
3233
<scope>test</scope>
3334
</dependency>
3435

35-
<!-- JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
36+
<!-- Spring - JPA Data (We are going to use Repositories, Entities, Hibernate, etc...) -->
3637
<dependency>
3738
<groupId>org.springframework.boot</groupId>
3839
<artifactId>spring-boot-starter-data-jpa</artifactId>
3940
</dependency>
4041

42+
<!-- Spring - AOP -->
43+
<dependency>
44+
<groupId>org.springframework</groupId>
45+
<artifactId>spring-aop</artifactId>
46+
</dependency>
47+
48+
<!-- AspectJ for Spring AOP -->
49+
<dependency>
50+
<groupId>org.aspectj</groupId>
51+
<artifactId>aspectjtools</artifactId>
52+
</dependency>
53+
4154
<!-- Use MySQL Connector-J -->
4255
<dependency>
4356
<groupId>mysql</groupId>

src/main/java/honours/ing/banq/Application.java

+2
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.context.annotation.ComponentScan;
6+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
67
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
78

89
/**
910
* @author Jeffrey Bakker
1011
*/
1112
@SpringBootApplication
13+
@EnableAspectJAutoProxy
1214
@ComponentScan("honours.ing")
1315
@EnableWebMvc
1416
public class Application {

src/main/java/honours/ing/banq/auth/AuthService.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@
1010
* @author jeffrey
1111
* @since 14-5-17
1212
*/
13-
@JsonRpcService("/api/auth")
14-
public interface AuthService {
15-
16-
AuthToken getAuthToken(@JsonRpcParam("username") String username,
17-
@JsonRpcParam("password") String password) throws AuthenticationError;
13+
public interface AuthService extends AuthServiceAPI {
1814

1915
Customer getAuthorizedCustomer(String token) throws NotAuthorizedError;
2016

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package honours.ing.banq.auth;
2+
3+
import com.googlecode.jsonrpc4j.JsonRpcParam;
4+
import com.googlecode.jsonrpc4j.JsonRpcService;
5+
6+
/**
7+
* @author Kevin Witlox
8+
* @since 21-8-2017.
9+
*/
10+
@JsonRpcService("/api/auth")
11+
public interface AuthServiceAPI {
12+
13+
AuthToken getAuthToken(@JsonRpcParam("username") String username,
14+
@JsonRpcParam("password") String password) throws AuthenticationError;
15+
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package honours.ing.banq.auth;
2+
3+
import com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Component;
6+
7+
/**
8+
* @author Kevin Witlox
9+
* @since 21-8-2017.
10+
*/
11+
@Component
12+
@AutoJsonRpcServiceImpl
13+
public class AuthServiceAPIImpl implements AuthServiceAPI {
14+
15+
@Autowired
16+
private AuthService authService;
17+
18+
@Override
19+
public AuthToken getAuthToken(String username, String password) throws AuthenticationError {
20+
return authService.getAuthToken(username, password);
21+
}
22+
23+
}

src/main/java/honours/ing/banq/auth/AuthServiceImpl.java

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
* @since 14-5-17
2626
*/
2727
@Service
28-
@AutoJsonRpcServiceImpl
2928
@Transactional
3029
public class AuthServiceImpl implements AuthService {
3130

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package honours.ing.banq.log;
2+
3+
import org.aspectj.lang.JoinPoint;
4+
import org.aspectj.lang.annotation.After;
5+
import org.aspectj.lang.annotation.AfterThrowing;
6+
import org.aspectj.lang.annotation.Aspect;
7+
import org.aspectj.lang.annotation.Pointcut;
8+
import org.slf4j.LoggerFactory;
9+
import org.springframework.stereotype.Component;
10+
11+
/**
12+
* @author Kevin Witlox
13+
* @since 21-8-2017.
14+
*/
15+
@Aspect
16+
@Component
17+
public class Logger {
18+
19+
private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());
20+
21+
@Pointcut("execution(* *.*(..))")
22+
protected void allMethod() {
23+
}
24+
25+
@Pointcut("within(@com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImpl *)")
26+
protected void service() {
27+
}
28+
29+
@After("allMethod() && service()")
30+
public void logAfter(JoinPoint point) {
31+
StringBuilder builder = new StringBuilder(point.getSignature().getName());
32+
builder.append("(");
33+
34+
Object[] args = point.getArgs();
35+
for (int i = 0; i < args.length; i++) {
36+
Object object = args[i];
37+
builder.append(object.toString().length() == 255 ? "token" : object.toString());
38+
39+
if (i + 1 < args.length) {
40+
builder.append(", ");
41+
}
42+
}
43+
44+
builder.append(");");
45+
logger.info(builder.toString());
46+
}
47+
48+
@AfterThrowing(pointcut = "service() && allMethod()", throwing = "exception")
49+
public void logAfterThrowing(JoinPoint joinPoint, Throwable exception) {
50+
StringBuilder builder = new StringBuilder(exception.getClass().getSimpleName());
51+
builder.append("(").append(exception.getMessage()).append(")");
52+
builder.append(" thrown by ").append(joinPoint.getSignature().getName());
53+
logger.error(builder.toString());
54+
}
55+
56+
57+
}

src/main/resources/application.properties

+3-1
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ spring.jpa.hibernate.ddl-auto=update
22
spring.datasource.url=jdbc:mysql://localhost:3306/ing_db
33
spring.datasource.username=ing_project
44
spring.datasource.password=localhost1234
5-
server.port=8080
5+
server.port=8080
6+
7+
logging.pattern.console=${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}

0 commit comments

Comments
 (0)