1.0.3 • Published 5 months ago

logger-plugin-dev v1.0.3

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

Logger Plugin for Express.js

A self-contained, easy-to-integrate logging solution for Express.js applications using Winston. This plugin allows you to add powerful logging capabilities with minimal setup, including request logging, error logging, and more, without the need for additional dependencies.


Features

  • Self-Contained: Includes Winston as a dependency, so no extra installation is required.
  • Customizable: Supports configuration of log level, log directory, and log file names.
  • Request Logging: Automatically logs incoming HTTP requests.
  • Error Logging: Logs errors to separate log files for better traceability.
  • Console Logging: Logs information to the console in addition to files.
  • Easy Integration: Plug-and-play for any Express.js application.

Installation

Install the plugin using NPM:

npm install logger-plugin-dev

This will install the plugin and include the necessary Winston dependency.


Usage

Step 1: Integrate into Your Express App

After installing the plugin, you can easily integrate it into your Express application. Below is a sample setup:

const express = require('express');
const { createLogger, requestLoggerMiddleware } = require('logger-plugin');

const app = express();

// Create the logger
const logger = createLogger({
  logDir: 'logs',             // Directory where log files are stored
  errorLogFile: 'error.log',  // File for logging errors
  combinedLogFile: 'app.log', // File for logging all events
});

// Use request logging middleware
app.use(requestLoggerMiddleware(logger));

// Sample route
app.get('/', (req, res) => {
  res.send('Hello, world!');
  logger.info('Responded to / with "Hello, world!"');
});

// Global error handler
app.use((err, req, res, next) => {
  logger.error(`Unhandled error: ${err.message}`, { stack: err.stack });
  res.status(500).send('Something went wrong');
});

// Start the server
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
  logger.info(`Server running on port ${PORT}`);
});

Step 2: Customize Logging Options (Optional)

You can customize the logging behavior by passing options to the createLogger() function. Below is an example:

const logger = createLogger({
  level: 'debug',                // Log all levels of logs
  logDir: 'logs',                // Directory where log files are stored
  consoleLog: true,              // Enable console logging
  errorLogFile: 'errors.log',    // File for error logs
  combinedLogFile: 'combined.log', // File for general logs
});

Available Options:

OptionDefaultDescription
levelinfoMinimum log level (e.g., debug, warn, error).
logDirlogsDirectory where log files are saved.
consoleLogtrueWhether to log to the console.
errorLogFileerror.logFile name for error logs.
combinedLogFileapp.logFile name for general logs.

Log Files

The plugin automatically creates log files in the specified directory. By default, the logs are stored in the following files:

  • app.log: Logs all events.
  • error.log: Logs error messages only.

Folder Structure Example

my-project/
├── node_modules/
│   └── logger-plugin/
├── app.js           # Your Express app
├── package.json
├── logs/            # Log directory where log files will be stored
│   ├── app.log
│   └── error.log
└── README.md        # (This file)

Contribution

We welcome contributions to improve the plugin! Feel free to open issues or submit pull requests on GitHub.


License

This plugin is licensed under the MIT License.


Contact

For questions or suggestions, reach out to our team at developers@cybersolin.com.

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

6 months ago