使用节点js中的log4js将日志写入文件

时间:2014-03-14 09:44:57

标签: node.js logging log4js-node

我的代码是:

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');
logger.setLevel('DEBUG');

logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

它正在创建一个名为' cheese'但那里面没有任何东西。 如何写内容?

4 个答案:

答案 0 :(得分:12)

此解决方案可帮助您使用日志在ur文件夹日志中创建日志文件夹和三个文件error.log,debug.log和info.log。为我工作。感谢。

var log4js = require('log4js'); // include log4js

log4js.configure({ // configure to use all types in different files.
    appenders: [
        {   type: 'file',
            filename: "/logs/error.log", // specify the path where u want logs folder error.log
            category: 'error',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: "file",
            filename: "/logs/info.log", // specify the path where u want logs folder info.log
            category: 'info',
            maxLogSize: 20480,
            backups: 10
        },
        {   type: 'file',
            filename: "/logs/debug.log", // specify the path where u want logs folder debug.log
            category: 'debug',
            maxLogSize: 20480,
            backups: 10
        }
    ]
});

var loggerinfo = log4js.getLogger('info'); // initialize the var to use.
var loggererror = log4js.getLogger('error'); // initialize the var to use.
var loggerdebug = log4js.getLogger('debug'); // initialize the var to use.

loggerinfo.info('This is Information Logger');
loggererror.info('This is Error Logger');
loggerdebug.info('This is Debugger');

答案 1 :(得分:10)

在上面的代码中,您缺少记录器声明: 添加第var logger = log4js.getLogger('cheese');

整个代码块如下:

var log4js = require('log4js');

log4js.loadAppender('file');
log4js.addAppender(log4js.appenders.file('logs/cheese.log'), 'cheese');

var logger = log4js.getLogger('cheese');

logger.setLevel('DEBUG');
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

Reference

答案 2 :(得分:0)

版本 2.x。

你需要做这样的事情:

async def check_for_pending_status(self, alert_id):
    alert_comments = await get_comments(alert_id)
    for comm in alert_comments:
        if comm['status'] == COMMENT_STATUS.PENDING.value:
            return True, comm.get('email')
    return False

如果要根据日志消息中的属性动态创建文件(如上面的文件名),请查看here

答案 3 :(得分:0)

Log4js具有用于多文件配置的功能。参见here

log4js.configure({
  appenders: {
    out: { type: 'stdout' },
    afile: { type: 'multiFile', base: 'logs/', property: 'categoryName', extension: '.log' }
  },
  categories: {
    default: { appenders: ['out'], level: 'info' },
    tofile: { appenders: ['afile'], level: 'debug' }
  }
});

然后开始登录文件logs/xyz.log

const xyzlog = log4js.getLogger('tofile.xyz');
xyzlog.info('Hello!');