@kairojsmodule/logging 0.0.0-stage → 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/index.d.ts +25 -0
- package/package.json +23 -4
- package/src/Logger.js +82 -0
- package/src/index.js +2 -0
- package/src/requestLogger.js +35 -0
- package/test/logging.test.js +37 -0
- package/README.md +0 -3
package/index.d.ts
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
export declare const LogLevel: {
|
|
2
|
+
readonly DEBUG: 10;
|
|
3
|
+
readonly INFO: 20;
|
|
4
|
+
readonly WARN: 30;
|
|
5
|
+
readonly ERROR: 40;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface LoggerOptions {
|
|
9
|
+
name?: string;
|
|
10
|
+
level?: string;
|
|
11
|
+
json?: boolean;
|
|
12
|
+
stream?: { write: (msg: string) => void };
|
|
13
|
+
redactPatterns?: (string | RegExp)[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export declare class Logger {
|
|
17
|
+
constructor(options?: LoggerOptions);
|
|
18
|
+
debug(message: string, meta?: Record<string, any>): void;
|
|
19
|
+
info(message: string, meta?: Record<string, any>): void;
|
|
20
|
+
warn(message: string, meta?: Record<string, any>): void;
|
|
21
|
+
error(message: string, meta?: Record<string, any>): void;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export declare const logger: Logger;
|
|
25
|
+
export declare function requestLogger(customLogger?: Logger): (ctx: any, next: () => Promise<void>) => Promise<void>;
|
package/package.json
CHANGED
|
@@ -1,6 +1,25 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kairojsmodule/logging",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Structured JSON logging with automatic secret redaction for KairoJS",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"types": "./index.d.ts",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": "./src/index.js"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"kairojs",
|
|
13
|
+
"logging",
|
|
14
|
+
"logger",
|
|
15
|
+
"structured-json",
|
|
16
|
+
"observability"
|
|
17
|
+
],
|
|
18
|
+
"publishConfig": {
|
|
19
|
+
"access": "public"
|
|
20
|
+
},
|
|
21
|
+
"dependencies": {
|
|
22
|
+
"@kairojsmodule/config": "^1.0.0"
|
|
23
|
+
},
|
|
24
|
+
"author": "Riski Ardiane <@kairojsmodule>"
|
|
25
|
+
}
|
package/src/Logger.js
ADDED
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { redactSecrets } from '@kairojsmodule/config';
|
|
2
|
+
|
|
3
|
+
export const LogLevel = {
|
|
4
|
+
DEBUG: 10,
|
|
5
|
+
INFO: 20,
|
|
6
|
+
WARN: 30,
|
|
7
|
+
ERROR: 40
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export class Logger {
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.name = options.name || 'kairo';
|
|
13
|
+
this.minLevel = this._parseLevel(options.level || process.env.LOG_LEVEL || 'info');
|
|
14
|
+
this.isJson = options.json !== undefined ? options.json : process.env.NODE_ENV === 'production';
|
|
15
|
+
this.outStream = options.stream || process.stdout;
|
|
16
|
+
this.customRedactPatterns = options.redactPatterns || [];
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
_parseLevel(levelStr) {
|
|
20
|
+
switch (String(levelStr).toLowerCase()) {
|
|
21
|
+
case 'debug':
|
|
22
|
+
return LogLevel.DEBUG;
|
|
23
|
+
case 'info':
|
|
24
|
+
return LogLevel.INFO;
|
|
25
|
+
case 'warn':
|
|
26
|
+
case 'warning':
|
|
27
|
+
return LogLevel.WARN;
|
|
28
|
+
case 'error':
|
|
29
|
+
return LogLevel.ERROR;
|
|
30
|
+
default:
|
|
31
|
+
return LogLevel.INFO;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_log(levelName, levelNum, message, meta = {}) {
|
|
36
|
+
if (levelNum < this.minLevel) return;
|
|
37
|
+
|
|
38
|
+
// Automatic secret redaction for all metadata
|
|
39
|
+
const cleanMeta = redactSecrets(meta, this.customRedactPatterns);
|
|
40
|
+
|
|
41
|
+
const record = {
|
|
42
|
+
timestamp: new Date().toISOString(),
|
|
43
|
+
level: levelName,
|
|
44
|
+
logger: this.name,
|
|
45
|
+
message,
|
|
46
|
+
...(Object.keys(cleanMeta).length > 0 ? { context: cleanMeta } : {})
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
if (this.isJson) {
|
|
50
|
+
this.outStream.write(JSON.stringify(record) + '\n');
|
|
51
|
+
} else {
|
|
52
|
+
const color =
|
|
53
|
+
levelName === 'ERROR'
|
|
54
|
+
? '\x1b[31m'
|
|
55
|
+
: levelName === 'WARN'
|
|
56
|
+
? '\x1b[33m'
|
|
57
|
+
: levelName === 'DEBUG'
|
|
58
|
+
? '\x1b[90m'
|
|
59
|
+
: '\x1b[36m';
|
|
60
|
+
const metaStr = Object.keys(cleanMeta).length > 0 ? ` ${JSON.stringify(cleanMeta)}` : '';
|
|
61
|
+
this.outStream.write(`${color}[${record.timestamp}] [${levelName}]\x1b[0m ${message}${metaStr}\n`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
debug(message, meta) {
|
|
66
|
+
this._log('DEBUG', LogLevel.DEBUG, message, meta);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
info(message, meta) {
|
|
70
|
+
this._log('INFO', LogLevel.INFO, message, meta);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
warn(message, meta) {
|
|
74
|
+
this._log('WARN', LogLevel.WARN, message, meta);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
error(message, meta) {
|
|
78
|
+
this._log('ERROR', LogLevel.ERROR, message, meta);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export const logger = new Logger();
|
package/src/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { logger } from './Logger.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Middleware that logs incoming requests and durations upon completion.
|
|
5
|
+
* @param {import('./Logger.js').Logger} [customLogger]
|
|
6
|
+
*/
|
|
7
|
+
export function requestLogger(customLogger = logger) {
|
|
8
|
+
return async (ctx, next) => {
|
|
9
|
+
const started = performance.now();
|
|
10
|
+
await next();
|
|
11
|
+
const duration = (performance.now() - started).toFixed(2);
|
|
12
|
+
|
|
13
|
+
const status = ctx.res.statusCode;
|
|
14
|
+
const method = ctx.req.method;
|
|
15
|
+
const path = ctx.req.path;
|
|
16
|
+
const requestId = ctx.req.id;
|
|
17
|
+
|
|
18
|
+
const message = `${method} ${path} ${status} - ${duration}ms`;
|
|
19
|
+
const meta = {
|
|
20
|
+
method,
|
|
21
|
+
path,
|
|
22
|
+
status,
|
|
23
|
+
duration: Number(duration),
|
|
24
|
+
...(requestId ? { requestId } : {})
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
if (status >= 500) {
|
|
28
|
+
customLogger.error(message, meta);
|
|
29
|
+
} else if (status >= 400) {
|
|
30
|
+
customLogger.warn(message, meta);
|
|
31
|
+
} else {
|
|
32
|
+
customLogger.info(message, meta);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { test, describe } from 'node:test';
|
|
2
|
+
import assert from 'node:assert/strict';
|
|
3
|
+
import { Logger } from '../src/index.js';
|
|
4
|
+
|
|
5
|
+
describe('Structured Logger', () => {
|
|
6
|
+
test('outputs structured JSON and redacts sensitive metadata', () => {
|
|
7
|
+
let output = '';
|
|
8
|
+
const mockStream = {
|
|
9
|
+
write(str) {
|
|
10
|
+
output += str;
|
|
11
|
+
}
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
const logger = new Logger({
|
|
15
|
+
name: 'test-app',
|
|
16
|
+
level: 'info',
|
|
17
|
+
json: true,
|
|
18
|
+
stream: mockStream
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
logger.info('User logged in', {
|
|
22
|
+
userId: 10,
|
|
23
|
+
username: 'johndoe',
|
|
24
|
+
password: 'plainSecretPassword',
|
|
25
|
+
token: 'jwt-token-val'
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
const parsed = JSON.parse(output.trim());
|
|
29
|
+
assert.equal(parsed.level, 'INFO');
|
|
30
|
+
assert.equal(parsed.logger, 'test-app');
|
|
31
|
+
assert.equal(parsed.message, 'User logged in');
|
|
32
|
+
assert.equal(parsed.context.userId, 10);
|
|
33
|
+
assert.equal(parsed.context.username, 'johndoe');
|
|
34
|
+
assert.equal(parsed.context.password, '[REDACTED]');
|
|
35
|
+
assert.equal(parsed.context.token, '[REDACTED]');
|
|
36
|
+
});
|
|
37
|
+
});
|
package/README.md
DELETED