give-logger v1.1.1
Here is the exact README.md file content that you can copy and paste:
# Winston Logger Middleware for Express
A customizable Winston logger middleware for Express.js applications. It provides easy logging of requests, responses, and errors with customizable log levels and formats.
## Features
- Logs requests and responses in Express.
- Customizable log levels (e.g., `info`, `warn`, `error`).
- Supports both console and file-based logging.
- Supports TypeScript and JavaScript usage.
## Installation
### NPM
You can install this package from npm:
```bash
npm install give-loggerYarn
If you are using Yarn:
yarn add give-loggerUsage
Basic Setup
In your Express.js app, you can import and use the logger middleware to log incoming requests and responses.
JavaScript
const express = require('express');
const Logger = require('give-logger'); // Import the Logger
const app = express();
// Use the requestLogger middleware to log incoming requests and responses
app.use(Logger.requestLogger());
// Custom routes
app.get('/', (req, res) => {
Logger.info('This is an info log from the root route');
res.send('Hello, world!');
});
app.get('/error', (req, res) => {
Logger.error('This is an error log');
res.status(500).send('Something went wrong!');
});
// Start the server
const port = 3000;
app.listen(port, () => {
console.log(`Server running on http://localhost:${port}`);
});TypeScript
import express from 'express';
import { Logger } from 'give-logger';
const app = express();
// Use the logger middleware
app.use(Logger.requestLogger());
app.get('/', (req, res) => {
Logger.info('Handling GET /');
res.send('Hello, World!');
});
app.get('/error', (req, res) => {
Logger.error('Handling GET /error');
res.send('handling GET /error');
});
app.get('/debug', (req, res) => {
Logger.debug('Handling GET /debug');
res.send('handling GET /debug');
});
app.get('/warn', (req, res) => {
Logger.warn('Handling GET /warn');
res.send('handling GET /warn');
});
const port = 3000;
app.listen(port, () => {
Logger.info(`Server is running on port ${port}`);
});Logger Configuration
By default, the logger writes logs to the console and a file (logs/app.log). You can customize the log level and the format.
Available Log Levels
infowarnerror
To change the log level or customize other settings, you can modify the Logger constructor:
const logger = new Logger('warn'); // Sets the default log level to 'warn'Middleware for Request/Response Logging
To log requests and responses, use the requestLogger middleware. This middleware logs details of incoming HTTP requests and the corresponding responses, including the status code.
app.use(Logger.requestLogger());The logs will include details such as:
- HTTP method (e.g.,
GET,POST) - URL
- Response status code
Development
To contribute to the development of this package:
- Clone the repository.
Install dependencies:
npm installBuild the TypeScript code (if applicable):
npm run buildRun tests (if applicable):
npm testPush your changes and create a pull request.