41 lines
824 B
JavaScript
41 lines
824 B
JavaScript
export class LogService {
|
|
constructor(limit = 400, logger = console) {
|
|
this.limit = limit;
|
|
this.logger = logger;
|
|
this.entries = [];
|
|
}
|
|
|
|
createEntry(type, message, meta = {}) {
|
|
return {
|
|
id: `${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
|
|
type,
|
|
message,
|
|
meta,
|
|
ts: new Date().toISOString()
|
|
};
|
|
}
|
|
|
|
push(type, message, meta = {}) {
|
|
const entry = this.createEntry(type, message, meta);
|
|
this.entries.push(entry);
|
|
|
|
if (this.entries.length > this.limit) {
|
|
this.entries.splice(0, this.entries.length - this.limit);
|
|
}
|
|
|
|
if (this.logger && type !== "output") {
|
|
this.logger.info(`[${entry.type}] ${entry.message}`);
|
|
}
|
|
|
|
return entry;
|
|
}
|
|
|
|
snapshot() {
|
|
return [...this.entries];
|
|
}
|
|
|
|
clear() {
|
|
this.entries = [];
|
|
}
|
|
}
|