7

在我的节点应用程序中,我使用“Winston”日志将错误打印在单独的文件中。

我跟着这个教程

当我尝试从其他文件访问记录器时......

我的代码:

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

fs.mkdir('./logs', function(err) {
    if (err) throw err;
});

// Define levels to be like log4j in java
var customLevels = {
  levels: {
    debug: 0,
    info: 1,
    warn: 2,
    error: 3
  },
  colors: {
    debug: 'blue',
    info: 'green',
    warn: 'yellow',
    error: 'red'
  }
};

// create the main logger
var logger = new(winston.Logger)({
    level: 'debug',
    levels: customLevels.levels,
    transports: [
        // setup console logging
        new(winston.transports.Console)({
            level: 'info', // Only write logs of info level or higher
            levels: customLevels.levels,
            colorize: true
        }),
        // setup logging to file
        new(winston.transports.File)({
            filename: './logs/project-debug.log',
            maxsize: 1024 * 1024 * 10, // 10MB
            level: 'debug',
            levels: customLevels.levels
        })
    ]
});

// create the data logger - I only log specific app output data here
var datalogger = new (winston.Logger) ({
    level: 'info',
    transports: [
        new (winston.transports.File) ({
            filename: './logs/project-data.log',
            maxsize: 1024 * 1024 * 10 // 10MB
        })
    ]
});

// make winston aware of your awesome colour choices
winston.addColors(customLevels.colors);

var Logging = function() {
    var loggers = {};

    // always return the singleton instance, if it has been initialised once already.
    if (Logging.prototype._singletonInstance) {
        return Logging.prototype._singletonInstance;
    }

    this.getLogger = function(name) {
        return loggers[name];
    }

    Logging.prototype.get = this.getLogger;

    loggers['project-debug.log'] = logger;
    loggers['project-data.log'] = datalogger;

    Logging.prototype._singletonInstance = this;
};

new Logging(); // I decided to force instantiation of the singleton logger here

logger.info('Logging set up OK!');

module.exports = Logging;

错误正在抛出:

Logging() is undefined.
4

4 回答 4

8

该教程似乎有很多错误。我打电话给它工作

var logger = logging.Logging().get('project-debug.log');

注意.log参数中的 。参数必须与loggers数组中定义的名称之一匹配。

于 2014-01-02T10:26:04.490 回答
7

我花了几个小时,这是我的解决方案:不要创建新的Logger,只需更改默认值。

创建一个模块lib/logger.js

var logger = require('winston');
logger.setLevels({debug:0,info: 1,silly:2,warn: 3,error:4,});
logger.addColors({debug: 'green',info:  'cyan',silly: 'magenta',warn:  'yellow',error: 'red'});
logger.remove(logger.transports.Console);
logger.add(logger.transports.Console, { level: 'debug', colorize:true });
module.exports = logger;

用于app.js

var logger = require('./lib/log.js');  

从所有其他模块使用:

var logger = require('winston');        
于 2014-04-27T22:52:48.493 回答
2

我创建并解释的文件:

var winston = require('winston'),
    winstonRedis = require('winston-redis').Redis;

// TR: console'da sadece hepsi tutuluyor olacak çünkü info log seviyesinden sonra diğer tüm log seviyeleri sıralanmış
// EN: all log level will be shown in Console, because 'info' is on the top of list with 0 value.
var transportConsole = new winston.transports.Console({ json: false, timestamp: true, prettyPrint:true, colorize: true, level:'info' }),

// TR: File'da sadece i ve db tutuluyor olacak çünkü i den sonra db log seviyesi sıralanmış
// EN: 'i' and 'db' log levels will be shown in File, because db is after i and for File transport level is 'i'
transportFileDebug = new winston.transports.File({ filename: __dirname + '/debug.log', json: true }),
transportFileException = new winston.transports.File({ filename: __dirname + '/exceptions.log', json: false }),

// TR: rediste sadece db tutuluyor olacak çünkü db den sonra bir log seviyesi yok
// EN: only 'db' will be stored in rediste because 'db' is the last one 
transportRedis = new (winstonRedis)({host: '127.0.0.1', port: 6379, level:'db'});


var logger = new (winston.Logger)({
    levels: {
        info: 0,
        warn: 1,
        error: 2,
        verbose: 3,
        i: 4,
        db: 5
    },
    transports: [
        transportConsole,
        transportFileDebug,
        transportRedis
    ],
    exceptionHandlers: [
        transportConsole,
        transportFileException
    ],
    exitOnError: false
});

winston.addColors({
    info: 'green',
    warn: 'cyan',
    error: 'red',
    verbose: 'blue',
    i: 'gray',
    db: 'magenta'
});

logger.i('iiiii foobar level-ed message');
logger.db('dbbbbb foobar level-ed message');
logger.info('infoo foobar level-ed message');
logger.warn('warnnnn foobar level-ed message');
logger.error('errroor foobar level-ed message');

module.exports = logger;
于 2015-02-20T07:42:21.063 回答
0

我使用了winston 调试日志,但它不是那么有效。我遇到了类似的问题,有时它也会崩溃。我最近切换到 mopublish 团队使用的新模块。它还支持通过 morgan 进行的 http 日志记录。NPM 模块 elogger - https://github.com/techunits/elogger

于 2015-01-01T07:52:52.130 回答