The communications platform that puts data protection first.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
Rocket.Chat/server/lib/logger/logQueue.ts

61 lines
1.2 KiB

import EventEmitter from 'events';
type LogQueue = {
id: string;
data: string;
ts: Date;
};
const queue: LogQueue[] = [];
const maxInt = 2147483647;
let queueLimit = 1000;
let queueSize = 0;
export function setQueueLimit(limit: number): void {
queueLimit = limit;
if (queueSize > queueLimit) {
queue.splice(0, queueSize - queueLimit);
}
}
export function getQueuedLogs(): LogQueue[] {
return queue;
}
export const logEntries = new EventEmitter();
const { write } = process.stdout;
function queueWrite(buffer: Uint8Array | string, cb?: (err?: Error) => void): boolean;
function queueWrite(str: Uint8Array | string, encoding?: string, cb?: (err?: Error) => void): boolean;
function queueWrite(...args: any): boolean {
write.apply(process.stdout, args);
const [str] = args;
if (typeof str !== 'string') {
return true;
}
const date = new Date();
const item = {
id: `logid-${queueSize}`,
data: str,
ts: date,
};
queue.push(item);
queueSize = (queueSize + 1) & maxInt;
if (queueSize > queueLimit) {
queue.shift();
}
logEntries.emit('log', item);
return true;
}
if (String(process.env.MOLECULER_LOG_LEVEL).toLowerCase() !== 'debug') {
process.stdout.write = queueWrite;
}