Nodejs - 如何使用 morgan 进行调试
Nodejs - How to use morgan with debug
Morgan 将请求直接输出到控制台。
我如何将它们重定向到 npm debug 以便它遵循与正在记录的其他内容相同的格式?
我的 debug.js
配置如下所示:
import debug from 'debug';
const LOG_PREFIX = 'api';
const info = debug(`${LOG_PREFIX}:info`);
const dev = debug(`${LOG_PREFIX}:dev`);
const error = debug(`${LOG_PREFIX}:error`);
export {
info,
dev,
error,
};
我目前记录其他内容,例如:
import { info } from './debug';
info('App is up and running!');
我当前的摩根电话是:
app.use(morgan('combined'));
Morgan 接受一个可选参数,即 stream。
默认情况下它指向 process.stdout
,控制台。
由于调用 stream.write
,您可以轻松构建一个快速流,重定向到您的 debug
。
app.use(morgan('combined', { stream: { write: msg => info(msg) } }));
一般来说(但仍然是 ES6),那就是:
import debug from 'debug';
const info = debug('info');
app.use(morgan('combined', { stream: { write: msg => info(msg) } }));
ES5:
var info = require('debug')('info');
app.use(morgan('combined', { stream: { write: function(msg) { info(msg); } }}));
将 morgan ˋstreamˋ 与 ˋdebugˋ 结合使用时,最好做到:
app.use(morgan('combined', { stream: { write: msg => info(msg.trimEnd()) } }));
原因:morgan在调用stream.write(...)
函数时在msg末尾添加了一个新行。
Morgan 将请求直接输出到控制台。
我如何将它们重定向到 npm debug 以便它遵循与正在记录的其他内容相同的格式?
我的 debug.js
配置如下所示:
import debug from 'debug';
const LOG_PREFIX = 'api';
const info = debug(`${LOG_PREFIX}:info`);
const dev = debug(`${LOG_PREFIX}:dev`);
const error = debug(`${LOG_PREFIX}:error`);
export {
info,
dev,
error,
};
我目前记录其他内容,例如:
import { info } from './debug';
info('App is up and running!');
我当前的摩根电话是:
app.use(morgan('combined'));
Morgan 接受一个可选参数,即 stream。
默认情况下它指向 process.stdout
,控制台。
由于调用 stream.write
,您可以轻松构建一个快速流,重定向到您的 debug
。
app.use(morgan('combined', { stream: { write: msg => info(msg) } }));
一般来说(但仍然是 ES6),那就是:
import debug from 'debug';
const info = debug('info');
app.use(morgan('combined', { stream: { write: msg => info(msg) } }));
ES5:
var info = require('debug')('info');
app.use(morgan('combined', { stream: { write: function(msg) { info(msg); } }}));
将 morgan ˋstreamˋ 与 ˋdebugˋ 结合使用时,最好做到:
app.use(morgan('combined', { stream: { write: msg => info(msg.trimEnd()) } }));
原因:morgan在调用stream.write(...)
函数时在msg末尾添加了一个新行。