This repository was archived by the owner on Apr 18, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabaseLogger.js
More file actions
182 lines (136 loc) · 5.34 KB
/
databaseLogger.js
File metadata and controls
182 lines (136 loc) · 5.34 KB
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
// DB
const tinyCache = {};
let lastUpdate = { number: null, moment: null };
const objType = require('@tinypudding/puddy-lib/get/objType');
let cacheLimit = 2000;
// Action
// Error To JSON
if (!('toJSON' in Error.prototype)) {
Object.defineProperty(Error.prototype, 'toJSON', {
value: function () {
var alt = {};
Object.getOwnPropertyNames(this).forEach(function (key) {
alt[key] = this[key];
}, this);
return alt;
},
configurable: true,
writable: true
});
}
const tinyAction = async function (where, type, args) {
// Try
try {
// Production
if (!isDebug) {
// Date Now
const now = moment();
// Update Counter
let count = tinyCache[where].count[type];
tinyCache[where].count[type]++;
// New Date
if ((lastUpdate.moment && lastUpdate.moment.date() !== now.date()) || tinyCache[where].count[type] > cacheLimit) {
await tinyCache[where].db.remove();
for (const item in tinyCache[where].count) { tinyCache[where].count[item] = 0; }
}
// Update Time
lastUpdate.moment = now;
lastUpdate.number = lastUpdate.moment.valueOf();
// Check Args
const insertArgs = [];
for (const item in args) {
// Is Error
if (args[item] instanceof Error) {
try { args[item] = JSON.parse(JSON.stringify(args[item])); } catch (err) { console.error(err); }
}
// Object Type
const type = objType(args[item]);
// Insert Args
if (type === "string" || type === "number" || type === "object" || type === "array") {
insertArgs.push(args[item]);
}
}
// Add Log
if (insertArgs.length > 0) {
await tinyCache[where].db.child(type).child(count).set({
time: lastUpdate.number,
args: insertArgs
});
}
// Nope
else { tinyCache[where].count[type]--; if (tinyCache[where].count[type] < 0) { tinyCache[where].count[type] = 0; } }
}
}
// Error
catch (err) {
console.error(`ERROR IN ${where} (${type})!`);
console.error(err);
}
// Complete
return;
};
// Is Debug
let isDebug = false;
// Modules
const moment = require('moment-timezone');
const getDBData = require('./getDBData');
const checkLastTime = function (value) {
// Update
if (!lastUpdate.number || value > lastUpdate.number) {
lastUpdate.moment = moment(value);
lastUpdate.number = lastUpdate.moment.valueOf();
}
// Complete
return;
};
// Functions Generator
const loggerGenerator = function (where) {
// Done
return {
// Log
log: async function () {
console.log.apply(console, arguments); tinyAction(where, 'log', arguments); return;
},
// Error
error: async function () {
console.error.apply(console, arguments); tinyAction(where, 'error', arguments); return;
},
// Info
info: async function () {
console.info.apply(console, arguments); tinyAction(where, 'info', arguments); return;
},
// Warning
warn: async function () {
console.warn.apply(console, arguments); tinyAction(where, 'warn', arguments); return;
}
};
};
// Module
module.exports = {
// Start
start: async function (newDB, where) {
// Prepare
tinyCache[where] = { db: newDB, count: { log: 0, error: 0, info: 0, warn: 0 } };
let loggerCache = null;
if (!isDebug) {
loggerCache = await getDBData(tinyCache[where].db);
if (loggerCache) {
try {
if (loggerCache.log) { tinyCache[where].count.log = loggerCache.log.length; for (const item in loggerCache.log) { if (typeof loggerCache.log[item].time === "number") { checkLastTime(loggerCache.log[item].time); } } }
if (loggerCache.error) { tinyCache[where].count.error = loggerCache.error.length; for (const item in loggerCache.error) { if (typeof loggerCache.error[item].time === "number") { checkLastTime(loggerCache.error[item].time); } } }
if (loggerCache.info) { tinyCache[where].count.info = loggerCache.info.length; for (const item in loggerCache.info) { if (typeof loggerCache.info[item].time === "number") { checkLastTime(loggerCache.info[item].time); } } }
if (loggerCache.warn) { tinyCache[where].count.warn = loggerCache.warn.length; for (const item in loggerCache.warn) { if (typeof loggerCache.warn[item].time === "number") { checkLastTime(loggerCache.warn[item].time); } } }
} catch (err) { console.error(err); isDebug = true; }
}
}
// Complete
delete loggerCache;
return loggerGenerator(where);
},
// Get
get: function (where) { return loggerGenerator(where); },
// Cache Limit
changeCacheLimit: function (value) { if (typeof value === "number") { cacheLimit = value; } return; },
// Set Is Debug
setDebugMode: function (value) { isDebug = value; return; }
};