@craftycodesmith/plug-and-play-logger 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 +0 -0
- package/dist/index.js +27 -0
- package/dist/interceptor.js +20 -0
- package/dist/writer.js +62 -0
- package/package.json +18 -0
package/README.md
ADDED
|
Binary file
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PlugPlayLogger = void 0;
|
|
7
|
+
const path_1 = __importDefault(require("path"));
|
|
8
|
+
const interceptor_js_1 = require("./interceptor.js");
|
|
9
|
+
const writer_js_1 = require("./writer.js");
|
|
10
|
+
class PlugPlayLogger {
|
|
11
|
+
static init(config = {}) {
|
|
12
|
+
const dir = config.dir || path_1.default.join(process.cwd(), 'logs');
|
|
13
|
+
const filename = config.filename || 'app.log';
|
|
14
|
+
this.writer = new writer_js_1.FileWriter(dir, filename);
|
|
15
|
+
if (config.intercept) {
|
|
16
|
+
(0, interceptor_js_1.patchStreams)(this.writer);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static log(message) {
|
|
20
|
+
if (!this.writer) {
|
|
21
|
+
throw new Error("Logger not initialized. Call PlugPlayLogger.init() first.");
|
|
22
|
+
}
|
|
23
|
+
const timestamp = new Date().toISOString();
|
|
24
|
+
this.writer.write(`[${timestamp}] [LOG] ${message}\n`);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
exports.PlugPlayLogger = PlugPlayLogger;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.patchStreams = patchStreams;
|
|
4
|
+
let cachedTimestamp = new Date().toISOString();
|
|
5
|
+
// Update timestamp every 1000ms
|
|
6
|
+
setInterval(() => {
|
|
7
|
+
cachedTimestamp = new Date().toISOString();
|
|
8
|
+
}, 1000);
|
|
9
|
+
function patchStreams(writer) {
|
|
10
|
+
const originalStdout = process.stdout.write.bind(process.stdout);
|
|
11
|
+
const originalStderr = process.stderr.write.bind(process.stderr);
|
|
12
|
+
process.stdout.write = (chunk, encoding, callback) => {
|
|
13
|
+
writer.write(`[${cachedTimestamp}] [STDOUT] ${chunk.toString()}`);
|
|
14
|
+
return originalStdout(chunk, encoding, callback);
|
|
15
|
+
};
|
|
16
|
+
process.stderr.write = (chunk, encoding, callback) => {
|
|
17
|
+
writer.write(`[${cachedTimestamp}] [STDERR] ${chunk.toString()}`);
|
|
18
|
+
return originalStderr(chunk, encoding, callback);
|
|
19
|
+
};
|
|
20
|
+
}
|
package/dist/writer.js
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.FileWriter = void 0;
|
|
27
|
+
// src/writer.ts
|
|
28
|
+
const fs = __importStar(require("fs"));
|
|
29
|
+
const path = __importStar(require("path"));
|
|
30
|
+
class FileWriter {
|
|
31
|
+
constructor(logDir, filename) {
|
|
32
|
+
this.logDir = logDir;
|
|
33
|
+
this.filename = filename;
|
|
34
|
+
this.buffer = [];
|
|
35
|
+
this.bufferLimit = 50; // Max logs before flush
|
|
36
|
+
if (!fs.existsSync(logDir))
|
|
37
|
+
fs.mkdirSync(logDir, { recursive: true });
|
|
38
|
+
this.stream = fs.createWriteStream(path.join(this.logDir, this.filename), { flags: 'a' });
|
|
39
|
+
// Auto-flush every 2 seconds if buffer isn't full
|
|
40
|
+
this.flushInterval = setInterval(() => this.flush(), 2000);
|
|
41
|
+
}
|
|
42
|
+
write(data) {
|
|
43
|
+
this.buffer.push(data);
|
|
44
|
+
if (this.buffer.length >= this.bufferLimit) {
|
|
45
|
+
this.flush();
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
flush() {
|
|
49
|
+
if (this.buffer.length === 0)
|
|
50
|
+
return;
|
|
51
|
+
const chunk = this.buffer.join('');
|
|
52
|
+
this.buffer = []; // Clear buffer
|
|
53
|
+
this.stream.write(chunk);
|
|
54
|
+
}
|
|
55
|
+
// Ensure logs are written before app crashes/exits
|
|
56
|
+
forceShutdown() {
|
|
57
|
+
clearInterval(this.flushInterval);
|
|
58
|
+
this.flush();
|
|
59
|
+
this.stream.end();
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.FileWriter = FileWriter;
|
package/package.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@craftycodesmith/plug-and-play-logger",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "A zero-config stream-intercepting logger for Node.js",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist",
|
|
9
|
+
"README.md"
|
|
10
|
+
],
|
|
11
|
+
"scripts": {
|
|
12
|
+
"build": "tsc",
|
|
13
|
+
"prepublishOnly": "npm run build"
|
|
14
|
+
},
|
|
15
|
+
"devDependencies": {
|
|
16
|
+
"@types/node": "^25.0.8"
|
|
17
|
+
}
|
|
18
|
+
}
|