Skip to content

Commit

Permalink
[#70409] signal: Create directories for log files automatically
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejWas committed Dec 18, 2024
1 parent c48832d commit 6c77552
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions bin/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,15 @@ if (process.env.LOGDIR) {

const logFile = (room) => path.join(process.env.LOGDIR, room + ".log");

export const logAsync = (room, obj) =>
process.env.LOGDIR &&
new Promise((r) => r(JSON.stringify({ time: new Date().toISOString().slice(0, -2), ...obj }) + "\n")).then((data) =>
fs.appendFileSync(logFile(room), data),
);
export const logAsync = async (room, obj) => {
if (!process.env.LOGDIR) return;

return await new Promise((r) => r(JSON.stringify({ time: new Date().toISOString().slice(0, -2), ...obj }) + "\n")).then((data) => {
const logFilePath = logFile(room);
fs.mkdirSync(path.dirname(logFilePath), { recursive: true });
fs.appendFileSync(logFilePath, data);
});
};

const humanReadableDelta = (delta) => {
delta.filter((d) => typeof d.insert?.join == "function").forEach((delta) => (delta.insert = delta.insert.join("")));
Expand Down

0 comments on commit 6c77552

Please sign in to comment.