@emartech/json-logger 8.1.1 → 9.0.1

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/config.js CHANGED
@@ -4,28 +4,28 @@ exports.config = void 0;
4
4
  const levels = {
5
5
  trace: {
6
6
  number: 10,
7
- name: 'TRACE'
7
+ name: 'TRACE',
8
8
  },
9
9
  debug: {
10
10
  number: 20,
11
- name: 'DEBUG'
11
+ name: 'DEBUG',
12
12
  },
13
13
  info: {
14
14
  number: 30,
15
- name: 'INFO'
15
+ name: 'INFO',
16
16
  },
17
17
  warn: {
18
18
  number: 40,
19
- name: 'WARN'
19
+ name: 'WARN',
20
20
  },
21
21
  error: {
22
22
  number: 50,
23
- name: 'ERROR'
23
+ name: 'ERROR',
24
24
  },
25
25
  fatal: {
26
26
  number: 60,
27
- name: 'FATAL'
28
- }
27
+ name: 'FATAL',
28
+ },
29
29
  };
30
30
  const availableLevels = Object.keys(levels);
31
31
  exports.config = { levels, availableLevels };
package/dist/index.js CHANGED
@@ -1,19 +1,20 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.createLogger = exports.Timer = exports.Logger = void 0;
3
+ exports.Timer = exports.Logger = void 0;
4
+ exports.createLogger = createLogger;
5
+ const enabled_1 = require("./enabled/enabled");
6
+ const formatter_1 = require("./formatter");
4
7
  const logger_1 = require("./logger/logger");
5
8
  var logger_2 = require("./logger/logger");
6
9
  Object.defineProperty(exports, "Logger", { enumerable: true, get: function () { return logger_2.Logger; } });
7
10
  var timer_1 = require("./timer/timer");
8
11
  Object.defineProperty(exports, "Timer", { enumerable: true, get: function () { return timer_1.Timer; } });
9
- const enabled_1 = require("./enabled/enabled");
10
- const formatter_1 = require("./formatter");
12
+ // eslint-disable-next-line func-style
11
13
  function createLogger(namespace) {
12
14
  return new logger_1.Logger(namespace, (0, enabled_1.isNamespaceEnabled)(createLogger.getNamespaces(), namespace));
13
15
  }
14
- exports.createLogger = createLogger;
15
16
  createLogger.getNamespaces = function () {
16
- return process.env.DEBUG || '';
17
+ return process.env.DEBUG ?? '';
17
18
  };
18
19
  createLogger.configure = function (options) {
19
20
  logger_1.Logger.configure(options);
@@ -3,7 +3,7 @@ export interface LoggerConfig {
3
3
  formatter: Function;
4
4
  output: Function;
5
5
  transformers: Function[];
6
- outputFormat: String;
6
+ outputFormat: string;
7
7
  }
8
8
  export declare class Logger {
9
9
  private readonly namespace;
@@ -1,15 +1,18 @@
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 lodash_1 = require("lodash");
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 {
14
+ namespace;
15
+ enabled;
13
16
  constructor(namespace, enabled) {
14
17
  this.namespace = namespace;
15
18
  this.enabled = enabled;
@@ -25,6 +28,12 @@ class Logger {
25
28
  }
26
29
  });
27
30
  }
31
+ static config = {
32
+ formatter: json_1.jsonFormatter,
33
+ output: console_1.consoleOutput,
34
+ transformers: [],
35
+ outputFormat: 'ecs',
36
+ };
28
37
  isEnabled() {
29
38
  return this.enabled;
30
39
  }
@@ -163,9 +172,3 @@ class Logger {
163
172
  }
164
173
  }
165
174
  exports.Logger = Logger;
166
- Logger.config = {
167
- formatter: json_1.jsonFormatter,
168
- output: console_1.consoleOutput,
169
- transformers: [],
170
- outputFormat: 'ecs',
171
- };
@@ -4,6 +4,8 @@ exports.Timer = void 0;
4
4
  const logger_1 = require("../logger/logger");
5
5
  const lodash_1 = require("lodash");
