expressjs日志记录的最佳实践是什么? [英] What's the best practice for expressjs logging?

查看:95
本文介绍了expressjs日志记录的最佳实践是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基于expressjs的应用程序,我想在其中记录所有事件.我可以找到温斯顿,这似乎很酷.无论如何,我正在寻找一种方法将其连接到我的expressjs应用程序.

I am building an application based on expressjs and I'd like to log all events in it. I could find winston, which seems to be cool. Anyway, I am looking for a way how to connect it to my expressjs app.

我还想要在应用程序内部登录.我的要求不是那么简单,所以我想将所有内容记录在我的应用程序中(不仅是请求).

What I also want is logging inside the application. My reqeusts are not so simple, so I'd like to log everything inside my app (not only requests).

我目前的情况:

server.js (我想在此级别记录http请求)

server.js (I'd like to log http requests on this level)

var express = require('express');
var app = express();
var fs = require('fs');

// Post parser
app.configure(function(){
    app.use(express.bodyParser());
});

// Load routes
require('fs').readdirSync(__dirname + '/routes').forEach(function(file) {
    require(__dirname + '/routes/' + file)(app);
});

// 404: Not found
app.use(function(req, res, next){
    res.json(404, {ERROR: 'Page not found.'});
});

// 500: Error reporing
app.use(function(err, req, res, next){
    console.error(err.stack);
    res.json(500, {ERROR: 'Internal server error.'} );
});

// Startup new server
app.listen(900);

routes/something.js

var something = require(__dirname + '/../controller/something.js');

module.exports = function(app) {
    app.get('/v1/something', function(req, res, next) { new something().getAll(req, res, next); });
};

controller/something.js (我想使用相同的记录器进行调试记录)

controller/something.js (I'd like to use the same logger for debug logging)

/**
 * Constructor
 *
 */
function Something() {
};

/**
 * Get all the data
 *
 */
Something.prototype.getAll = function(req, res, next) {
    // I want to log some very important information here
    res.json({result: true, data: ['hello', 'hi', 'ciao', 'buf']});
}

module.exports = Something;

我正在考虑的另一件事是将所有事件记录在从控制器(例如模型或其他库)调用的函数中.

The other thing I am thinking about is logging all the events in functions that are called from controllers (e.g. models or other libraries).

所以我认为,创建一些记录器库的好方法可能是使用:

So I think, the good way might to create some logger library, that will be called using:

var logger = require(__dirname + '/../libraries/logger.js');

包含记录器定义.我不知道如何解决的另一个问题是如何为数据添加前缀.您知道,我有很多并发请求,我想看看每个请求调用了哪个调试消息.

containing logger definition. The other issue I don't know how to solve is how to prefix data. You know, I have a lot of concurrent requests and I'd like to see which debug message was called by each request.

推荐答案

我们使用 winston ,它可能是目前最强大的日志记录程序包.

We use winston, it's probably the most robust logging package out there.

我们最终完全按照您的建议进行了设置.创建一个用于将记录器对象包装在我们的定义和传输周围的通用库,然后处理我们希望以不同方式处理的任何其他类型的对象.

We ended up setting it up exactly like you suggested. Creating a common library used for wrapping the logger object around our definitions and transports, and then handling any other type of objects we want to be handled differently.

https://gist.github.com/rtgibbons/7354879

这篇关于expressjs日志记录的最佳实践是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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