1.1.2 • Published 9 months ago

express-logger-helper v1.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
9 months ago

express-logger-helper

express-logger-helper is a simple and useful logging and reporting tool for Node.js and Express.js-based applications. It allows you to log user activities or system events and export these logs as text or JSON reports.

Features

  • Logging: Records log messages with timestamps.
  • Read Logs: Reads recorded logs in plain text format.
  • JSON Reporting: Exports logs as JSON format.
  • Clear Logs: Clears the log file to remove old records.
  • Customizable File and Directory: Specify the file name and directory where logs are stored.

Installation

npm install express-logger-helper

Usage

1. Import and Initialize

const Logger = require('express-logger-helper');

// Initialize the logger
const logger = new Logger({
    logDirectory: 'my-logs',
    fileName: 'app-log.txt'
});

2. Log Events

// Log a message
logger.log('User logged in successfully.');
logger.log('Payment transaction completed.');

3. Read Logs

// Read logs as plain text
console.log(logger.readLogs());

4. Get Logs as JSON

// Get logs in JSON format
console.log(logger.getLogsAsJson());

5. Clear Logs

// Clear all logs
logger.clearLogs();

Configuration Options

  • logDirectory (default: 'logs'): Directory where log files are stored.
  • fileName (default: 'log.txt'): Name of the log file.

Example

const Logger = require('express-logger-helper');
const express = require('express');

const app = express();
const logger = new Logger();

app.use((req, res, next) => {
    logger.log(`Request received: ${req.method} ${req.url}`);
    next();
});

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

app.listen(3000, () => {
    console.log('Server is running on port 3000');
});

License

MIT

1.2.0

9 months ago

1.2.1

9 months ago

1.1.2

9 months ago

1.1.1

9 months ago

1.1.0

9 months ago

1.0.0

9 months ago