Morgan(node.js):使用自定义格式时着色状态代码(如'dev')

Bin*_*inu 11 logging node.js

我正在使用morgan登录node.js.

我喜欢预定义格式模式'dev'中提供的状态代码着色,但我使用的是自定义格式.

如何在"开发"模式下获得相同的颜色?

根据摩根文档,dev格式如下:

 :method :url :status :response-time ms - :res[content-length]
Run Code Online (Sandbox Code Playgroud)

当我使用它时它没有颜色:

// does not color
app.use(morgan(':method :url :status :response-time ms - :res[content-length]')); 
Run Code Online (Sandbox Code Playgroud)

但是当我使用预定义的'dev'时它会变色!

app.use(morgan('dev'));
Run Code Online (Sandbox Code Playgroud)

mkb*_*mkb 8

@Adam Reis 指出了非常好的方向。我想出了这个解决方案:

morgan.token('splitter', (req) => {
    return "\x1b[36m--------------------------------------------\x1b[0m\n";
});
morgan.token('statusColor', (req, res, args) => {
    // get the status code if response written
    var status = (typeof res.headersSent !== 'boolean' ? Boolean(res.header) : res.headersSent)
        ? res.statusCode
        : undefined

    // get status color
    var color = status >= 500 ? 31 // red
        : status >= 400 ? 33 // yellow
            : status >= 300 ? 36 // cyan
                : status >= 200 ? 32 // green
                    : 0; // no color

    return '\x1b[' + color + 'm' + status + '\x1b[0m';
});
server.use(morgan(`:splitter\x1b[33m:method\x1b[0m \x1b[36m:url\x1b[0m :statusColor :response-time ms - length|:res[content-length]`));
Run Code Online (Sandbox Code Playgroud)

结果如下:

在此输入图像描述


Par*_*ngh 7

您可以很容易地使用粉笔JS进行着色。

import morgan from 'morgan';
import chalk from 'chalk'; // or you can use the require('chalk') syntax too

export const morganMiddleware = morgan(function (tokens, req, res) {
    return [
        '\n\n\n',
        chalk.hex('#ff4757').bold('  Morgan --> '),
        chalk.hex('#34ace0').bold(tokens.method(req, res)),
        chalk.hex('#ffb142').bold(tokens.status(req, res)),
        chalk.hex('#ff5252').bold(tokens.url(req, res)),
        chalk.hex('#2ed573').bold(tokens['response-time'](req, res) + ' ms'),
        chalk.hex('#f78fb3').bold('@ ' + tokens.date(req, res)),
        chalk.yellow(tokens['remote-addr'](req, res)),
        chalk.hex('#fffa65').bold('from ' + tokens.referrer(req, res)),
        chalk.hex('#1e90ff')(tokens['user-agent'](req, res)),
        '\n\n\n',
    ].join(' ');
});

app.use(morganMiddleware);
Run Code Online (Sandbox Code Playgroud)


小智 5

我在所有工作中使用这个 chalk morgan 配置,并将其作为中间件包含在 Express 应用程序中。

const morgan = require ('morgan');
const chalk = require ('chalk');

const  morganChalk = morgan(function (tokens, req, res) {
    return [
        chalk.green.bold(tokens.method(req, res)),
        chalk.red.bold(tokens.status(req, res)),
        chalk.white(tokens.url(req, res)),
        chalk.yellow(tokens['response-time'](req, res) + ' ms'),
    ].join(' ');
});

module.exports = {
    morganChalk
}
Run Code Online (Sandbox Code Playgroud)


小智 4

是的,默认情况下它无法对控制台的输出进行着色。

您可以参考这篇文章,它借助“chalk”模块来对控制台的输出进行着色。

或者我所做的是使用默认的“dev”配置,并为自定义令牌添加了额外的配置,这保留了默认的 dev 输出。像这样的东西:

app.use(morgan('dev'));
app.use(morgan('auth_id - :userid user_email - :email'));
Run Code Online (Sandbox Code Playgroud)

这将完成您想要做的事情,但是摩根的第二个输出将换行。