@akc42/server-utils 2.0.2 → 3.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 +8 -3
- package/debug.js +33 -5
- package/logger.js +16 -12
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -7,8 +7,8 @@ The packages are:-
|
|
|
7
7
|
|
|
8
8
|
`logger` provides a logging service for the app. It is controlled by three environment variables LOG_NONE prevents it from logging anything.
|
|
9
9
|
This is designed to be used during testing of the server side of the app so that nothing is logged. LOG_NO_DATE omits the date and time from
|
|
10
|
-
the logged output. This is generally used when another logger (e.g PM2 log output) is also adding date/time. Finally
|
|
11
|
-
to say
|
|
10
|
+
the logged output. This is generally used when another logger (e.g PM2 log output) is also adding date/time. Finally LOG_HIDDEN_IP is used
|
|
11
|
+
to say to try and anonomise client ip addresses (see below). `logger` is called so `logger([clientip,] level, ...messages);`.
|
|
12
12
|
|
|
13
13
|
`Responder` is a class to provide the ability to stream JSON responses to a node js http request. It is instanciated
|
|
14
14
|
with `new Responder(response);` and the resultant object has three methods;
|
|
@@ -39,8 +39,13 @@ separated list of topics to be logged), then this is output. Regardless, all
|
|
|
39
39
|
debug calls are stored in a 50 line cache, and will be output (newest first) on a call
|
|
40
40
|
to `dumpDebugCache`
|
|
41
41
|
|
|
42
|
+
Breaking change as of 3.0.0 logger is now an async function returning a promise fulfilled when (if set) a log file entry is made
|
|
42
43
|
|
|
43
|
-
|
|
44
|
+
both `Debug` and `logger` bit now support LOG_FILE environment variable. When set points to a log file rather than the
|
|
45
|
+
console. If it is not undefined these two modules will write to the log file rather than the console.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
These are installed with as many of few of the items that you want like so:-
|
|
44
49
|
```
|
|
45
50
|
import {logger,Responder,Debug} from '@akc42/server-utils';
|
|
46
51
|
```
|
package/debug.js
CHANGED
|
@@ -17,7 +17,7 @@
|
|
|
17
17
|
You should have received a copy of the GNU General Public License
|
|
18
18
|
along with Server Utils. If not, see <http://www.gnu.org/licenses/>.
|
|
19
19
|
*/
|
|
20
|
-
|
|
20
|
+
import fs from 'node:fs/promises';
|
|
21
21
|
import chalk from 'chalk';
|
|
22
22
|
let config = '';
|
|
23
23
|
let cache = [];
|
|
@@ -40,7 +40,14 @@ export function Debug (topic) {
|
|
|
40
40
|
}, '');
|
|
41
41
|
const output = `${chalk.greenBright(topic)} ${chalk.cyan(message)} ${chalk.whiteBright(`(${gap}ms)`)}`
|
|
42
42
|
if (enabled) {
|
|
43
|
-
|
|
43
|
+
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
44
|
+
//eslint-disable-next-line no-console
|
|
45
|
+
console.log(output);
|
|
46
|
+
} else {
|
|
47
|
+
fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true}, err => {
|
|
48
|
+
if (err) console.warn('Failed to write following message to log file', output);
|
|
49
|
+
});
|
|
50
|
+
}
|
|
44
51
|
}
|
|
45
52
|
cache.push(output);
|
|
46
53
|
if (cache.length > cachelimit) cache.splice(0,cache.length - cachelimit); //prevent it getting too big
|
|
@@ -50,17 +57,38 @@ export function dumpDebugCache() {
|
|
|
50
57
|
const output = chalk.white.bgBlue('Above are all the debug calls (most recent first) which lead up to, and then followed on from, the error above');
|
|
51
58
|
cache.reverse();
|
|
52
59
|
for(const line of cache) {
|
|
53
|
-
|
|
60
|
+
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
61
|
+
//eslint-disable-next-line no-console
|
|
62
|
+
console.log(line);
|
|
63
|
+
} else {
|
|
64
|
+
fs.appendFile(process.env.LOG_FILE, line + '\n',{flush: true}, err => {
|
|
65
|
+
if (err) console.warn('Failed to write following message to log file', line);
|
|
66
|
+
})
|
|
67
|
+
}
|
|
54
68
|
}
|
|
55
69
|
cache.reverse();
|
|
56
|
-
|
|
70
|
+
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
71
|
+
//eslint-disable-next-line no-console
|
|
72
|
+
console.log(output);
|
|
73
|
+
} else {
|
|
74
|
+
fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true}, err => {
|
|
75
|
+
if (err) console.warn('Failed to write following message to log file', output);
|
|
76
|
+
})
|
|
77
|
+
}
|
|
57
78
|
};
|
|
58
79
|
export function setDebugConfig(con, limit = 50) {
|
|
59
80
|
cachelimit = limit;
|
|
60
81
|
if (con !== config) {
|
|
61
82
|
config = con;
|
|
62
83
|
const output = `${chalk.greenBright('debug server config')} ${chalk.redBright(`new server config "${config}"`)}`
|
|
63
|
-
|
|
84
|
+
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
85
|
+
//eslint-disable-next-line no-console
|
|
86
|
+
console.log(output);
|
|
87
|
+
} else {
|
|
88
|
+
fs.appendFile(process.env.LOG_FILE, output + '\n',{flush: true}, err => {
|
|
89
|
+
if (err) console.warn('Failed to write following message to log file', output);
|
|
90
|
+
})
|
|
91
|
+
}
|
|
64
92
|
}
|
|
65
93
|
};
|
|
66
94
|
|
package/logger.js
CHANGED
|
@@ -19,7 +19,8 @@
|
|
|
19
19
|
*/
|
|
20
20
|
|
|
21
21
|
import chalk from 'chalk';
|
|
22
|
-
import { isIP } from 'net';
|
|
22
|
+
import { isIP } from 'node:net';
|
|
23
|
+
import fs from 'node:fs/promises'
|
|
23
24
|
|
|
24
25
|
const COLOURS = {
|
|
25
26
|
app: chalk.rgb(255, 136, 0).bold, //orange,
|
|
@@ -34,11 +35,7 @@ const COLOURS = {
|
|
|
34
35
|
error: chalk.white.bgRed
|
|
35
36
|
|
|
36
37
|
};
|
|
37
|
-
|
|
38
|
-
if (process.env.LOG_NO_ENCODE) {
|
|
39
|
-
cyrb53 = (str, seed = 0) => {return str;};
|
|
40
|
-
} else {
|
|
41
|
-
cyrb53 = (str, seed = 0) => {
|
|
38
|
+
function cyrb53 (str, seed = 0) {
|
|
42
39
|
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
43
40
|
for (let i = 0, ch; i < str.length; i++) {
|
|
44
41
|
ch = str.charCodeAt(i);
|
|
@@ -48,27 +45,34 @@ if (process.env.LOG_NO_ENCODE) {
|
|
|
48
45
|
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
|
|
49
46
|
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
50
47
|
return 4294967296 * (2097151 & h2) + (h1 >>> 0);
|
|
51
|
-
};
|
|
52
48
|
}
|
|
53
49
|
|
|
54
|
-
export default function logger(ip,level, ...messages) {
|
|
50
|
+
export default async function logger(ip,level, ...messages) {
|
|
55
51
|
if (process.env.LOG_NONE === undefined) {
|
|
56
52
|
let logLine = '';
|
|
57
|
-
if (process.env.LOG_NO_DATE === undefined) logLine += new Date().toISOString() + ': ';
|
|
53
|
+
if (typeof process.env.LOG_NO_DATE === 'undefined') logLine += new Date().toISOString() + ': ';
|
|
58
54
|
let message;
|
|
59
55
|
let logcolor;
|
|
60
56
|
if (isIP(ip) === 0 ) {
|
|
61
57
|
logcolor = ip;
|
|
62
58
|
message = level + messages.join(' ');
|
|
63
59
|
} else {
|
|
64
|
-
const client = process.env.LOG_IP_HIDDEN !== undefined ? cyrb53(ip): ip;
|
|
60
|
+
const client = typeof process.env.LOG_IP_HIDDEN !== 'undefined' ? cyrb53(ip): ip;
|
|
65
61
|
logLine += COLOURS.client(client + ': ');
|
|
66
62
|
logcolor = level
|
|
67
63
|
message = messages.join(' ');
|
|
68
64
|
}
|
|
69
65
|
logLine += COLOURS[logcolor](message);
|
|
70
|
-
|
|
71
|
-
|
|
66
|
+
if (typeof process.env.LOG_FILE === 'undefined') {
|
|
67
|
+
//eslint-disable-next-line no-console
|
|
68
|
+
console.log(logLine.trim());
|
|
69
|
+
} else {
|
|
70
|
+
try {
|
|
71
|
+
await fs.appendFile(process.env.LOG_FILE, logLine.trim() + '\n',{flush: true})
|
|
72
|
+
} catch(err) {
|
|
73
|
+
console.warn('Error writing log file:',err,'message being logged', logLine.trim());
|
|
74
|
+
}
|
|
75
|
+
}
|
|
72
76
|
}
|
|
73
77
|
}
|
|
74
78
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@akc42/server-utils",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "A Set of Utilities to use on the Server Side of a Project",
|
|
6
6
|
"main": "./utils.js",
|
|
@@ -23,8 +23,7 @@
|
|
|
23
23
|
"homepage": "https://github.com/akc42/server-utils#readme",
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"app-root-path": "^3.1.0",
|
|
26
|
-
"chalk": "^5.
|
|
26
|
+
"chalk": "^5.3.0",
|
|
27
27
|
"debug": "^4.3.4"
|
|
28
28
|
}
|
|
29
|
-
|
|
30
29
|
}
|