-
Notifications
You must be signed in to change notification settings - Fork 10
/
Logsene.java
206 lines (185 loc) · 5.24 KB
/
Logsene.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package com.sematext.android;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.Iterator;
/**
* Logs messages to Logsene from Android applications.
*/
public class Logsene {
private final String TAG = getClass().getSimpleName();
private static JSONObject defaultMeta;
private final Context context;
private String versionName;
private Integer versionCode;
private String uuid;
public Logsene(Context context) {
Utils.requireNonNull(context);
this.context = context;
this.uuid = Installation.id(context);
}
/**
* Logs a simple message.
*
* @param level value of the log `level` field
* @param message message text
*/
public void log(String level, String message) {
Utils.requireNonNull(message);
JSONObject obj = new JSONObject();
try {
obj.put("level", level);
obj.put("message", message);
sendServiceIntent(obj);
} catch (JSONException e) {
// thrown when key is null in put(), so should never happen
Log.e(TAG, "Failed to construct json object", e);
}
}
/**
* Logs debug message.
* @param message the message text
*/
public void debug(String message) {
log("debug", message);
}
/**
* Logs debug message.
* @param message the message text
*/
public void info(String message) {
log("info", message);
}
/**
* Logs debug message.
* @param message the message text
*/
public void warn(String message) {
log("warn", message);
}
/**
* Logs exception with `warn` level.
* @param error the exception
*/
public void warn(Throwable error) {
log("warn", error);
}
/**
* Logs error message.
* @param message the message text
*/
public void error(String message) {
log("error", message);
}
/**
* Logs exception with `error` level.
* @param error the exception
*/
public void error(Throwable error) {
log("error", error);
}
/**
* Logs an exception.
* @param level value of log `level` field
* @param error any throwable
*/
public void log(String level, Throwable error) {
Utils.requireNonNull(error);
JSONObject obj = new JSONObject();
try {
obj.put("level", level);
obj.put("exception", error.getClass().toString());
obj.put("message", error.getMessage());
obj.put("stacktrace", Utils.getStackTrace(error));
sendServiceIntent(obj);
} catch (JSONException e) {
// thrown when key is null in put(), so should never happen
Log.e(TAG, "Failed to construct json object", e);
}
}
/**
* Send a custom event.
* @param object the event data.
*/
public void event(JSONObject object) {
Utils.requireNonNull(object);
sendServiceIntent(object);
}
/**
* Sets the default meta properties.
*
* These will be included with every request.
*
* @param metadata the default meta properties, use null to disable.
*/
public static void setDefaultMeta(JSONObject metadata) {
defaultMeta = metadata;
}
private void sendServiceIntent(JSONObject obj) {
assert obj != null;
enrich(obj);
Intent intent = new Intent(context, LogseneService.class);
intent.putExtra("obj", obj.toString());
context.startService(intent);
}
private String getVersionName() {
if (versionName == null) {
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionName = pInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage(), e);
// set to n/a so we don't try again
versionName = "n/a";
}
}
return versionName;
}
private int getVersionCode() {
if (versionCode == null) {
PackageInfo pInfo = null;
try {
pInfo = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
versionCode = pInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage(), e);
// set to n/a so we don't try again
versionCode = -1;
}
}
return versionCode;
}
private void enrich(JSONObject obj) {
assert obj != null;
try {
if (!obj.has("@timestamp")) {
obj.put("@timestamp", Utils.iso8601());
}
// if user has specified some value for this field, we don't touch it
if (!obj.has("meta")) {
JSONObject metadata = new JSONObject();
metadata.put("versionName", getVersionName());
metadata.put("versionCode", getVersionCode());
metadata.put("osRelease", Build.VERSION.RELEASE);
metadata.put("uuid", uuid);
if (defaultMeta != null) {
Iterator<String> keys = defaultMeta.keys();
while (keys.hasNext()) {
String key = keys.next();
metadata.put(key, defaultMeta.get(key));
}
}
obj.put("meta", metadata);
}
} catch (JSONException e) {
// thrown when key is null in put(), so should never happen
Log.e(TAG, "Failed to construct json object", e);
}
}
}