6
6
  class Timer {
7
+ start;
8
+ logger;
7
9
  constructor(logger) {
8
10
  this.logger = logger;
9
11
  this.start = new Date().getTime();
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 ./src/**/*.ts",
13
- "lint:fix": "eslint ./src/**/*.ts --fix",
12
+ "lint": "eslint .",
13
+ "lint:fix": "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",
@@ -36,35 +36,36 @@
36
36
  "json"
37
37
  ],
38
38
  "engines": {
39
- "node": ">=14"
39
+ "node": ">=20"
40
40
  },
41
41
  "dependencies": {
42
42
  "lodash": "^4.17.21"
43
43
  },
44
44
  "devDependencies": {
45
- "@types/chai": "4.3.3",
46
- "@types/lodash": "4.14.198",
47
- "@types/mocha": "10.0.0",
48
- "@types/node": "18.7.23",
49
- "@types/sinon": "10.0.13",
50
- "@types/sinon-chai": "3.2.8",
51
- "@typescript-eslint/eslint-plugin": "5.38.1",
52
- "@typescript-eslint/parser": "5.38.1",
53
- "axios": "0.30.0",
54
- "chai": "4.3.6",
55
- "eslint": "8.24.0",
45
+ "@types/chai": "4.3.20",
46
+ "@types/lodash": "4.17.20",
47
+ "@types/mocha": "10.0.10",
48
+ "@types/node": "20.19.22",
49
+ "@types/sinon": "17.0.4",
50
+ "@types/sinon-chai": "3.2.12",
51
+ "@typescript-eslint/eslint-plugin": "8.46.1",
52
+ "@typescript-eslint/parser": "8.46.1",
53
+ "axios": "1.12.2",
54
+ "chai": "4.5.0",
55
+ "eslint": "8.57.1",
56
56
  "eslint-config-emarsys": "5.1.0",
57
- "eslint-config-prettier": "8.5.0",
58
- "eslint-plugin-no-only-tests": "3.0.0",
59
- "eslint-plugin-prettier": "4.2.1",
60
- "eslint-plugin-security": "1.5.0",
61
- "mocha": "10.0.0",
62
- "prettier": "2.7.1",
63
- "semantic-release": "19.0.5",
64
- "sinon": "14.0.0",
57
+ "eslint-config-prettier": "10.1.8",
58
+ "eslint-plugin-chai-friendly": "1.1.0",
59
+ "eslint-plugin-no-only-tests": "3.3.0",
60
+ "eslint-plugin-prettier": "5.5.4",
61
+ "eslint-plugin-security": "3.0.1",
62
+ "mocha": "11.7.4",
63
+ "prettier": "3.6.2",
64
+ "semantic-release": "24.2.9",
65
+ "sinon": "21.0.0",
65
66
  "sinon-chai": "3.7.0",
66
- "ts-node": "10.9.1",
67
- "typescript": "4.8.4"
67
+ "ts-node": "10.9.2",
68
+ "typescript": "5.9.3"
68
69
  },
69
- "version": "8.1.1"
70
+ "version": "9.0.1"
70
71
  }
