使用Winston Node js库未创建任何日志文件 [英] No log files are being created using winston node js library

查看:201
本文介绍了使用Winston Node js库未创建任何日志文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Node js,并使用Winston库进行日志记录.以下代码不会创建日志文件.

I am working in node js and using Winston library for logging. The following code does not create a log file.

var winston = require('winston');


var logger = winston.createLogger({
    transports: [
        new winston.transports.File({
            level: 'info',
            filename: './logs/all-logs.log',
            handleExceptions: true,
            json: true,
            maxsize: 5242880, //5MB
            maxFiles: 5,
            colorize: false
        }),
        new winston.transports.Console({
            level: 'debug',
            handleExceptions: true,
            json: false,
            colorize: true
        })
    ],
    exitOnError: false
});

module.exports = logger;
module.exports.stream = {
    write: function(message, encoding){
        logger.info(message);
    }
};

logger.info("hello world");

logger.info("hello world");

它可以很好地登录到终端

It logs to the terminal fine:

 {"message":"hello world","level":"info"}

目录结构是这样的

-test.js
-winston.js
-log

推荐答案

在这里,您应该使用Winston:

Here's you should use Winston:

下面的代码在/log/目录中创建日志文件.

Below code creates log files in /log/ directory.

npm i winston-daily-rotate-file fs winston

创建名称为 winston.js

const fs = require("fs");
const winston = require("winston");
const logDir = "log";

if (!fs.existsSync(logDir)) {
    fs.mkdirSync(logDir);
}

const tsFormat = () => (new Date()).toLocaleTimeString();
module.exports = logger = winston.createLogger({
    transports: [
        new (winston.transports.Console)({
            format: winston.format.combine(
                winston.format.colorize(),
                winston.format.timestamp(),
                winston.format.align(),
                winston.format.simple(),
            ),
            level: 'info'
        }),
        new (require("winston-daily-rotate-file"))({
            filename: `${logDir}/-results.log`,
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.json(),
            )
        }),
        new winston.transports.File({ 
            filename: 'log/error.log', 
            level: 'error',
            format: winston.format.combine(
                winston.format.timestamp(),
                winston.format.simple(),
            )
        }),
    ]
});


现在您要做的就是导入记录器并使用它.下面是示例.

现在在同一目录中,创建一个新文件 test.js 并添加以下代码:


Now all you have to do is, import the logger and use it. Below is the example.

Now in the same directory, create a new file test.js and add following code:

const logger = require("./winston.js");

logger.info(`Test info Log!`);
logger.error(`Test error Log!`);


现在使用运行


Now run test.js using

node test.js

希望这就是您想要的.

这篇关于使用Winston Node js库未创建任何日志文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