温斯顿:尝试写没有传输的日志 [英] Winston: Attempt to write logs with no transports

查看:115
本文介绍了温斯顿:尝试写没有传输的日志的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Winston为我的快速服务器设置访问日志和错误日志,但是我似乎做错了事.

I'm trying to set up an access log and an error log for my express server using Winston, but I seem to be doing something wrong.

这是我尝试使用的配置文件:

Here is my attempt at a config file:

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

const tsFormat = () => (new Date()).toLocaleTimeString();
winston.loggers.add('errorLog', {
        file: {
                filename: '<path>/errors.log', //<path> is replaced by the 
                timestamp: tsFormat,           //absolute path of the log
                level: 'info'
        }

});
winston.loggers.add('accessLog', {
        file: {
                filename: '<path>/access.log', //same as before
                timestamp: tsFormat,
                level: 'info'
        }
});

这就是我将其包含在其他文件中的方式:

And this is how I'm including it in my other files:

var winston = require('winston'),
    accessLog = winston.loggers.get('accessLog'),
    errorLog = winston.loggers.get('errorLog');

在我看来,它似乎遵循文档( https://github.com/winstonjs/winston/tree/2.4.0#working-with-multiple-loggers-in-winston ) 但是尝试登录时出现此错误:

This seems to me like it follows the documentation (https://github.com/winstonjs/winston/tree/2.4.0#working-with-multiple-loggers-in-winston) but I'm getting this error when I try to log to it:

[winston] Attempt to write logs with no transports {"message":"pls","level":"info"}
[winston] Attempt to write logs with no transports {"message":"Bad request: undefined","level":"warn"}

任何帮助将不胜感激,我已经困扰了几天.

Any help would be greatly appreciated, I've been pretty stumped for a couple days now.

推荐答案

我会尝试类似的方法,将所有与记录器相关的内容放入logger.js模块中:

I'd try something like this, put all the logger related stuff into a module logger.js:

logger.js

var winston = require('winston');
var path = require('path');
    
// Set this to whatever, by default the path of the script.
var logPath = __dirname;
const tsFormat = () => (new Date().toISOString());
    
const errorLog = winston.createLogger({
    transports: [
        new winston.transports.File({
            filename: path.join(logPath, 'errors.log'),
            timestamp: tsFormat,
            level: 'info'
        })
    ]
});
    
const accessLog = winston.createLogger({
    transports: [
        new winston.transports.File({
            filename: path.join(logPath, 'access.log'),
            timestamp: tsFormat,
            level: 'info'
        })
    ]
});
      
    
module.exports = {
    errorLog: errorLog,
    accessLog: accessLog
};

然后在index.js中进行测试:

and then test in index.js:

index.js

var logger = require('./logger');

logger.errorLog.info('Test error log');
logger.accessLog.info('Test access log');

您应该看到如下日志行:

You should see log lines like:

errors.log:

errors.log:

{"level":"info","message":"Test access log","timestamp":"2018-03-14T07:51:11.185Z"}

access.log:

access.log:

{"level":"info","message":"Test error log","timestamp":"2018-03-14T07:51:11.182Z"}

编辑

在Winston的最新版本上,new (winston.Logger)已由winston.createLogger代替( https ://github.com/bithavoc/express-winston/issues/175 )

这篇关于温斯顿:尝试写没有传输的日志的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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