redesigned http logger to output json objects
This commit is contained in:
@@ -5,24 +5,24 @@ import express = require('express');
|
||||
import cors = require('cors');
|
||||
import morgan = require('morgan');
|
||||
const app = express()
|
||||
import chalk from 'chalk';
|
||||
|
||||
app.use(morgan((tokens: morgan.TokenIndexer, req: express.Request, res: express.Response) => {
|
||||
const status = Number(tokens.status(req, res));
|
||||
return JSON.stringify({
|
||||
type: 'http',
|
||||
timestamp: new Date().toISOString(),
|
||||
|
||||
// Colorize status code
|
||||
const statusColor = status >= 500 ? chalk.red
|
||||
: status >= 400 ? chalk.yellow
|
||||
: status >= 300 ? chalk.cyan
|
||||
: chalk.green;
|
||||
method: tokens.method(req, res),
|
||||
path: tokens.url(req, res),
|
||||
status: Number(tokens.status(req, res)),
|
||||
response_time_ms: Number(tokens['response-time'](req, res)),
|
||||
|
||||
return [
|
||||
chalk.gray(`[${new Date().toISOString()}]`),
|
||||
chalk.blue.bold(tokens.method(req, res)),
|
||||
tokens.url(req, res),
|
||||
statusColor(status),
|
||||
chalk.magenta(tokens['response-time'](req, res) + ' ms'),
|
||||
chalk.yellow(`- User: ${req.user?.name ? `${req.user.name} (${req.user.id})` : 'Unauthenticated'}`),
|
||||
].join(' ');
|
||||
ip: req.ip,
|
||||
user_agent: req.headers['user-agent'],
|
||||
|
||||
user: req.user
|
||||
? { id: req.user.id, name: req.user.name }
|
||||
: null,
|
||||
});
|
||||
}, {
|
||||
skip: (req: express.Request) => {
|
||||
return req.originalUrl === '/members/me';
|
||||
|
||||
Reference in New Issue
Block a user