node-logy 0.1.3 → 0.1.4
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/dist/index.d.ts +18 -35
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +35 -79
- package/dist/index.js.map +1 -1
- package/dist/protocol.d.ts +5 -186
- package/dist/protocol.d.ts.map +1 -1
- package/dist/protocol.js +1 -357
- package/dist/protocol.js.map +1 -1
- package/dist/server.js +30 -12
- package/dist/server.js.map +1 -1
- package/dist/worker.d.ts +5 -0
- package/dist/worker.d.ts.map +1 -0
- package/dist/worker.js +199 -0
- package/dist/worker.js.map +1 -0
- package/package.json +2 -1
package/dist/worker.js
ADDED
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* This is a worker file which will be spawned
|
|
4
|
+
*/
|
|
5
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
6
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
|
+
};
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
const node_path_1 = __importDefault(require("node:path"));
|
|
10
|
+
const protocol_1 = require("./protocol");
|
|
11
|
+
const node_fs_1 = __importDefault(require("node:fs"));
|
|
12
|
+
const worker_threads_1 = require("worker_threads");
|
|
13
|
+
/**
|
|
14
|
+
* Used for successful exits
|
|
15
|
+
*/
|
|
16
|
+
const EXIT_SUCCESS = 0;
|
|
17
|
+
/**
|
|
18
|
+
* Holds the stream for the log file
|
|
19
|
+
*/
|
|
20
|
+
let fileStream = null;
|
|
21
|
+
/**
|
|
22
|
+
* Holds the base path of where to save the log files
|
|
23
|
+
*/
|
|
24
|
+
let basePath = "./logs";
|
|
25
|
+
/**
|
|
26
|
+
* Holds the buffer of log entries waiting to be written
|
|
27
|
+
*/
|
|
28
|
+
let logBuffer = [];
|
|
29
|
+
/**
|
|
30
|
+
* How long we wait until we flush unless the buffer gets full
|
|
31
|
+
*/
|
|
32
|
+
const FLUSH_MS = 130;
|
|
33
|
+
/**
|
|
34
|
+
* How many entries can accumulate before we have to flush
|
|
35
|
+
*/
|
|
36
|
+
const BUFFER_FLUSH_COUNT = 100;
|
|
37
|
+
/**
|
|
38
|
+
* Holds the timeout for flush
|
|
39
|
+
*/
|
|
40
|
+
let flushTimeout = null;
|
|
41
|
+
/**
|
|
42
|
+
* Clears the pending flush timeout
|
|
43
|
+
*/
|
|
44
|
+
const clearFlushTimeout = () => {
|
|
45
|
+
if (flushTimeout) {
|
|
46
|
+
clearTimeout(flushTimeout);
|
|
47
|
+
flushTimeout = null;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
/**
|
|
51
|
+
* Flushes the buffer to the file and resets it
|
|
52
|
+
*/
|
|
53
|
+
const flush = () => {
|
|
54
|
+
if (logBuffer.length === 0 || !fileStream)
|
|
55
|
+
return;
|
|
56
|
+
const payload = logBuffer.map((x) => x.payload).join("\n") + "\n";
|
|
57
|
+
fileStream.write(payload, (err) => {
|
|
58
|
+
if (err)
|
|
59
|
+
console.error(`Write error: ${err.message}`);
|
|
60
|
+
});
|
|
61
|
+
logBuffer = [];
|
|
62
|
+
clearFlushTimeout();
|
|
63
|
+
};
|
|
64
|
+
/**
|
|
65
|
+
* Starts the delayed flush timer if not already running
|
|
66
|
+
*/
|
|
67
|
+
const startFlush = () => {
|
|
68
|
+
if (flushTimeout !== null) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
flushTimeout = setTimeout(() => {
|
|
72
|
+
flush();
|
|
73
|
+
}, FLUSH_MS);
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* Used to send response to the parent
|
|
77
|
+
* @param response The response
|
|
78
|
+
*/
|
|
79
|
+
const sendResponse = (response) => {
|
|
80
|
+
worker_threads_1.parentPort?.postMessage(response);
|
|
81
|
+
};
|
|
82
|
+
/**
|
|
83
|
+
* Handle the request decoded
|
|
84
|
+
*/
|
|
85
|
+
const requestHandler = (request) => {
|
|
86
|
+
switch (request.method) {
|
|
87
|
+
case protocol_1.METHOD.LOG: {
|
|
88
|
+
logBuffer.push(request);
|
|
89
|
+
if (logBuffer.length >= BUFFER_FLUSH_COUNT) {
|
|
90
|
+
flush();
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
startFlush();
|
|
94
|
+
}
|
|
95
|
+
sendResponse({
|
|
96
|
+
id: request.id,
|
|
97
|
+
level: request.level,
|
|
98
|
+
method: request.method,
|
|
99
|
+
success: true,
|
|
100
|
+
});
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
case protocol_1.METHOD.FLUSH:
|
|
104
|
+
flush();
|
|
105
|
+
if (fileStream?.writableNeedDrain) {
|
|
106
|
+
fileStream.once("drain", () => {
|
|
107
|
+
sendResponse({
|
|
108
|
+
id: request.id,
|
|
109
|
+
level: request.level,
|
|
110
|
+
method: request.method,
|
|
111
|
+
success: true,
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
sendResponse({
|
|
117
|
+
id: request.id,
|
|
118
|
+
level: request.level,
|
|
119
|
+
method: request.method,
|
|
120
|
+
success: true,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
break;
|
|
124
|
+
case protocol_1.METHOD.RELOAD:
|
|
125
|
+
flush();
|
|
126
|
+
fileStream?.end(() => {
|
|
127
|
+
fileStream = null;
|
|
128
|
+
createStream();
|
|
129
|
+
sendResponse({
|
|
130
|
+
id: request.id,
|
|
131
|
+
level: request.level,
|
|
132
|
+
method: request.method,
|
|
133
|
+
success: true,
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
return;
|
|
137
|
+
case protocol_1.METHOD.SHUTDOWN:
|
|
138
|
+
flush();
|
|
139
|
+
fileStream?.end(() => {
|
|
140
|
+
sendResponse({
|
|
141
|
+
id: request.id,
|
|
142
|
+
level: request.level,
|
|
143
|
+
method: request.method,
|
|
144
|
+
success: true,
|
|
145
|
+
});
|
|
146
|
+
setImmediate(() => {
|
|
147
|
+
process.exit(EXIT_SUCCESS);
|
|
148
|
+
});
|
|
149
|
+
});
|
|
150
|
+
return;
|
|
151
|
+
default:
|
|
152
|
+
process.stderr.write(`Unhandled request method: ${request.method}\n`);
|
|
153
|
+
sendResponse({
|
|
154
|
+
id: request.id,
|
|
155
|
+
level: request.level,
|
|
156
|
+
method: request.method,
|
|
157
|
+
success: false,
|
|
158
|
+
});
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
/**
|
|
163
|
+
* Generates a log filename based on current date
|
|
164
|
+
* @returns Filename in format YYYY-MM-DD.log
|
|
165
|
+
*/
|
|
166
|
+
const getLogFileName = () => {
|
|
167
|
+
const date = new Date();
|
|
168
|
+
const year = date.getFullYear();
|
|
169
|
+
const month = String(date.getMonth() + 1).padStart(2, "0");
|
|
170
|
+
const day = String(date.getDate()).padStart(2, "0");
|
|
171
|
+
return `${year}-${month}-${day}.log`;
|
|
172
|
+
};
|
|
173
|
+
/**
|
|
174
|
+
* Creates a stream to the file in append mode for today's log file.
|
|
175
|
+
* Closes existing stream if one is already open.
|
|
176
|
+
*/
|
|
177
|
+
const createStream = () => {
|
|
178
|
+
fileStream = null;
|
|
179
|
+
const fileName = getLogFileName();
|
|
180
|
+
const filePath = node_path_1.default.join(basePath, fileName);
|
|
181
|
+
fileStream = node_fs_1.default.createWriteStream(filePath, { flags: "a" });
|
|
182
|
+
fileStream.on("error", (err) => {
|
|
183
|
+
console.error(`Stream error: ${err.message}`);
|
|
184
|
+
});
|
|
185
|
+
};
|
|
186
|
+
/**
|
|
187
|
+
* Main entry point
|
|
188
|
+
*/
|
|
189
|
+
async function main() {
|
|
190
|
+
basePath = process.env["BASE_PATH"] ? process.env["BASE_PATH"] : "./logs";
|
|
191
|
+
basePath = node_path_1.default.normalize(node_path_1.default.resolve(basePath));
|
|
192
|
+
await node_fs_1.default.promises.mkdir(basePath, { recursive: true });
|
|
193
|
+
createStream();
|
|
194
|
+
worker_threads_1.parentPort?.on("message", (request) => {
|
|
195
|
+
requestHandler(request);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
main();
|
|
199
|
+
//# sourceMappingURL=worker.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"worker.js","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":";AAAA;;GAEG;;;;;AAEH,0DAA6B;AAC7B,yCAA6D;AAC7D,sDAAyB;AACzB,mDAA4C;AAE5C;;GAEG;AACH,MAAM,YAAY,GAAG,CAAC,CAAC;AAEvB;;GAEG;AACH,IAAI,UAAU,GAA0B,IAAI,CAAC;AAE7C;;GAEG;AACH,IAAI,QAAQ,GAAG,QAAQ,CAAC;AAExB;;GAEG;AACH,IAAI,SAAS,GAAiB,EAAE,CAAC;AAEjC;;GAEG;AACH,MAAM,QAAQ,GAAG,GAAG,CAAC;AAErB;;GAEG;AACH,MAAM,kBAAkB,GAAG,GAAG,CAAC;AAE/B;;GAEG;AACH,IAAI,YAAY,GAA0B,IAAI,CAAC;AAE/C;;GAEG;AACH,MAAM,iBAAiB,GAAG,GAAG,EAAE;IAC7B,IAAI,YAAY,EAAE,CAAC;QACjB,YAAY,CAAC,YAAY,CAAC,CAAC;QAC3B,YAAY,GAAG,IAAI,CAAC;IACtB,CAAC;AACH,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,KAAK,GAAG,GAAG,EAAE;IACjB,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,UAAU;QAAE,OAAO;IAElD,MAAM,OAAO,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,IAAI,CAAC;IAElE,UAAU,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,GAAG,EAAE,EAAE;QAChC,IAAI,GAAG;YAAE,OAAO,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IACjB,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,GAAG,CAAC,WAAW,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC;IAC1E,QAAQ,GAAG,mBAAI,CAAC,SAAS,CAAC,mBAAI,CAAC,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC;IAElD,MAAM,iBAAE,CAAC,QAAQ,CAAC,KAAK,CAAC,QAAQ,EAAE,EAAE,SAAS,EAAE,IAAI,EAAE,CAAC,CAAC;IAEvD,YAAY,EAAE,CAAC;IAEf,2BAAU,EAAE,EAAE,CAAC,SAAS,EAAE,CAAC,OAAmB,EAAE,EAAE;QAChD,cAAc,CAAC,OAAO,CAAC,CAAC;IAC1B,CAAC,CAAC,CAAC;AACL,CAAC;AAED,IAAI,EAAE,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-logy",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.4",
|
|
4
4
|
"description": "A lightweight Node.js logging utility that outputs logs to the console and writes them to rotating log files. Supports different log levels, timestamps, and customizable formatting for production-ready applications.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
},
|
|
36
36
|
"devDependencies": {
|
|
37
37
|
"@types/node": "^25.0.10",
|
|
38
|
+
"pino": "^10.3.0",
|
|
38
39
|
"ts-node": "^10.9.2",
|
|
39
40
|
"typescript": "^5.9.3"
|
|
40
41
|
}
|