forked from forcedotcom/EMP-Connector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingListener.java
52 lines (42 loc) · 1.54 KB
/
LoggingListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package com.salesforce.emp.connector.example;
import org.cometd.bayeux.Message;
import org.cometd.bayeux.client.ClientSessionChannel;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LoggingListener implements ClientSessionChannel.MessageListener {
private boolean logSuccess;
private boolean logFailure;
public LoggingListener() {
this.logSuccess = true;
this.logFailure = true;
}
public LoggingListener(boolean logSuccess, boolean logFailure) {
this.logSuccess = logSuccess;
this.logFailure = logFailure;
}
@Override
public void onMessage(ClientSessionChannel clientSessionChannel, Message message) {
if (logSuccess && message.isSuccessful()) {
System.out.println(">>>>");
printPrefix();
System.out.println("Success:[" + clientSessionChannel.getId() + "]");
System.out.println(message);
System.out.println("<<<<");
}
if (logFailure && !message.isSuccessful()) {
System.out.println(">>>>");
printPrefix();
System.out.println("Failure:[" + clientSessionChannel.getId() + "]");
System.out.println(message);
System.out.println("<<<<");
}
}
private void printPrefix() {
System.out.print("[" + timeNow() + "] ");
}
private String timeNow() {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Date now = new Date();
return dateFormat.format(now);
}
}