无法使记录器(winston)创建日志文件

时间:2019-04-07 06:39:55

标签: node.js fs winston

我回答了许多与温斯顿有关的答案,但是我找不到我的问题(代码)

const winston = require('winston');
const fs = require('fs');

const logDir = `./logs/`;
if (!fs.existsSync(logDir)) {
  fs.mkdirSync(logDir);
}

console.log(`logDir: ${logDir}`);

/* https://github.com/winstonjs/winston */
const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  exitOnError: false,
  transports: [
    //
    // - Write to all logs with level `info` and below to `combined.log`
    // - Write all logs error (and below) to `error.log`.
    //
    new winston.transports.File({ filename: `${logDir}/error-${process.pid}.log`,
                                  level: 'error',
                                  format: winston.format.combine(
                                    winston.format.timestamp(),
                                    winston.format.json(),
                                  )
                               }),
    new winston.transports.File({ filename: `${logDir}/combined-${process.pid}.log`,
                                  format: winston.format.combine(
                                    winston.format.timestamp(),
                                    winston.format.json(),
                                  )
                               }),

  ]
});

logger.info('info test');
logger.warn('warn test');
logger.error('error test');

const combinedLogs = logger.transports.find(transport => {
 console.log(`transport.filename: ${transport.filename}, transport.dirname: ${transport.dirname}`);
 // console.log(`transport: ${util.inspect(transport)}`);
});

process.exit(0);

当我使用nodejs(v10.15.3)运行时,输出为:

# nodejs myscript.js
logDir: ./logs/
transport.filename: error-24238.log, transport.dirname: ./logs/
transport.filename: combined-24238.log, transport.dirname: ./logs/

但是日志目录中没有任何内容:

root@someserver:/usr/local/somepath# ls -l logs/
total 0

我正在使用winston和fs:

+ winston@3.2.1
+ fs@0.0.1-security

0 个答案:

没有答案