Node.js-如何在调试中使用morgan [英] Nodejs - How to use morgan with debug

查看:137
本文介绍了Node.js-如何在调试中使用morgan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

摩根直接将请求输出到控制台.

Morgan outputs requests directly to the console.

如何将它们重定向到npm debug ,使其遵循与其他正在记录的东西?

How can I redirect them to npm debug so that it follows the same format as the other stuff which is being logged?

我的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,
};

我目前正在记录其他内容,例如:

And I currently log other stuff like:

import { info } from './debug';

info('App is up and running!');

我当前的摩根电话是:

app.use(morgan('combined'));

推荐答案

Morgan接受一个可选参数,该参数是

Morgan accepts an optional parameter which is the stream.

默认情况下,它指向控制台process.stdout.

By default it points to process.stdout, the console.

由于调用stream.write的作用,您可以轻松构建快速流,该流将重定向到您的debug.

Since what it does it to call stream.write, you can easily build a quick stream which redirects to your debug.

app.use(morgan('combined', { stream: { write: msg => info(msg) } }));

一般而言(但仍为 ES6 ),将是:

Generically (but still ES6), that would be:

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); } }}));

这篇关于Node.js-如何在调试中使用morgan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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