f0ckv2/src/inc/log.mjs

16 lines
491 B
JavaScript
Raw Normal View History

2020-04-02 02:35:28 +00:00
import fs from "fs";
const logger = ["debug", "info", "error"];
const logfiles = logger.reduce((a, b) => ({...a, [b]: fs.createWriteStream(`./logs/${~~(Date.now() / 1000)}_${b}.log`)}), {});
export default new Proxy({}, {
get: (_, prop) => (msg, timestamp = new Date().toLocaleString()) =>
logger.includes(prop) &&
logfiles[prop].write(JSON.stringify({
level: prop,
message: msg,
timestamp: timestamp
}) + "\n") &&
console.log(timestamp, prop, msg)
});