-
Notifications
You must be signed in to change notification settings - Fork 596
/
Copy pathloggingService.ts
49 lines (41 loc) · 1.33 KB
/
loggingService.ts
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
let tickEvent = "pxt.error";
const timestamp = () => {
const time = new Date();
const hours = padTime(time.getHours());
const minutes = padTime(time.getMinutes());
const seconds = padTime(time.getSeconds());
return `[${hours}:${minutes}:${seconds}]`;
};
const padTime = (time: number) => ("0" + time).slice(-2);
export const logError = (errorCode: string, message?: any, data: pxt.Map<string | number> = {}) => {
let dataObj = { ...data };
if (message) {
if (typeof message === "object") {
dataObj = { ...dataObj, ...message };
// Look for non-enumerable properties found on Error objects
["message", "stack", "name"].forEach(key => {
if (message[key]) {
dataObj[key] = message[key];
}
});
} else {
dataObj.message = message;
}
}
pxt.tickEvent(tickEvent, {
...dataObj,
errorCode,
});
pxt.error(timestamp(), errorCode, dataObj);
};
export const logInfo = (message: any) => {
pxt.log(timestamp(), message);
};
export const logDebug = (message: any, data?: any) => {
if (pxt.BrowserUtils.isLocalHost() || pxt.options.debug) {
pxt.log(timestamp(), message, data);
}
};
export const setTickEvent = (event: string) => {
tickEvent = event;
}