-
-
Notifications
You must be signed in to change notification settings - Fork 0
Description
π 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