-
Notifications
You must be signed in to change notification settings - Fork 2
/
logger.js
69 lines (60 loc) · 1.58 KB
/
logger.js
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
require("dotenv").config();
const db = require("./db.js");
const Log = require("./models/Log.js");
function replacer(key, value) {
const originalObject = this[key];
if(originalObject instanceof Map) {
return {
dataType: 'Map',
value: Array.from(originalObject.entries()),
};
} else {
return value;
}
}
class Logger {
static log(category, userId, payload, exercise, test) {
let log = Log({
environment: process.env.NODE_ENV,
category: category,
createdBy: userId,
payload: payload,
exercise: exercise,
test: test,
});
log.save((err) => {
if (err) {
console.log("There has been an error logging to Mongo: " + err);
}else{
if(category != "Code")
this.dbg("Saving Log - "+category+" - "+userId+": ",payload);
}
});
}
static dbg(msg, obj, fields) {
if(obj){
if(fields && Array.isArray(fields)){
var logObj = {};
for (const field in obj) {
if(fields.includes(field))
logObj[field] = obj[field];
}
console.log("DEBUG - "+msg+" <"+JSON.stringify(logObj,replacer).slice(0, -1)+",...}>");
}else{
console.log("DEBUG - "+msg+" <"+JSON.stringify(obj,replacer)+">");
}
} else{
console.log("DEBUG - "+msg);
}
}
static dbgerr(msg, err) {
if(err)
console.log("DEBUG - ERROR - "+msg+" <"+JSON.stringify(err)+">");
else
console.log("DEBUG - ERROR - "+msg);
}
static monitorLog(msg) {
this.log("Monitor", "Server", msg);
}
}
module.exports = Logger;