Skip to content

Commit 77afb6d

Browse files
committed
softlayer#469 Add event logs java example
1 parent 1afad75 commit 77afb6d

1 file changed

Lines changed: 154 additions & 0 deletions

File tree

content/java/EventLogs.java.md

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
---
2+
title: "Working with Event Logs"
3+
description: "Some examples of Event Log"
4+
date: "2021-04-14"
5+
classes:
6+
- "SoftLayer_Event_Log"
7+
tags:
8+
- "eventlogs"
9+
- "resultlimit"
10+
---
11+
# Event Log
12+
This example deals with a few ways of pulling data from [SoftLayer_Event_Log](https://sldn.softlayer.com/reference/services/SoftLayer_Event_Log/). Also, uses the latest version of the SoftLayer API Client for Java (`v0.3.2` at the time of publishing this example), which does not support object filters available in the SoftLayer API, so programmatic filters are used instead.
13+
14+
```java
15+
import com.google.gson.Gson;
16+
import com.google.gson.GsonBuilder;
17+
import com.softlayer.api.ApiClient;
18+
import com.softlayer.api.RestApiClient;
19+
import com.softlayer.api.ResultLimit;
20+
import com.softlayer.api.service.event.Log;
21+
22+
import java.time.LocalDate;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
27+
28+
public class EventLogExample {
29+
30+
private final ApiClient client;
31+
private final Log.Service logService;
32+
33+
public EventLogExample() {
34+
String username = "set-me";
35+
String apiKey = "set-me";
36+
client = new RestApiClient().withCredentials(username, apiKey).withLoggingEnabled();
37+
logService = Log.service(client);
38+
}
39+
40+
public static void main(String[] args) {
41+
EventLogExample eventLogExample = new EventLogExample();
42+
eventLogExample.getAllTypes();
43+
eventLogExample.getUserTypes();
44+
eventLogExample.allLogs();
45+
eventLogExample.loginLogs();
46+
eventLogExample.recentLogs();
47+
eventLogExample.systemLogs();
48+
}
49+
50+
/**
51+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllEventObjectNames.json with no body
52+
*/
53+
private void getAllTypes() {
54+
print(logService.getAllEventObjectNames());
55+
}
56+
57+
/***
58+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllUserTypes.json with no body
59+
*/
60+
private void getUserTypes() {
61+
print(logService.getAllUserTypes());
62+
}
63+
64+
/***
65+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllObjects.json?resultLimit=0,50 with no body
66+
*/
67+
private void systemLogs() {
68+
List<Log> logs=getAllEvents(false);
69+
String systemName="SYSTEM";
70+
List<Log> systemLogs = logs.stream()
71+
.filter(log -> systemName.equalsIgnoreCase(log.getEventName()))
72+
.collect(Collectors.toList());
73+
printEventLogs(systemLogs);
74+
}
75+
76+
/**
77+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllObjects.json?resultLimit=0,50 with no body
78+
*/
79+
private void recentLogs() {
80+
LocalDate daysAgo = LocalDate.now().minusDays(30);
81+
List<Log> logs=getAllEvents(false);
82+
List<Log> recentLogs = logs.stream()
83+
.filter(log -> log.getEventCreateDate()
84+
.toZonedDateTime()
85+
.toLocalDate()
86+
.compareTo(daysAgo)>0)
87+
.collect(Collectors.toList());
88+
printEventLogs(recentLogs);
89+
}
90+
91+
/**
92+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllObjects.json?resultLimit=0,50 with no body
93+
*/
94+
private void loginLogs() {
95+
List<Log> logs=getAllEvents(false);
96+
String loginName="login";
97+
List<Log> loginLogs = logs.stream()
98+
.filter(log -> log
99+
.getEventName()
100+
.toLowerCase()
101+
.contains(loginName))
102+
.collect(Collectors.toList());
103+
printEventLogs(loginLogs);
104+
}
105+
106+
/**
107+
* Running GET on https://api.softlayer.com/rest/v3.1/SoftLayer_Event_Log/AllObjects.json?resultLimit=0,50 with no body
108+
*/
109+
private void allLogs() {
110+
printEventLogs(getAllEvents(false));
111+
}
112+
113+
114+
/**
115+
* Pages through all results from the Event_Log. This might take long time.
116+
*/
117+
private List<Log> getAllEvents(boolean allEvents) {
118+
List<Log> result=new ArrayList<>();
119+
int limit =50;
120+
int offset =0;
121+
ResultLimit resultLimit =new ResultLimit(offset,limit);
122+
boolean iterateEvents=true;
123+
124+
while(iterateEvents) {
125+
logService.setResultLimit(resultLimit);
126+
List<Log> logs = logService.getAllObjects();
127+
result.addAll(logs);
128+
if (logs.size() < resultLimit.limit) {
129+
iterateEvents = false;
130+
}
131+
offset+=limit;
132+
resultLimit=new ResultLimit(offset,limit);
133+
iterateEvents=iterateEvents&&allEvents;
134+
}
135+
return result;
136+
}
137+
138+
void print(Object object) {
139+
Gson gson = new GsonBuilder().setPrettyPrinting().create();
140+
String json;
141+
json = gson.toJson(object);
142+
System.out.println(json);
143+
}
144+
145+
void printEventLogs(List<Log> logs){
146+
for (Log event :logs) {
147+
System.out.println(String.format("%s - %s - %s",
148+
event.getEventName(),
149+
event.getEventCreateDate().getTime(),
150+
event.getUserType()));
151+
}
152+
}
153+
}
154+
```

0 commit comments

Comments
 (0)