1.0.3 • Published 6 months ago

log-it-express v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
6 months ago

Log-It-Express

log-it-express is a lightweight and flexible logging middleware for Express.js applications. It provides detailed and customizable logs for HTTP requests and responses, making it easier to monitor and debug your applications.

Features

  • Log Levels: Supports error, warn, info, and debug levels for customizable verbosity.
  • Color-Coded Logs: Uses color coding to differentiate log levels for easier readability.
  • Timestamp Formatting: Customizable timestamp formats using dayjs.
  • IP Address Logging: Logs the IP address of incoming requests.
  • Conditional Logging: Log specific requests based on route or condition.
  • Custom Middleware for Specific Routes: Apply logging only to specific routes.
  • Structured JSON Logs: Output logs in JSON format for easy integration with logging systems.
  • Session-Based Context: Track logs for specific sessions or requests.
  • Error Handling: Automatically logs errors for failed requests.
  • Response Data Logging: Optionally log response status and data.

Installation

Install the package using npm:

npm install log-it-express

Usage

Integrate log-it-express into your Express application:

const express = require('express');
const logItExpress = require('log-it-express');

const app = express();

// Use the middleware with custom options
app.use(
    logItExpress({
        logLevel: 'debug',         // Log verbosity level
        logHeaders: true,          // Log request headers
        logBody: true,             // Log request body
        logIP: true,               // Log client IP address
        logResponseData: true,     // Log response data
        jsonLogs: true,            // Output logs as JSON
        customRouteLogger: (url) => url === '/test', // Log only for specific routes
        sessionContext: true,      // Track logs per session
        timestampFormat: 'YYYY-MM-DD HH:mm:ss', // Custom timestamp format
    })
);

// Example routes
app.get('/', (req, res) => {
    res.send('Hello, World!');
});

app.get('/test', (req, res) => {
    res.send('Testing the logger!');
});

// Start the server
app.listen(3000, () => {
    console.log('Server running on http://localhost:3000');
});

Configuration Options

OptionTypeDefaultDescription
logLevelstringinfoLog verbosity level: error, warn, info, or debug.
logHeadersbooleanfalseLog request headers.
logBodybooleanfalseLog request body.
logIPbooleanfalseLog the client’s IP address.
logResponseDatabooleanfalseLog response status and data.
jsonLogsbooleanfalseOutput logs in structured JSON format.
customRouteLoggerfunctionnullCustom logic to log specific routes.
sessionContextbooleanfalseTrack session or request-based context.
timestampFormatstringYYYY-MM-DDTHH:mm:ss.SSSZCustomize the format of timestamps using dayjs.

Examples

1. Simple Logging

app.use(logItExpress());

2. Logging with Headers and Body

app.use(
    logItExpress({
        logHeaders: true,
        logBody: true,
    })
);

3. Debug-Level Logging with Color Coding

app.use(
    logItExpress({
        logLevel: 'debug',
    })
);

4. JSON Logs with IP Address Logging

app.use(
    logItExpress({
        jsonLogs: true,
        logIP: true,
    })
);

Development

Feel free to contribute to this package by submitting issues or pull requests. Clone the repository:

git clone https://github.com/RiyazN0921/log-it-express.git

License

This package is licensed under the MIT License.

Author

Developed by Riyaz Nadaf. For any queries or feedback, feel free to contact me!

1.0.3

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago