log-it-pretty
is a customizable Express.js middleware that logs request details such as HTTP method, status code, response time, and URL in a pretty format with emojis and colors. Itβs designed to be simple, lightweight, and easy to integrate into any Node.js/Express.js application.
You can install log-it-pretty
via npm:
npm install log-it-pretty
To use log-it-pretty
, import the logRequestsMiddleware
and add it to your Express.js app as middleware.
import express from 'express'
import { logRequestsMiddleware } from 'log-it-pretty'
const app = express()
// Use log-it-pretty middleware to log requests
app.use(logRequestsMiddleware)
app.get('/api/v1/users', (req, res) => {
res.status(200).json({ message: 'User data' })
})
app.listen(3000, () => {
console.log('Server is running on port 3000')
})
When a request is made to your Express app, log-it-pretty
will log the request details to the console in a pretty format:
β
200 π’ GET 0.123s /api/v1/users
GET
: HTTP method.0.123s
: Request processing time./api/v1/users
: Requested URL.Hereβs an example of what the logs might look like:
β
200 π’ GET 0.150s /api/v1/products
β οΈ 404 π‘ GET 0.002s /api/v1/non-existent-route
β 500 π΄ POST 1.234s /api/v1/create-user
You can integrate log-it-pretty into your Express.js application by defining your own middleware. This allows you to have more control over how request logs are handled, including adding extra information like IP addresses, headers, or custom logic.
import type { NextFunction, Request, Response } from 'express'
import { logItPretty } from 'log-it-pretty'
// Define the custom middleware
export const logRequests = (
req: Request,
res: Response,
next: NextFunction
): void => {
const startTime = process.hrtime() // Start timing the request
res.on('finish', () => {
// Once the response is finished, log the request details
const logMsg = logItPretty({
statusCode: res.statusCode,
method: req.method,
url: req.originalUrl,
startTime // The startTime is passed to calculate the duration
})
// You can use any logger service here, or simply use console.log
console.log(logMsg) // Log the message to the console
})
// Continue to the next middleware
next()
}
Once youβve defined your custom middleware, you can use it in your Express app like this:
import express from 'express'
import { logRequests } from './middleware/logRequests' // Import the custom middleware
const app = express()
// Apply the custom logging middleware to log requests
app.use(logRequests)
// Define some routes
app.get('/api/v1/users', (req, res) => {
res.status(200).json({ message: 'User data' })
})
app.get('/api/v1/products', (req, res) => {
res.status(200).json({ message: 'Product data' })
})
// Start the Express server
app.listen(3000, () => {
console.log('Server is running on port 3000')
})
The output will be similar to:
β
200 π’ GET 0.123s /api/v1/users
Middleware function to log request details automatically in Express.js applications.
logRequestsMiddleware(req: Request, res: Response, next: NextFunction): void
A function that formats request details into a pretty log message.
logItPretty({
statusCode?: number;
method?: string;
duration?: number; // In milliseconds
startTime?: [number, number]; // process.hrtime tuple
url?: string;
}): string
We welcome contributions! Feel free to submit issues, bug fixes, or pull requests to improve this package.
Clone the repository:
git clone https://github.com/yourusername/log-it-pretty.git
Install the dependencies:
npm install
Build the project (if using TypeScript):
npm run build
Run tests:
npm test