Skip to content

Commit

Permalink
core: optimize logger
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodrr authored and ammarahm-ed committed Nov 25, 2023
1 parent 7987ef8 commit dff1c7e
Showing 1 changed file with 23 additions and 33 deletions.
56 changes: 23 additions & 33 deletions packages/core/src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,7 @@ class DatabaseLogWriter {
*/
constructor(storage) {
this.storage = storage;
this.key = new Date().toLocaleDateString();
this.queue = {};
this.queue = new Map();
this.hasCleared = false;
setInterval(() => {
setTimeout(() => {
Expand All @@ -74,22 +73,14 @@ class DatabaseLogWriter {
}

push(message) {
this.queue[`${this.key}:${message.timestamp}`] = message;
}

async read() {
const logKeys = await this.storage.getAllKeys();
const keys = [];
for (let key of logKeys) {
if (key.startsWith(this.key)) keys.push(key);
}
return Object.values(await this.storage.readMulti(keys));
const key = new Date(message.timestamp).toLocaleDateString();
this.queue.set(`${key}:${message.timestamp}`, message);
}

async flush() {
if (Object.keys(this.queue).length === 0) return;
const queueCopy = Object.entries(this.queue);
this.queue = {};
if (this.queue.size === 0) return;
const queueCopy = Array.from(this.queue.entries());
this.queue = new Map();

await this.storage.writeMulti(queueCopy);
}
Expand All @@ -104,9 +95,7 @@ class DatabaseLogWriter {
}
}

if (keysToRemove.length) {
await this.storage.removeMulti(keysToRemove);
}
if (keysToRemove.length) await this.storage.removeMulti(keysToRemove);
}
}

Expand All @@ -121,23 +110,24 @@ class DatabaseLogManager {

async get() {
const logKeys = await this.storage.getAllKeys();
const logs = {};
const logs = await this.storage.readMulti(logKeys);
const logGroups = {};

for (const logKey of logKeys) {
const keyParts = logKey.split(":");
for (const [key, log] of logs) {
const keyParts = key.split(":");
if (keyParts.length === 1) continue;
const key = keyParts[0];

const log = await this.storage.read(logKey, true);

if (!logs[key]) logs[key] = [];
logs[key].push(log);
const groupKey = keyParts[0];
if (!logGroups[groupKey]) logGroups[groupKey] = [];
logGroups[groupKey].push(log);
}

return Object.keys(logs).map((key) => ({
key: key,
logs: logs[key]?.sort((a, b) => a.timestamp - b.timestamp)
}));
return Object.keys(logGroups)
.sort((a, b) => new Date(a).getTime() - new Date(b).getTime())
.map((key) => ({
key: new Date(key).toLocaleDateString(),
logs: logGroups[key]?.sort((a, b) => a.timestamp - b.timestamp)
}));
}

async clear() {
Expand All @@ -147,14 +137,14 @@ class DatabaseLogManager {

async delete(key) {
const logKeys = await this.storage.getAllKeys();
const keysToRemove = [];
for (const logKey of logKeys) {
const keyParts = logKey.split(":");
if (keyParts.length === 1) continue;
const currKey = keyParts[0];
if (currKey === key) {
await this.storage.remove(logKey);
}
if (currKey === key) keysToRemove.push(logKey);
}
if (keysToRemove.length) await this.storage.removeMulti(keysToRemove);
}
}

Expand Down

0 comments on commit dff1c7e

Please sign in to comment.