@@ -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
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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,320 +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 chai_1 = require("chai");
7
- const sinon_1 = __importDefault(require("sinon"));
8
- const logger_1 = require("./logger");
9
- const json_1 = require("../formatter/json");
10
- const console_1 = require("../output/console");
11
- const axios_1 = require("axios");
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: {},
156
- };
157
- error.config = {
158
- url: 'http://amazinghost.com/beautiful-path',
159
- method: 'get',
160
- };
161
- logger.fromError('hi', error, { details: 'here' });
162
- const logArguments = JSON.parse(outputStub.args[0][0]);
163
- (0, chai_1.expect)(logArguments.name).to.eql('mongo');
164
- (0, chai_1.expect)(logArguments.action).to.eql('hi');
165
- (0, chai_1.expect)(logArguments.level).to.eql(50);
166
- (0, chai_1.expect)(logArguments.error_name).to.eql(error.name);
167
- (0, chai_1.expect)(logArguments.error_stack).to.eql(error.stack);
168
- (0, chai_1.expect)(logArguments.error_message).to.eql(error.message);
169
- (0, chai_1.expect)(logArguments.request_method).to.eql(error.config.method);
170
- (0, chai_1.expect)(logArguments.request_url).to.eql(error.config.url);
171
- (0, chai_1.expect)(logArguments.response_status).to.eql(error.response.status);
172
- (0, chai_1.expect)(logArguments.response_status_text).to.eql(error.response.statusText);
173
- (0, chai_1.expect)(logArguments.response_data).to.eql(JSON.stringify(error.response.data));
174
- });
175
- it('should log request/response details for Axios-like error objects (ecs format)', () => {
176
- logger_1.Logger.configure({ outputFormat: 'ecs' });
177
- const error = new axios_1.AxiosError('Request failed with status code 500');
178
- error.response = {
179
- status: 500,
180
- statusText: 'Something horrible happened',
181
- data: { useful_detail: 'important info' },
182
- headers: {},
183
- config: {},
184
- };
185
- error.config = {
186
- url: 'http://amazinghost.com/beautiful-path',
187
- method: 'get',
188
- };
189
- error.code = 'ECONNREINVENTED';
190
- logger.fromError('hi', error, { details: 'here' });
191
- const logArguments = JSON.parse(outputStub.args[0][0]);
192
- (0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
193
- (0, chai_1.expect)(logArguments.event.action).to.eql('hi');
194
- (0, chai_1.expect)(logArguments.log.level).to.eql(50);
195
- (0, chai_1.expect)(logArguments.error.code).to.eql(error.code);
196
- (0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
197
- (0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
198
- (0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
199
- (0, chai_1.expect)(logArguments.http.request.method).to.eql(error.config.method);
200
- (0, chai_1.expect)(logArguments.url.full).to.eql(error.config.url);
201
- (0, chai_1.expect)(logArguments.http.response.status_code).to.eql(error.response.status);
202
- (0, chai_1.expect)(logArguments.http.response.body.content).to.eql(JSON.stringify(error.response.data));
203
- });
204
- describe('#customError', () => {
205
- it('should log error as the given severity with action', () => {
206
- const error = new Error('failed');
207
- error.data = { test: 'data' };
208
- logger.customError('info', 'hi', error, { details: 'here' });
209
- const logArguments = JSON.parse(outputStub.args[0][0]);
210
- (0, chai_1.expect)(logArguments.event.action).to.eql('hi');
211
- (0, chai_1.expect)(logArguments.log.logger).to.eql('mongo');
212
- (0, chai_1.expect)(logArguments.log.level).to.eql(30);
213
- (0, chai_1.expect)(logArguments.details).to.eql('here');
214
- (0, chai_1.expect)(logArguments.error.type).to.eql(error.name);
215
- (0, chai_1.expect)(logArguments.error.stack_trace).to.eql(error.stack);
216
- (0, chai_1.expect)(logArguments.error.message).to.eql(error.message);
217
- (0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(error.data));
218
- });
219
- it('should not log error data when it is undefined', () => {
220
- const error = new Error('failed');
221
- logger.customError('warn', 'hi', error, { details: 'here' });
222
- const logArguments = JSON.parse(outputStub.args[0][0]);
223
- (0, chai_1.expect)(logArguments.error).to.not.have.any.keys('context');
224
- });
225
- it('should not log error code when it is undefined in the AxiosError object', () => {
226
- const error = new axios_1.AxiosError('failed');
227
- logger.customError('warn', 'hi', error, { details: 'here' });
228
- const logArguments = JSON.parse(outputStub.args[0][0]);
229
- (0, chai_1.expect)(logArguments.error).to.not.have.any.keys('code');
230
- });
231
- it('should log only 3000 character of data', () => {
232
- const error = new Error('failed');
233
- error.data = 'exactlyTen'.repeat(400);
234
- logger.customError('error', 'hi', error, { details: 'here' });
235
- const logArguments = JSON.parse(outputStub.args[0][0]);
236
- (0, chai_1.expect)(logArguments.error.context.length).to.eql(3004);
237
- });
238
- describe('when not an Error instance is passed as error', () => {
239
- [
240
- { type: 'custom object', value: {} },
241
- { type: 'string', value: 'error' },
242
- { type: 'null', value: null },
243
- { type: 'number', value: 12 },
244
- { type: 'bool', value: true },
245
- ].forEach(({ type, value }) => {
246
- it(`should not throw error when ${type} is passed as error`, () => {
247
- (0, chai_1.expect)(() => logger.customError('error', 'hi', value, { details: 'here' })).to.not.throw();
248
- });
249
- });
250
- it('should log error properties from custom error object', () => {
251
- const errorObject = { name: 'Error', message: 'My custom error', stack: 'Stack', data: { value: 1 } };
252
- logger.customError('error', 'hi', errorObject, { details: 'here' });
253
- const logArguments = JSON.parse(outputStub.args[0][0]);
254
- (0, chai_1.expect)(logArguments.error.type).to.eql(errorObject.name);
255
- (0, chai_1.expect)(logArguments.error.stack_trace).to.eql(errorObject.stack);
256
- (0, chai_1.expect)(logArguments.error.message).to.eql(errorObject.message);
257
- (0, chai_1.expect)(logArguments.error.context).to.eql(JSON.stringify(errorObject.data));
258
- });
259
- it('should not log additional or missing error properties from custom error object', () => {
260
- const errorObject = { color: 'color', value: 'value' };
261
- // @ts-expect-error TS2345
262
- logger.customError('error', 'hi', errorObject, { details: 'here' });
263
- const logArguments = JSON.parse(outputStub.args[0][0]);
264
- (0, chai_1.expect)(logArguments).to.not.have.any.keys('error_name', 'error_stack', 'error_message', 'error_data');
265
- (0, chai_1.expect)(logArguments).to.not.have.any.keys('color', 'value');
266
- });
267
- });
268
- });
269
- describe('#configure', () => {
270
- it('should change format method', () => {
271
- const formattedOutput = '{"my":"method"}';
272
- const formatterStub = sinon_1.default.stub();
273
- formatterStub.returns(formattedOutput);
274
- logger_1.Logger.configure({
275
- formatter: formatterStub,
276
- });
277
- logger.info('hi');
278
- (0, chai_1.expect)(formatterStub).to.have.been.called;
279
- (0, chai_1.expect)(logger_1.Logger.config.output).to.have.been.calledWith(formattedOutput);
280
- });
281
- it('should change output method', () => {
282
- const outputStub = sinon_1.default.stub();
283
- logger_1.Logger.configure({
284
- output: outputStub,
285
- });
286
- logger.info('hi');
287
- (0, chai_1.expect)(outputStub).to.have.been.called;
288
- });
289
- it('should change output format', () => {
290
- logger_1.Logger.configure({
291
- outputFormat: 'legacy',
292
- });
293
- logger.info('hi');
294
- const logArguments = JSON.parse(outputStub.args[0][0]);
295
- (0, chai_1.expect)(logArguments.action).to.eql('hi');
296
- (0, chai_1.expect)(logArguments.event).to.be.undefined;
297
- });
298
- it('should throw error on invalid config', () => {
299
- try {
300
- logger_1.Logger.configure({
301
- // @ts-expect-error TS2345
302
- invalid: true,
303
- });
304
- throw new Error('should throw');
305
- }
306
- catch (e) {
307
- (0, chai_1.expect)(e.message).to.eql('Only the following keys are allowed: output,formatter,transformers,outputFormat');
308
- }
309
- });
310
- it('should modify logged data based on transformers', () => {
311
- logger_1.Logger.configure({
312
- transformers: [(log) => Object.assign({ debug: true }, log)],
313
- });
314
- logger.info('hi');
315
- const logArguments = JSON.parse(outputStub.args[0][0]);
316
- (0, chai_1.expect)(logArguments.event.action).to.eql('hi');
317
- (0, chai_1.expect)(logArguments.debug).to.eql(true);
318
- });
319
- });
320
- });
@@ -1 +0,0 @@
1
- export {};
@@ -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 {};
@@ -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
- });