monilog-sdk 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,55 @@
1
+ # Monilog SDK
2
+
3
+ A simple Node.js SDK to monitor backend logs, save them to a file, and send Slack notifications for 400/500 errors.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install logger_sdk
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - **Monitor Logs**: Automatically captures request details (method, URL, status code, duration, IP).
14
+ - **Log File Creation**: Appends all logs to a local file (defaults to `logs.txt`).
15
+ - **Error Filtering**: Automatically filters for 400 and 500 status codes.
16
+ - **Slack Notifications**: Sends alerts to a Slack channel via Webhook URL when errors occur.
17
+
18
+ ## Usage
19
+
20
+ ### Express Middleware
21
+
22
+ ```javascript
23
+ import express from 'express';
24
+ import { monitor } from 'logger_sdk';
25
+
26
+ const app = express();
27
+
28
+ app.use(monitor({
29
+ slackWebhookUrl: 'https://hooks.slack.com/services/YOUR/WEBHOOK/URL',
30
+ logFilePath: './my-logs.txt', // Optional: defaults to process.cwd()/logs.txt
31
+ monitorStatusCodes: [200, 201] // Optional: monitor specific status codes in addition to 4xx/5xx
32
+ }));
33
+
34
+ app.get('/', (req, res) => {
35
+ res.send('Hello World!');
36
+ });
37
+
38
+ app.listen(3000);
39
+ ```
40
+
41
+ ### Options
42
+
43
+ | Option | Type | Description |
44
+ | --- | --- | --- |
45
+ | `slackWebhookUrl` | `string` | (Optional) Your Slack incoming webhook URL. |
46
+ | `logFilePath` | `string` | (Optional) Path where logs will be saved. Defaults to `./logs.txt`. |
47
+ | `monitorStatusCodes` | `number[]` | (Optional) Specific status codes to monitor in addition to 400s and 500s. |
48
+
49
+
50
+ ## Development
51
+
52
+ 1. Clone the repository.
53
+ 2. Install dependencies: `npm install`
54
+ 3. Build the project: `npm run build`
55
+ 4. Run the test app: `cd test-app && node app.js`
@@ -0,0 +1,2 @@
1
+ export { monitor } from './monitor.js';
2
+ export { sendSlackNotification } from './slack.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { monitor } from './monitor.js';
2
+ export { sendSlackNotification } from './slack.js';
@@ -0,0 +1,7 @@
1
+ interface MonitorOptions {
2
+ slackWebhookUrl?: string;
3
+ logFilePath?: string;
4
+ monitorStatusCodes?: number[];
5
+ }
6
+ export declare function monitor(options?: MonitorOptions): (req: any, res: any, next: any) => void;
7
+ export {};
@@ -0,0 +1,42 @@
1
+ import fs from 'fs';
2
+ import path from 'path';
3
+ import { sendSlackNotification } from './slack.js';
4
+ export function monitor(options = {}) {
5
+ const logFilePath = options.logFilePath || path.join(process.cwd(), 'logs.txt');
6
+ const slackWebhookUrl = options.slackWebhookUrl;
7
+ const monitorStatusCodes = options.monitorStatusCodes || [];
8
+ return (req, res, next) => {
9
+ const start = Date.now();
10
+ // Capture the original end function
11
+ const originalEnd = res.end;
12
+ res.end = function (chunk, encoding) {
13
+ const duration = Date.now() - start;
14
+ const logEntry = {
15
+ timestamp: new Date().toISOString(),
16
+ method: req.method,
17
+ url: req.originalUrl || req.url,
18
+ statusCode: res.statusCode,
19
+ duration: `${duration}ms`,
20
+ ip: req.ip || req.connection.remoteAddress,
21
+ };
22
+ const logString = JSON.stringify(logEntry) + '\n';
23
+ // Create/Append to logs file
24
+ fs.appendFileSync(logFilePath, logString);
25
+ // Check for 400s, 500s or specific status codes
26
+ const isError = res.statusCode >= 400 && res.statusCode < 600;
27
+ const isMonitored = monitorStatusCodes.includes(res.statusCode);
28
+ if (isError || isMonitored) {
29
+ // Send notification to Slack
30
+ if (slackWebhookUrl) {
31
+ sendSlackNotification(slackWebhookUrl, {
32
+ title: `Monitor Alert: ${res.statusCode}`,
33
+ ...logEntry
34
+ });
35
+ }
36
+ }
37
+ // Call the original end function
38
+ originalEnd.apply(res, arguments);
39
+ };
40
+ next();
41
+ };
42
+ }
@@ -0,0 +1 @@
1
+ export declare function sendSlackNotification(webhookUrl: string, message: any): Promise<void>;
package/dist/slack.js ADDED
@@ -0,0 +1,11 @@
1
+ import axios from 'axios';
2
+ export async function sendSlackNotification(webhookUrl, message) {
3
+ try {
4
+ await axios.post(webhookUrl, {
5
+ text: JSON.stringify(message, null, 2),
6
+ });
7
+ }
8
+ catch (error) {
9
+ console.error('Failed to send Slack notification:', error);
10
+ }
11
+ }
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "monilog-sdk",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "description": "A middleware-based logger SDK for Node.js backend applications to monitor logs and send Slack alerts.",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "files": [
9
+ "dist",
10
+ "README.md"
11
+ ],
12
+ "scripts": {
13
+ "build": "tsc",
14
+ "test": "echo \"Error: no test specified\" && exit 1"
15
+ },
16
+ "keywords": [
17
+ "logger",
18
+ "sdk",
19
+ "monitoring",
20
+ "slack",
21
+ "express-middleware",
22
+ "error-alerting"
23
+ ],
24
+ "author": "Nikhil Dhaliya",
25
+ "license": "ISC",
26
+ "dependencies": {
27
+ "axios": "^1.8.1"
28
+ },
29
+ "devDependencies": {
30
+ "@types/node": "^22.13.9",
31
+ "ts-node": "^10.9.2",
32
+ "typescript": "^5.8.2"
33
+ }
34
+ }