Skip to content

πŸ” Create Universal Logging Module with Customizable Usage & Export SupportΒ #24

@abhishek-nexgen-dev

Description

@abhishek-nexgen-dev

πŸ“– Overview

Build a pluggable logging utility that records any event, API call, user action, or internal business logic β€” without enforcing any specific route or middleware. This module will allow any developer to:

Use it manually or attach to middleware

Store logs in any format they choose

Export logs to Excel, CSV, JSON

Create their own routes, their own way

You don’t need to register default routes. Developers will import and use the Logger functions where they need them.


πŸ’‘ Why It Matters

πŸ”’ Feature πŸ“˜ Benefit

Custom control Devs define their own route, filters, access
Reusable in all apps Use in Express, Fastify, NestJS, or CLI
No forced structure You just import and call logger where needed
Easy export Provide Excel, CSV, JSON exports with a single function
Use with any DB or in-memory Logs are saved using the dev’s own DB method (Mongo, Postgres, JSON file, etc.)


πŸ—οΈ Module Purpose

This is a universal utility for:

Logging custom messages

Tracking events

Recording API actions manually

Exporting logs

Supporting audits without strict rules

πŸ“Œ Think of it like console.log() β€” but made for production, pluggable, structured, and exportable.


🧱 File Structure

src/
└── utils/
└── Logger/
β”œβ”€β”€ Logger.ts # Core logging class
β”œβ”€β”€ Logger.types.ts # Type interfaces
β”œβ”€β”€ Logger.export.ts # Export functions: Excel, CSV, JSON
β”œβ”€β”€ Logger.constant.ts # Constants & log types
β”œβ”€β”€ Logger.middleware.ts # Optional express middleware
└── Logger.demo.ts # Examples for Express, CLI, FastKit


βœ… Core Features

πŸ”Ή Logger Class

Logger.log({
userId: 'abc123',
method: 'POST',
route: '/orders',
statusCode: 201,
metadata: { orderId: 'XYZ', amount: 250 },
});

πŸ”Ή Export Logs

LoggerExporter.export({
data: logArray,
format: 'csv',
fileName: 'user-activity',
});

Supported formats:

βœ… .xlsx

βœ… .csv

βœ… .json


πŸ”Ή Optional Middleware

If the dev wants to use middleware:

app.use(LoggerMiddleware.autoLog());

But it's optional. Developers can ignore this and log manually instead.


πŸ” You Define the Route

Instead of shipping a default /logs route, developers can do:

router.get('/admin/logs', async (req, res) => {
const logs = await myDb.findLogs({ userId: req.user.id });
res.json(logs);
});

Same for export:

router.get('/admin/logs/export', async (req, res) => {
const logs = await myDb.findLogs();
const buffer = await LoggerExporter.export({
data: logs,
format: 'xlsx',
fileName: 'activity',
});

res.setHeader('Content-Disposition', 'attachment; filename=activity.xlsx');
res.send(buffer);
});


πŸ”§ Tasks

[ ] Create Logger.types.ts for LogEntry, ExportOptions

[ ] Create Logger.ts with log(), info(), warn(), error() methods

[ ] Create Logger.export.ts for Excel, CSV, JSON export

[ ] Create Logger.middleware.ts (optional)

[ ] Create examples inside Logger.demo.ts

[ ] Ensure clean error handling & validation


πŸ§ͺ Sample Log Output

{
"timestamp": "2025-06-27T10:15:00Z",
"userId": "abc123",
"method": "DELETE",
"route": "/projects/45",
"statusCode": 403,
"metadata": {
"reason": "Unauthorized access",
"projectOwner": "xyz"
}
}


🎯 Expected Outcome

Works in any project

No forced routes or framework dependencies

Pure functions, no side effects

Let users wire up logs to MongoDB, MySQL, flat files, Redis, etc.

Devs write their own Express/Nest/Fastify route using this utility


🏷️ Suggested Labels

feature, utility, export, logger, customizable, framework-agnostic, typescript

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions