@emartech/json-logger 9.0.0 → 9.1.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/dist/logger/logger.d.ts +2 -1
- package/dist/logger/logger.js +16 -10
- package/package.json +16 -14
- package/dist/enabled/enabled.spec.d.ts +0 -1
- package/dist/enabled/enabled.spec.js +0 -29
- package/dist/formatter/json.spec.d.ts +0 -1
- package/dist/formatter/json.spec.js +0 -9
- package/dist/formatter/logentries.spec.d.ts +0 -1
- package/dist/formatter/logentries.spec.js +0 -18
- package/dist/index.spec.d.ts +0 -1
- package/dist/index.spec.js +0 -22
- package/dist/logger/logger.spec.d.ts +0 -1
- package/dist/logger/logger.spec.js +0 -322
- package/dist/setup.spec.d.ts +0 -1
- package/dist/setup.spec.js +0 -12
- package/dist/timer/timer.spec.d.ts +0 -1
- package/dist/timer/timer.spec.js +0 -83
package/dist/logger/logger.d.ts
CHANGED
|
@@ -3,7 +3,7 @@ export interface LoggerConfig {
|
|
|
3
3
|
formatter: Function;
|
|
4
4
|
output: Function;
|
|
5
5
|
transformers: Function[];
|
|
6
|
-
outputFormat:
|
|
6
|
+
outputFormat: string;
|
|
7
7
|
}
|
|
8
8
|
export declare class Logger {
|
|
9
9
|
private readonly namespace;
|
|
@@ -29,5 +29,6 @@ export declare class Logger {
|
|
|
29
29
|
private shortenData;
|
|
30
30
|
private getErrorDetails;
|
|
31
31
|
private getBaseErrorDetails;
|
|
32
|
+
private extractError;
|
|
32
33
|
private getAxiosErrorDetails;
|
|
33
34
|
}
|
package/dist/logger/logger.js
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Logger = void 0;
|
|
4
|
+
const lodash_1 = require("lodash");
|
|
4
5
|
const config_1 = require("../config");
|
|
5
|
-
const STACK_TRACE_LIMIT = 3000;
|
|
6
|
-
const DATA_LIMIT = 3000;
|
|
7
|
-
const timer_1 = require("../timer/timer");
|
|
8
6
|
const json_1 = require("../formatter/json");
|
|
9
7
|
const console_1 = require("../output/console");
|
|
10
|
-
const
|
|
8
|
+
const timer_1 = require("../timer/timer");
|
|
9
|
+
const STACK_TRACE_LIMIT = 3000;
|
|
10
|
+
const DATA_LIMIT = 3000;
|
|
11
11
|
const allowedKeys = ['output', 'formatter', 'transformers', 'outputFormat'];
|
|
12
|
+
/* eslint-enable @typescript-eslint/no-unsafe-function-type */
|
|
12
13
|
class Logger {
|
|
13
14
|
namespace;
|
|
14
15
|
enabled;
|
|
@@ -125,17 +126,22 @@ class Logger {
|
|
|
125
126
|
};
|
|
126
127
|
}
|
|
127
128
|
return {
|
|
128
|
-
error:
|
|
129
|
-
type: error.name,
|
|
130
|
-
message: error.message,
|
|
131
|
-
context: this.shortenData(error.data),
|
|
132
|
-
stack_trace: this.shortenStackTrace(error.stack || ''),
|
|
133
|
-
},
|
|
129
|
+
error: this.extractError(error),
|
|
134
130
|
event: {
|
|
135
131
|
reason: error.message,
|
|
136
132
|
},
|
|
137
133
|
};
|
|
138
134
|
}
|
|
135
|
+
extractError(error) {
|
|
136
|
+
const shortenedData = this.shortenData(error.data);
|
|
137
|
+
return {
|
|
138
|
+
type: error.name,
|
|
139
|
+
message: error.message,
|
|
140
|
+
...(shortenedData && { context: shortenedData }),
|
|
141
|
+
stack_trace: this.shortenStackTrace(error.stack || ''),
|
|
142
|
+
...(error.cause instanceof Error && { cause: this.extractError(error.cause) }),
|
|
143
|
+
};
|
|
144
|
+
}
|
|
139
145
|
getAxiosErrorDetails(error) {
|
|
140
146
|
if (!error.isAxiosError) {
|
|
141
147
|
return {};
|
package/package.json
CHANGED
|
@@ -9,8 +9,8 @@
|
|
|
9
9
|
"scripts": {
|
|
10
10
|
"test": "mocha --require ts-node/register --extension ts ./src --recursive",
|
|
11
11
|
"test:watch": "mocha --require ts-node/register --extension ts ./src --recursive --watch",
|
|
12
|
-
"lint": "eslint .",
|
|
13
|
-
"lint:fix": "eslint . --fix",
|
|
12
|
+
"lint": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint .",
|
|
13
|
+
"lint:fix": "cross-env ESLINT_USE_FLAT_CONFIG=false eslint . --fix",
|
|
14
14
|
"build": "rm -rf dist && tsc --project ./tsconfig.json",
|
|
15
15
|
"release": "CI=true semantic-release",
|
|
16
16
|
"example-js": "DEBUG=* node examples/index-js.js",
|
|
@@ -42,29 +42,31 @@
|
|
|
42
42
|
"lodash": "^4.17.21"
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
|
-
"@types/chai": "
|
|
45
|
+
"@types/chai": "5.2.2",
|
|
46
46
|
"@types/lodash": "4.17.20",
|
|
47
47
|
"@types/mocha": "10.0.10",
|
|
48
48
|
"@types/node": "20.19.22",
|
|
49
49
|
"@types/sinon": "17.0.4",
|
|
50
|
-
"@types/sinon-chai": "
|
|
51
|
-
"@typescript-eslint/eslint-plugin": "
|
|
52
|
-
"@typescript-eslint/parser": "
|
|
53
|
-
"axios": "1.
|
|
54
|
-
"chai": "
|
|
55
|
-
"
|
|
50
|
+
"@types/sinon-chai": "4.0.0",
|
|
51
|
+
"@typescript-eslint/eslint-plugin": "8.46.1",
|
|
52
|
+
"@typescript-eslint/parser": "8.46.1",
|
|
53
|
+
"axios": "1.12.2",
|
|
54
|
+
"chai": "6.2.0",
|
|
55
|
+
"cross-env": "10.1.0",
|
|
56
|
+
"eslint": "9.38.0",
|
|
56
57
|
"eslint-config-emarsys": "5.1.0",
|
|
57
58
|
"eslint-config-prettier": "10.1.8",
|
|
59
|
+
"eslint-plugin-chai-friendly": "1.1.0",
|
|
58
60
|
"eslint-plugin-no-only-tests": "3.3.0",
|
|
59
61
|
"eslint-plugin-prettier": "5.5.4",
|
|
60
|
-
"eslint-plugin-security": "
|
|
61
|
-
"mocha": "
|
|
62
|
+
"eslint-plugin-security": "3.0.1",
|
|
63
|
+
"mocha": "11.7.4",
|
|
62
64
|
"prettier": "3.6.2",
|
|
63
|
-
"semantic-release": "
|
|
65
|
+
"semantic-release": "24.2.9",
|
|
64
66
|
"sinon": "21.0.0",
|
|
65
|
-
"sinon-chai": "
|
|
67
|
+
"sinon-chai": "4.0.1",
|
|
66
68
|
"ts-node": "10.9.2",
|
|
67
69
|
"typescript": "5.9.3"
|
|
68
70
|
},
|
|
69
|
-
"version": "9.
|
|
71
|
+
"version": "9.1.0"
|
|
70
72
|
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const enabled_1 = require("./enabled");
|
|
4
|
-
const chai_1 = require("chai");
|
|
5
|
-
describe('isNamespaceAvailable', () => {
|
|
6
|
-
it('should enable when variables only contain one', () => {
|
|
7
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('mongo', 'mongo')).to.eql(true);
|
|
8
|
-
});
|
|
9
|
-
it('should disable when not in availables', () => {
|
|
10
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('mongo', 'redis')).to.eql(false);
|
|
11
|
-
});
|
|
12
|
-
it('should enable when part of available and available contains *', () => {
|
|
13
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('mongo*', 'mongolab')).to.eql(true);
|
|
14
|
-
});
|
|
15
|
-
it('should allow multiple available namespaces', () => {
|
|
16
|
-
const availableNamespaces = 'mongo,redis';
|
|
17
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)(availableNamespaces, 'mongo')).to.eql(true);
|
|
18
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)(availableNamespaces, 'redis')).to.eql(true);
|
|
19
|
-
});
|
|
20
|
-
it('should disable names starting with -', () => {
|
|
21
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('mongo*,-*lab', 'mongolab')).to.eql(false);
|
|
22
|
-
});
|
|
23
|
-
it('should not work with empty strings', () => {
|
|
24
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('', '')).to.eql(false);
|
|
25
|
-
});
|
|
26
|
-
it('should enable everything for star', () => {
|
|
27
|
-
(0, chai_1.expect)((0, enabled_1.isNamespaceEnabled)('*', 'mongo')).to.eql(true);
|
|
28
|
-
});
|
|
29
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,9 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const json_1 = require("./json");
|
|
5
|
-
describe('json formatter', () => {
|
|
6
|
-
it('should stringify object to json', () => {
|
|
7
|
-
(0, chai_1.expect)((0, json_1.jsonFormatter)({ name: 'debugger' })).to.eql('{"name":"debugger"}');
|
|
8
|
-
});
|
|
9
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const logentries_1 = require("./logentries");
|
|
5
|
-
describe('logentries formatter', () => {
|
|
6
|
-
it('should stringify single field to key value pairs', () => {
|
|
7
|
-
(0, chai_1.expect)((0, logentries_1.logentriesFormatter)({ name: 'debugger' })).to.eql('name="debugger"');
|
|
8
|
-
});
|
|
9
|
-
it('should stringify multiple fields separated', () => {
|
|
10
|
-
(0, chai_1.expect)((0, logentries_1.logentriesFormatter)({ name: 'debugger', namespace: 'it' })).to.eql('name="debugger" namespace="it"');
|
|
11
|
-
});
|
|
12
|
-
it('should not print numbers as string', () => {
|
|
13
|
-
(0, chai_1.expect)((0, logentries_1.logentriesFormatter)({ name: 'debugger', duration: 10, percent: 1.5 })).to.eql('name="debugger" duration=10 percent=1.5');
|
|
14
|
-
});
|
|
15
|
-
it('should json stringify nested objects', () => {
|
|
16
|
-
(0, chai_1.expect)((0, logentries_1.logentriesFormatter)({ name: 'debugger', nested: { inner: 10 } })).to.eql('name="debugger" nested="{"inner":10}"');
|
|
17
|
-
});
|
|
18
|
-
});
|
package/dist/index.spec.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/index.spec.js
DELETED
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const index_1 = require("./index");
|
|
5
|
-
describe('createLogger', function () {
|
|
6
|
-
beforeEach(function () {
|
|
7
|
-
process.env.DEBUG = 'mongo';
|
|
8
|
-
});
|
|
9
|
-
afterEach(function () {
|
|
10
|
-
process.env.DEBUG = '';
|
|
11
|
-
});
|
|
12
|
-
it('should return an enabled log instance when env variable is set to same', function () {
|
|
13
|
-
const logger = (0, index_1.createLogger)('mongo');
|
|
14
|
-
(0, chai_1.expect)(logger).to.be.an.instanceOf(index_1.Logger);
|
|
15
|
-
(0, chai_1.expect)(logger.isEnabled()).to.be.true;
|
|
16
|
-
});
|
|
17
|
-
it('should return a disabled log instance when different', function () {
|
|
18
|
-
const logger = (0, index_1.createLogger)('redis');
|
|
19
|
-
(0, chai_1.expect)(logger).to.be.an.instanceOf(index_1.Logger);
|
|
20
|
-
(0, chai_1.expect)(logger.isEnabled()).to.be.false;
|
|
21
|
-
});
|
|
22
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
|
@@ -1,322 +0,0 @@
|
|
|
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
|
-
const axios_1 = require("axios");
|
|
7
|
-
const chai_1 = require("chai");
|
|
8
|
-
const sinon_1 = __importDefault(require("sinon"));
|
|
9
|
-
const json_1 = require("../formatter/json");
|
|
10
|
-
const console_1 = require("../output/console");
|
|
11
|
-
const logger_1 = require("./logger");
|
|
12
|
-
describe('Logger', () => {
|
|
13
|
-
let logger;
|
|
14
|
-
let clock;
|
|
15
|
-
let outputStub;
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
logger = new logger_1.Logger('mongo', true);
|
|
18
|
-
outputStub = sinon_1.default.stub(logger_1.Logger.config, 'output');
|
|
19
|
-
clock = sinon_1.default.useFakeTimers();
|
|
20
|
-
});
|
|
21
|
-
afterEach(() => {
|
|
22
|
-
logger_1.Logger.configure({
|
|
23
|
-
formatter: json_1.jsonFormatter,
|
|
24
|
-
output: console_1.consoleOutput,
|
|
25
|
-
transformers: [],
|
|
26
|
-
outputFormat: 'ecs',
|
|
27
|
-
});
|
|
28
|
-
clock.restore();
|
|
29
|
-
});
|
|
30
|
-
it('should call log info method when enabled (legacy format)', () => {
|
|
31
|
-
logger_1.Logger.configure({ outputFormat: 'legacy' });
|
|
32
|
-
logger.info('wedidit', { details: 'forever' });
|
|
33
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
34
|
-
(0, chai_1.expect)(logArguments.name).to.eql('mongo');
|
|
35
|
-
(0, chai_1.expect)(logArguments.action).to.eql('wedidit');
|
|
36
|
-
(0, chai_1.expect)(logArguments.level).to.eql(30);
|
|
37
|
-
(0, chai_1.expect)(logArguments.details).to.eql('forever');
|
|
38
|
-
});
|
|
39
|
-
it('should call log info method when enabled (ecs format)', () => {
|
|
40
|
-
logger_1.Logger.configure({ outputFormat: 'ecs' });
|
|
41
|
-
logger.info('wedidit', { details: 'forever' });
|
|
42
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
43
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('wedidit');
|
|
44
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
45
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(30);
|
|
46
|
-
(0, chai_1.expect)(logArguments.details).to.eql('forever');
|
|
47
|
-
});
|
|
48
|
-
it('should not overwrite fields in ecs base log', () => {
|
|
49
|
-
logger.info('wedidit', { event: { duration: 245 }, log: { ter: 'abc' } });
|
|
50
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
51
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('wedidit');
|
|
52
|
-
(0, chai_1.expect)(logArguments.event.duration).to.eql(245);
|
|
53
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
54
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(30);
|
|
55
|
-
(0, chai_1.expect)(logArguments.log.ter).to.eql('abc');
|
|
56
|
-
});
|
|
57
|
-
it('should be callable without the data object', () => {
|
|
58
|
-
logger.info('wedidit');
|
|
59
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
60
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('wedidit');
|
|
61
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
62
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(30);
|
|
63
|
-
});
|
|
64
|
-
it('should not call log info method when disabled', () => {
|
|
65
|
-
logger = new logger_1.Logger('mongo', false);
|
|
66
|
-
logger.info('hi');
|
|
67
|
-
(0, chai_1.expect)(logger_1.Logger.config.output).not.to.have.been.called;
|
|
68
|
-
});
|
|
69
|
-
it('should not call log info method when disabled (timer)', () => {
|
|
70
|
-
logger = new logger_1.Logger('mongo', false);
|
|
71
|
-
const timer = logger.timer();
|
|
72
|
-
const infoStub = sinon_1.default.stub(logger, 'info');
|
|
73
|
-
clock.tick(100);
|
|
74
|
-
timer.info('hi');
|
|
75
|
-
(0, chai_1.expect)(infoStub).to.have.been.calledWith('hi', { event: { duration: 100 } });
|
|
76
|
-
});
|
|
77
|
-
it('should log error with action (legacy format)', () => {
|
|
78
|
-
logger_1.Logger.configure({ outputFormat: 'legacy' });
|
|
79
|
-
const error = new Error('failed');
|
|
80
|
-
error.data = { test: 'data' };
|
|
81
|
-
logger.fromError('hi', error, { details: 'here' });
|
|
82
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
83
|
-
(0, chai_1.expect)(logArguments.name).to.eql('mongo');
|
|
84
|
-
(0, chai_1.expect)(logArguments.action).to.eql('hi');
|
|
85
|
-
(0, chai_1.expect)(logArguments.level).to.eql(50);
|
|
86
|
-
(0, chai_1.expect)(logArguments.details).to.eql('here');
|
|
87
|
-
(0, chai_1.expect)(logArguments.error_name).to.eql(error.name);
|
|
88
|
-
(0, chai_1.expect)(logArguments.error_stack).to.eql(error.stack);
|
|
89
|
-
(0, chai_1.expect)(logArguments.error_message).to.eql(error.message);
|
|
90
|
-
(0, chai_1.expect)(logArguments.error_data).to.eql(JSON.stringify(error.data));
|
|
91
|
-
});
|
|
92
|
-
it('should log error with action (ecs format)', () => {
|
|
93
|
-
logger_1.Logger.configure({ outputFormat: 'ecs' });
|
|
94
|
-
const error = new Error('failed');
|
|
95
|
-
error.data = { test: 'data' };
|
|
96
|
-
logger.fromError('hi', error, { details: 'here' });
|
|
97
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
98
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('hi');
|
|
99
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
100
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(50);
|
|
101
|
-
(0, chai_1.expect)(logArguments.details).to.eql('here');
|
|
102
|
-
(0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
|
|
103
|
-
(0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
|
|
104
|
-
(0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
|
|
105
|
-
(0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(error.data));
|
|
106
|
-
});
|
|
107
|
-
it('should not overwrite ecs error fields with custom data', () => {
|
|
108
|
-
const error = new Error('failed');
|
|
109
|
-
error.data = { test: 'data' };
|
|
110
|
-
logger.fromError('hi', error, { error: { bajvan: true } });
|
|
111
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
112
|
-
(0, chai_1.expect)(logArguments.error).to.eql({
|
|
113
|
-
type: error.name,
|
|
114
|
-
stack_trace: error.stack,
|
|
115
|
-
message: error.message,
|
|
116
|
-
context: JSON.stringify(error.data),
|
|
117
|
-
bajvan: true,
|
|
118
|
-
});
|
|
119
|
-
});
|
|
120
|
-
it('should log error as warning with action', () => {
|
|
121
|
-
const error = new Error('failed');
|
|
122
|
-
error.data = { test: 'data' };
|
|
123
|
-
logger.warnFromError('hi', error, { details: 'here' });
|
|
124
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
125
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('hi');
|
|
126
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
127
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(40);
|
|
128
|
-
(0, chai_1.expect)(logArguments.details).to.eql('here');
|
|
129
|
-
(0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
|
|
130
|
-
(0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
|
|
131
|
-
(0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
|
|
132
|
-
(0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(error.data));
|
|
133
|
-
});
|
|
134
|
-
it('should not log error data when it is undefined', () => {
|
|
135
|
-
const error = new Error('failed');
|
|
136
|
-
logger.warnFromError('hi', error, { details: 'here' });
|
|
137
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
138
|
-
(0, chai_1.expect)(logArguments.error).to.not.have.any.keys('context');
|
|
139
|
-
});
|
|
140
|
-
it('should log only 3000 character of data', () => {
|
|
141
|
-
const error = new Error('failed');
|
|
142
|
-
error.data = 'exactlyTen'.repeat(400);
|
|
143
|
-
logger.warnFromError('hi', error, { details: 'here' });
|
|
144
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
145
|
-
(0, chai_1.expect)(logArguments.error.context.length).to.eql(3004);
|
|
146
|
-
});
|
|
147
|
-
it('should log request/response details for Axios-like error objects (legacy format)', () => {
|
|
148
|
-
logger_1.Logger.configure({ outputFormat: 'legacy' });
|
|
149
|
-
const error = new axios_1.AxiosError('Request failed with status code 500');
|
|
150
|
-
error.response = {
|
|
151
|
-
status: 500,
|
|
152
|
-
statusText: 'Something horrible happened',
|
|
153
|
-
data: { useful_detail: 'important info' },
|
|
154
|
-
headers: {},
|
|
155
|
-
config: { headers: {} },
|
|
156
|
-
};
|
|
157
|
-
error.config = {
|
|
158
|
-
url: 'http://amazinghost.com/beautiful-path',
|
|
159
|
-
method: 'get',
|
|
160
|
-
headers: {},
|
|
161
|
-
};
|
|
162
|
-
logger.fromError('hi', error, { details: 'here' });
|
|
163
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
164
|
-
(0, chai_1.expect)(logArguments.name).to.eql('mongo');
|
|
165
|
-
(0, chai_1.expect)(logArguments.action).to.eql('hi');
|
|
166
|
-
(0, chai_1.expect)(logArguments.level).to.eql(50);
|
|
167
|
-
(0, chai_1.expect)(logArguments.error_name).to.eql(error.name);
|
|
168
|
-
(0, chai_1.expect)(logArguments.error_stack).to.eql(error.stack);
|
|
169
|
-
(0, chai_1.expect)(logArguments.error_message).to.eql(error.message);
|
|
170
|
-
(0, chai_1.expect)(logArguments.request_method).to.eql(error.config.method);
|
|
171
|
-
(0, chai_1.expect)(logArguments.request_url).to.eql(error.config.url);
|
|
172
|
-
(0, chai_1.expect)(logArguments.response_status).to.eql(error.response.status);
|
|
173
|
-
(0, chai_1.expect)(logArguments.response_status_text).to.eql(error.response.statusText);
|
|
174
|
-
(0, chai_1.expect)(logArguments.response_data).to.eql(JSON.stringify(error.response.data));
|
|
175
|
-
});
|
|
176
|
-
it('should log request/response details for Axios-like error objects (ecs format)', () => {
|
|
177
|
-
logger_1.Logger.configure({ outputFormat: 'ecs' });
|
|
178
|
-
const error = new axios_1.AxiosError('Request failed with status code 500');
|
|
179
|
-
error.response = {
|
|
180
|
-
status: 500,
|
|
181
|
-
statusText: 'Something horrible happened',
|
|
182
|
-
data: { useful_detail: 'important info' },
|
|
183
|
-
headers: {},
|
|
184
|
-
config: { headers: {} },
|
|
185
|
-
};
|
|
186
|
-
error.config = {
|
|
187
|
-
url: 'http://amazinghost.com/beautiful-path',
|
|
188
|
-
method: 'get',
|
|
189
|
-
headers: {},
|
|
190
|
-
};
|
|
191
|
-
error.code = 'ECONNREINVENTED';
|
|
192
|
-
logger.fromError('hi', error, { details: 'here' });
|
|
193
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
194
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
195
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('hi');
|
|
196
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(50);
|
|
197
|
-
(0, chai_1.expect)(logArguments.error.code).to.eql(error.code);
|
|
198
|
-
(0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
|
|
199
|
-
(0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
|
|
200
|
-
(0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
|
|
201
|
-
(0, chai_1.expect)(logArguments.http.request.method).to.eql(error.config?.method);
|
|
202
|
-
(0, chai_1.expect)(logArguments.url.full).to.eql(error.config?.url);
|
|
203
|
-
(0, chai_1.expect)(logArguments.http.response.status_code).to.eql(error.response?.status);
|
|
204
|
-
(0, chai_1.expect)(logArguments.http.response.body.content).to.eql(JSON.stringify(error.response?.data));
|
|
205
|
-
});
|
|
206
|
-
describe('#customError', () => {
|
|
207
|
-
it('should log error as the given severity with action', () => {
|
|
208
|
-
const error = new Error('failed');
|
|
209
|
-
error.data = { test: 'data' };
|
|
210
|
-
logger.customError('info', 'hi', error, { details: 'here' });
|
|
211
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
212
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('hi');
|
|
213
|
-
(0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
|
|
214
|
-
(0, chai_1.expect)(logArguments.log.level).to.eql(30);
|
|
215
|
-
(0, chai_1.expect)(logArguments.details).to.eql('here');
|
|
216
|
-
(0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
|
|
217
|
-
(0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
|
|
218
|
-
(0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
|
|
219
|
-
(0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(error.data));
|
|
220
|
-
});
|
|
221
|
-
it('should not log error data when it is undefined', () => {
|
|
222
|
-
const error = new Error('failed');
|
|
223
|
-
logger.customError('warn', 'hi', error, { details: 'here' });
|
|
224
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
225
|
-
(0, chai_1.expect)(logArguments.error).to.not.have.any.keys('context');
|
|
226
|
-
});
|
|
227
|
-
it('should not log error code when it is undefined in the AxiosError object', () => {
|
|
228
|
-
const error = new axios_1.AxiosError('failed');
|
|
229
|
-
logger.customError('warn', 'hi', error, { details: 'here' });
|
|
230
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
231
|
-
(0, chai_1.expect)(logArguments.error).to.not.have.any.keys('code');
|
|
232
|
-
});
|
|
233
|
-
it('should log only 3000 character of data', () => {
|
|
234
|
-
const error = new Error('failed');
|
|
235
|
-
error.data = 'exactlyTen'.repeat(400);
|
|
236
|
-
logger.customError('error', 'hi', error, { details: 'here' });
|
|
237
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
238
|
-
(0, chai_1.expect)(logArguments.error.context.length).to.eql(3004);
|
|
239
|
-
});
|
|
240
|
-
describe('when not an Error instance is passed as error', () => {
|
|
241
|
-
[
|
|
242
|
-
{ type: 'custom object', value: {} },
|
|
243
|
-
{ type: 'string', value: 'error' },
|
|
244
|
-
{ type: 'null', value: null },
|
|
245
|
-
{ type: 'number', value: 12 },
|
|
246
|
-
{ type: 'bool', value: true },
|
|
247
|
-
].forEach(({ type, value }) => {
|
|
248
|
-
it(`should not throw error when ${type} is passed as error`, () => {
|
|
249
|
-
(0, chai_1.expect)(() => logger.customError('error', 'hi', value, { details: 'here' })).to.not.throw();
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
it('should log error properties from custom error object', () => {
|
|
253
|
-
const errorObject = { name: 'Error', message: 'My custom error', stack: 'Stack', data: { value: 1 } };
|
|
254
|
-
logger.customError('error', 'hi', errorObject, { details: 'here' });
|
|
255
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
256
|
-
(0, chai_1.expect)(logArguments.error.type).to.eql(errorObject.name);
|
|
257
|
-
(0, chai_1.expect)(logArguments.error.stack_trace).to.eql(errorObject.stack);
|
|
258
|
-
(0, chai_1.expect)(logArguments.error.message).to.eql(errorObject.message);
|
|
259
|
-
(0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(errorObject.data));
|
|
260
|
-
});
|
|
261
|
-
it('should not log additional or missing error properties from custom error object', () => {
|
|
262
|
-
const errorObject = { color: 'color', value: 'value' };
|
|
263
|
-
// @ts-expect-error TS2345
|
|
264
|
-
logger.customError('error', 'hi', errorObject, { details: 'here' });
|
|
265
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
266
|
-
(0, chai_1.expect)(logArguments).to.not.have.any.keys('error_name', 'error_stack', 'error_message', 'error_data');
|
|
267
|
-
(0, chai_1.expect)(logArguments).to.not.have.any.keys('color', 'value');
|
|
268
|
-
});
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
describe('#configure', () => {
|
|
272
|
-
it('should change format method', () => {
|
|
273
|
-
const formattedOutput = '{"my":"method"}';
|
|
274
|
-
const formatterStub = sinon_1.default.stub();
|
|
275
|
-
formatterStub.returns(formattedOutput);
|
|
276
|
-
logger_1.Logger.configure({
|
|
277
|
-
formatter: formatterStub,
|
|
278
|
-
});
|
|
279
|
-
logger.info('hi');
|
|
280
|
-
(0, chai_1.expect)(formatterStub).to.have.been.called;
|
|
281
|
-
(0, chai_1.expect)(logger_1.Logger.config.output).to.have.been.calledWith(formattedOutput);
|
|
282
|
-
});
|
|
283
|
-
it('should change output method', () => {
|
|
284
|
-
const outputStub = sinon_1.default.stub();
|
|
285
|
-
logger_1.Logger.configure({
|
|
286
|
-
output: outputStub,
|
|
287
|
-
});
|
|
288
|
-
logger.info('hi');
|
|
289
|
-
(0, chai_1.expect)(outputStub).to.have.been.called;
|
|
290
|
-
});
|
|
291
|
-
it('should change output format', () => {
|
|
292
|
-
logger_1.Logger.configure({
|
|
293
|
-
outputFormat: 'legacy',
|
|
294
|
-
});
|
|
295
|
-
logger.info('hi');
|
|
296
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
297
|
-
(0, chai_1.expect)(logArguments.action).to.eql('hi');
|
|
298
|
-
(0, chai_1.expect)(logArguments.event).to.be.undefined;
|
|
299
|
-
});
|
|
300
|
-
it('should throw error on invalid config', () => {
|
|
301
|
-
try {
|
|
302
|
-
logger_1.Logger.configure({
|
|
303
|
-
// @ts-expect-error TS2345
|
|
304
|
-
invalid: true,
|
|
305
|
-
});
|
|
306
|
-
throw new Error('should throw');
|
|
307
|
-
}
|
|
308
|
-
catch (e) {
|
|
309
|
-
(0, chai_1.expect)(e.message).to.eql('Only the following keys are allowed: output,formatter,transformers,outputFormat');
|
|
310
|
-
}
|
|
311
|
-
});
|
|
312
|
-
it('should modify logged data based on transformers', () => {
|
|
313
|
-
logger_1.Logger.configure({
|
|
314
|
-
transformers: [(log) => Object.assign({ debug: true }, log)],
|
|
315
|
-
});
|
|
316
|
-
logger.info('hi');
|
|
317
|
-
const logArguments = JSON.parse(outputStub.args[0][0]);
|
|
318
|
-
(0, chai_1.expect)(logArguments.event.action).to.eql('hi');
|
|
319
|
-
(0, chai_1.expect)(logArguments.debug).to.eql(true);
|
|
320
|
-
});
|
|
321
|
-
});
|
|
322
|
-
});
|
package/dist/setup.spec.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/setup.spec.js
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
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
|
-
const sinon_1 = __importDefault(require("sinon"));
|
|
7
|
-
const chai_1 = __importDefault(require("chai"));
|
|
8
|
-
const sinon_chai_1 = __importDefault(require("sinon-chai"));
|
|
9
|
-
chai_1.default.use(sinon_chai_1.default);
|
|
10
|
-
afterEach(() => {
|
|
11
|
-
sinon_1.default.restore();
|
|
12
|
-
});
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/timer/timer.spec.js
DELETED
|
@@ -1,83 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
const chai_1 = require("chai");
|
|
4
|
-
const sinon_1 = require("sinon");
|
|
5
|
-
const logger_1 = require("../logger/logger");
|
|
6
|
-
const timer_1 = require("./timer");
|
|
7
|
-
describe('Timer', () => {
|
|
8
|
-
let clock;
|
|
9
|
-
beforeEach(() => {
|
|
10
|
-
clock = (0, sinon_1.useFakeTimers)();
|
|
11
|
-
});
|
|
12
|
-
afterEach(() => {
|
|
13
|
-
clock.restore();
|
|
14
|
-
logger_1.Logger.configure({ outputFormat: 'ecs' });
|
|
15
|
-
});
|
|
16
|
-
describe('legacy format', () => {
|
|
17
|
-
beforeEach(() => {
|
|
18
|
-
logger_1.Logger.configure({ outputFormat: 'legacy' });
|
|
19
|
-
});
|
|
20
|
-
it('should log elapsed time (legacy format)', () => {
|
|
21
|
-
const logger = new logger_1.Logger('test', false);
|
|
22
|
-
const infoStub = (0, sinon_1.stub)(logger, 'info');
|
|
23
|
-
const timer = new timer_1.Timer(logger);
|
|
24
|
-
clock.tick(100);
|
|
25
|
-
timer.info('time', { customer_id: 10 });
|
|
26
|
-
(0, chai_1.expect)(infoStub).to.have.been.calledWith('time', { customer_id: 10, duration: 100 });
|
|
27
|
-
});
|
|
28
|
-
it('should log elapsed time with error', () => {
|
|
29
|
-
const logger = new logger_1.Logger('test', false);
|
|
30
|
-
const errorStub = (0, sinon_1.stub)(logger, 'fromError');
|
|
31
|
-
const timer = new timer_1.Timer(logger);
|
|
32
|
-
const error = new Error('intended');
|
|
33
|
-
clock.tick(100);
|
|
34
|
-
timer.fromError('time', error, { customer_id: 10 });
|
|
35
|
-
(0, chai_1.expect)(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
|
|
36
|
-
});
|
|
37
|
-
it('should log elapsed time with error', () => {
|
|
38
|
-
const logger = new logger_1.Logger('test', false);
|
|
39
|
-
const errorStub = (0, sinon_1.stub)(logger, 'warnFromError');
|
|
40
|
-
const timer = new timer_1.Timer(logger);
|
|
41
|
-
const error = new Error('intended');
|
|
42
|
-
clock.tick(100);
|
|
43
|
-
timer.warnFromError('time', error, { customer_id: 10 });
|
|
44
|
-
(0, chai_1.expect)(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, duration: 100 });
|
|
45
|
-
});
|
|
46
|
-
});
|
|
47
|
-
describe('ecs format', () => {
|
|
48
|
-
it('should log elapsed time (legacy format)', () => {
|
|
49
|
-
const logger = new logger_1.Logger('test', false);
|
|
50
|
-
const infoStub = (0, sinon_1.stub)(logger, 'info');
|
|
51
|
-
const timer = new timer_1.Timer(logger);
|
|
52
|
-
clock.tick(100);
|
|
53
|
-
timer.info('time', { customer_id: 10 });
|
|
54
|
-
(0, chai_1.expect)(infoStub).to.have.been.calledWith('time', { customer_id: 10, event: { duration: 100 } });
|
|
55
|
-
});
|
|
56
|
-
it('should log elapsed time with error', () => {
|
|
57
|
-
const logger = new logger_1.Logger('test', false);
|
|
58
|
-
const errorStub = (0, sinon_1.stub)(logger, 'fromError');
|
|
59
|
-
const timer = new timer_1.Timer(logger);
|
|
60
|
-
const error = new Error('intended');
|
|
61
|
-
clock.tick(100);
|
|
62
|
-
timer.fromError('time', error, { customer_id: 10 });
|
|
63
|
-
(0, chai_1.expect)(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, event: { duration: 100 } });
|
|
64
|
-
});
|
|
65
|
-
it('should log elapsed time with error', () => {
|
|
66
|
-
const logger = new logger_1.Logger('test', false);
|
|
67
|
-
const errorStub = (0, sinon_1.stub)(logger, 'warnFromError');
|
|
68
|
-
const timer = new timer_1.Timer(logger);
|
|
69
|
-
const error = new Error('intended');
|
|
70
|
-
clock.tick(100);
|
|
71
|
-
timer.warnFromError('time', error, { customer_id: 10 });
|
|
72
|
-
(0, chai_1.expect)(errorStub).to.have.been.calledWith('time', error, { customer_id: 10, event: { duration: 100 } });
|
|
73
|
-
});
|
|
74
|
-
it('should not overwrite ecs fields', () => {
|
|
75
|
-
const logger = new logger_1.Logger('test', false);
|
|
76
|
-
const logStub = (0, sinon_1.stub)(logger, 'warn');
|
|
77
|
-
const timer = new timer_1.Timer(logger);
|
|
78
|
-
clock.tick(100);
|
|
79
|
-
timer.warn('time', { event: { majomkutya: 1 } });
|
|
80
|
-
(0, chai_1.expect)(logStub).to.have.been.calledWith('time', { event: { duration: 100, majomkutya: 1 } });
|
|
81
|
-
});
|
|
82
|
-
});
|
|
83
|
-
});
|