Skip to content

Commit

Permalink
fix: only create logstream when LOG_ENABLE=true
Browse files Browse the repository at this point in the history
  • Loading branch information
BLxcwg666 committed Apr 6, 2024
1 parent 4078fd1 commit fdee70f
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;
Expand Down Expand Up @@ -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;

0 comments on commit fdee70f

Please sign in to comment.