logwin
Version:
A simple but efficient logger
85 lines (65 loc) • 2.51 kB
Markdown
`logwin` is a lightweight Express.js middleware for logging HTTP request and response details. It captures critical information such as route, method, status code, response time, IP address, and user-agent, and highlights server-side errors for easier debugging.
Install `logwin` via npm:
```bash
npm install logwin
```
Import `logwin` in your Express.js application:
```typescript
import express from "express";
import logwin from "logwin";
const app = express();
// Apply the middleware
app.use(logwin);
// Define routes
app.get("/", (req, res) => {
res.send("Hello, World!");
});
app.listen(3000, () => {
console.log("Server running on http://localhost:3000");
});
```
- Logs the following details for every request:
- HTTP method
- Route
- Response status code
- Processing time in milliseconds
- Client IP address
- User-Agent string
- Error details for server-side errors
- Highlights errors with status codes between 500 and 511.
Below is an example log generated by `logwin`:
```json
{
"route": "/example-route",
"method": "GET",
"statusCode": 200,
"time": "50ms",
"duration": "2025-01-03T10:00:00.000Z",
"ip": "127.0.0.1",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/117.0.0.0 Safari/537.36",
"error": null
}
```
| Field | Description |
|---------------|--------------------------------------------------|
| `route` | The URL path of the incoming request. |
| `method` | The HTTP method (e.g., GET, POST, etc.). |
| `statusCode` | The HTTP status code of the response. |
| `time` | The time taken to process the request, in ms. |
| `duration` | Timestamp when the request was processed. |
| `ip` | The IP address of the client making the request. |
| `user-agent` | The User-Agent string from the request header. |
| `error` | Error message if status code is 500-511, or null.|
## Contribution
Contributions are welcome! Feel free to open issues or submit pull requests on the [GitHub repository](https://github.com/winnerezy/logwin).
## License
This package is licensed under the MIT License. See the LICENSE file for more details.
## Support
If you encounter any issues, please open an issue on the [GitHub repository](https://github.com/winnerezy/logwin).