How to log express HTTP requests using morgan with style?
Customizing node morgan logger with tokens.

Search for a command to run...
Customizing node morgan logger with tokens.

No comments yet. Be the first to comment.
The Problem That Makes Developers Pull Their Hair Out When you design infrastructure across multiple AWS regions, you expect each region to be self-contained — its own EKS clusters, databases, Lambdas, and certificates — all neatly separated by regio...
In my experience building distributed systems, I've consistently seen the need to evolve messaging infrastructure as applications mature. What starts as a simple Redis pub/sub for prototyping might need to scale to Kafka for high throughput, or switc...

Introduction: In the fast-paced world of backend development, working with PostgreSQL comes with its own set of hurdles. In this blog post, I'll share my experiences facing challenges like index and table bloating, an unusual performance bug related ...

Greetings fellow engineers, As we bid farewell to another year filled with coding, debugging, and pushing the boundaries of what's possible, it's time to reflect on the pages that have molded our minds in 2023 and prepare for the adventures that 2024...

Introduction Hey there, fellow developers! Today, I want to share with you a powerful technique that can take your NestJS applications to a whole new level. It's called dependency injection (DI), and trust me, once you get the hang of it, it'll becom...

In this cookbook, I am gonna help you setup morgan with express to log HTTP requests like this,

at the end of this post, you will be able to log the following details
npm i morgan moment-timezone
# or if you are using yarn
yarn add morgan moment-timezone
morgan logger allows you to customize with the .token() method. The .token() method takes in name of the token as the first argument, following a callback function. morgan will run the callback function each time a log occurs using the token.
morgan.token('date', (req, res, tz: string) => {
return moment()
.tz(tz)
.format('hh:mm a ddd, MMMM Do YYYY');
});
morgan.token('splitter', () => {
return '\x1b[36m--------------------------------------------\x1b[0m\n';
});
morgan.token('statusColor', (req, res) => {
const status = (typeof res.headersSent !== 'boolean'
? Boolean(res.header)
: res.headersSent)
? res.statusCode
: undefined;
const color =
status >= 500
? 31
: status >= 400
? 33
: status >= 300
? 36
: status >= 200
? 32
: 0;
return '\x1b[' + color + 'm' + status + '\x1b[0m';
});
app.use(
morgan(
`:splitter :date[Asia/Kolkata] \x1b[33m:method\x1b[0m \x1b[36m:url\x1b[0m :statusColor \x1b[35m:response-time ms\x1b[0m | \x1b[35m:remote-addr\x1b[0m | :remote-user | \x1b[30m:user-agent\x1b[0m`,
),
);
here I have used my local timezone, you can change Asia/Kolkata to your local timezone.
if you want to skip logging a specific request that's called frequently but you don't need to have the log you can use the below code and replace startsWith argument with the request URL of yours
app.use(
morgan(
`:splitter :date[Asia/Kolkata] \x1b[33m:method\x1b[0m \x1b[36m:url\x1b[0m :statusColor \x1b[35m:response-time ms\x1b[0m | \x1b[35m:remote-addr\x1b[0m | :remote-user | \x1b[30m:user-agent\x1b[0m`,
{
skip: (req, res) => {
return req.originalUrl.startsWith('/metrics');
},
},
),
);