A lightweight and developer-friendly middleware that provides structured and contextual logging for Express APIs.
npm install logs-ksA lightweight and developer-friendly middleware that provides structured and contextual logging for Express APIs.
This middleware helps developers easily trace API calls, debug issues, and monitor request/response cycles by capturing detailed logs. It supports time-based log batching and works seamlessly in both development and production environments with minimal setup.
---
Install the core logging package:
``bash`
npm install logs-ks
---
`ts
import express from 'express';
import { Logger } from 'logs-ks';
const app = express();
// Initialize logger with custom configuration
// Note: Logger.init() must be called before using the middleware
Logger.init({
writeLogTime: 5, // Write logs to file every 5 minutes
logWithEnd: true, // Default false logging response at the end of API calls
logWithSummary: true, // Default false logging api summary like > 2025-06-13 10:11:12 | GET 200 /api/v2/user 20 ms
});
`
---
`ts
import express, { Request, Response } from 'express';
import { loggerMiddleware, LoggerContext, logEmitter } from 'logs-ks';
const router = express.Router();
// request api localhost:3000/api/foo?page=1&limit=10
router.get('/api/:id', loggerMiddleware(), async (req: Request, res: Response) => {
try {
const { id } = req.params;
const data = await getController(id);
res.json(data);
} catch (error: unknown) {
res.status(500).send(error);
}
});
// Use loggerMiddleware(false) to disable logging the response payload
// Useful for lightweight GET endpoints
router.get('/api/user/all', loggerMiddleware(false), (req, res) => res.json({ code: 200, payload: '...too much data' }));
async function getController(id) {
const logger = LoggerContext.get();
logger.addService({ type: 'DB' }, { name: 'findUser', argument: { id } });
const findUser = await findUserDB({ id });
logger.endService(findUser);
return { code: 200, payload: { foo: 'bar' } };
}
// Listen for when a log file is finalized (previous file that finished writing)
// filePath refers to the last log file that was completed and closedNew log file created: ${filePath}
logEmitter.on('file-end', (filePath) => {
console.log();
// You can trigger backup, upload to S3, or notify devs here
});
export default router;
`
---
`js
{
"api": "api/foo?page=1&limit=10",
"method": "GET",
"authorization": null,
"status": 200,
"message": "OK",
"inputTimestamp": "2025-06-13T10:11:12.000Z",
"outputTimestamp": "2025-06-13T10:11:12.020Z",
"processingTime": "20 ms",
"request": {
"headers": {},
"params": { id: "foo"},
"query": { page: "1", limit: "10" },
"body": {},
"detail": {}
},
"services": [{
"id": 1,
"type": "DB",
"request": { "name": "findUser", "argument": { "id": "foo" } },
"response": { "id": "foo", "name": "foobar" },
"processingTime": "5 ms",
"startTime": "2025-06-11T10:11:12.010Z",
"endTime": "2025-06-11T10:11:12.015Z"
}],
"response": {
"code": 200,
"payload": { "foo": "bar" }
},
"summary": "2025-06-13 10:11:12 | GET 200 /api/user 20 ms"
}
type {
api: string;
method: string;
authorization: string | JwtPayload | null;
status: number;
message: string;
inputTimestamp: string;
outputTimestamp: string;
processingTime: string;
request: {
headers: Record
params: Record
query: Record
body: Record
detail: Record
};
services: Array<{
id: number;
type: 'DB' | 'API' | 'REDIS' | 'S3' | 'FILE' | 'FUNC';
request: {
name: string;
argument: Record
};
response: Record
processingTime: string;
startTime: string;
endTime: string;
}>;
response: Record
summary: string;
}
`
- โ
Plug-and-play Express middleware
- ๐ Structured logs for easy debugging
- โฑ๏ธ Time-based file writes for performance
- ๐ Contextual request/response trace logging
- ๐ป Console + file output support
---
Log files will be saved automatically in the working directory (or custom location if configured via logs-ks).
- ๐ Default path: ./logs/app-logs-${yy}-${mm}-${dd}-${hh}${mi}.jsonllogs/app-log-25-06-13-1300.jsonl`)
- (e.g.
---