From fdee70f4b71bbbb55743f43567a1b511bcc08ebf Mon Sep 17 00:00:00 2001 From: BLxcwg666 Date: Sat, 6 Apr 2024 12:46:09 +0800 Subject: [PATCH] fix: only create logstream when LOG_ENABLE=true --- logger.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/logger.js b/logger.js index 31562e9..ace4263 100644 --- a/logger.js +++ b/logger.js @@ -26,6 +26,13 @@ if (!fs.existsSync(logPath) && process.env.LOG_ENABLE === 'true') { const logger = { enableWrite: process.env.LOG_ENABLE === 'true', + logStream: null, + + initLogStream: function() { + if (this.enableWrite) { + this.logStream = fs.createWriteStream(path.join(logPath, `${moment().tz('Asia/Shanghai').format('YYYY-MM-DD HH-mm-ss')}.log`), { flags: 'a' }); + } + }, info: function (msg, who) { const log = `[${time()}] [${who}] [INFO] ${msg}`; @@ -65,10 +72,9 @@ const logger = { if (this.enableWrite) { this.logStream.write('\n' + log); } - }, - - logStream: fs.createWriteStream(path.join(logPath, `${moment().tz('Asia/Shanghai').format('YYYY-MM-DD HH-mm-ss')}.log`), { flags: 'a' }) - + } }; +logger.initLogStream(); + module.exports = logger; \ No newline at end of file