UNPKG

@build-connect/utils

Version:
35 lines (25 loc) 826 B
const fs = require('fs'); const path = require('path'); function getCallerInfo() { const originalFunc = Error.prepareStackTrace; Error.prepareStackTrace = (_, stack) => stack; const err = new Error(); const { stack } = err; Error.prepareStackTrace = originalFunc; const caller = stack[2]; // 0 = getCallerInfo, 1 = log, 2 = caller of log() const fileName = path.basename(caller.getFileName()); const lineNumber = caller.getLineNumber(); return `${fileName}:${lineNumber}`; } function log(level, message, meta = {}) { const location = getCallerInfo(); const logLine = `${JSON.stringify({ level, time: new Date().toISOString(), location, message, ...meta, })}\n`; fs.appendFileSync('app.log', logLine); } module.exports = log;