Skip to content

Commit 097a0d7

Browse files
committed
feat(logger): log data if exists with proper limit
1 parent bf2225d commit 097a0d7

File tree

2 files changed

+63
-9
lines changed

2 files changed

+63
-9
lines changed

src/logger/logger.js

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
const config = require('../config');
44
const continuationLocalStorage = require('cls-hooked');
5-
const STACK_TRACE_LIMIT = 4000;
5+
const STACK_TRACE_LIMIT = 3000;
6+
const DATA_LIMIT = 3000;
67
const Timer = require('../timer/timer');
78
const jsonFormatter = require('../formatter/json');
89
const consoleOutput = require('../output/console');
@@ -44,27 +45,41 @@ class Logger {
4445
fromError(action, error, data = {}) {
4546
this.error(action, Object.assign({
4647
error_name: error.name,
47-
error_stack: this._shortenStackTrace(error),
48-
error_message: error.message
48+
error_stack: this._shortenStackTrace(error.stack),
49+
error_message: error.message,
50+
error_data: this._shortenData(error.data)
4951
}, data));
5052
}
5153

5254
warnFromError(action, error, data = {}) {
5355
this.warn(action, Object.assign({
5456
error_name: error.name,
55-
error_stack: this._shortenStackTrace(error),
56-
error_message: error.message
57+
error_stack: this._shortenStackTrace(error.stack),
58+
error_message: error.message,
59+
error_data: this._shortenData(error.data)
5760
}, data));
5861
}
5962

6063
timer() {
6164
return new Timer(this);
6265
}
6366

64-
_shortenStackTrace(error) {
65-
return error.stack.length > STACK_TRACE_LIMIT
66-
? error.stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
67-
: error.stack
67+
_shortenStackTrace(stack) {
68+
return stack.length > STACK_TRACE_LIMIT
69+
? stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
70+
: stack
71+
}
72+
73+
_shortenData(data) {
74+
if (typeof data === 'undefined') {
75+
return;
76+
}
77+
78+
const stringifiedData = typeof data === 'object' ? JSON.stringify(data) : data;
79+
80+
return stringifiedData.length > DATA_LIMIT
81+
? stringifiedData.substring(0, DATA_LIMIT) + ' ...'
82+
: stringifiedData
6883
}
6984
}
7085

src/logger/logger.spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ describe('Logger', function() {
6363

6464
it('should log error with action', function() {
6565
const error = new Error('failed');
66+
error.data = { test: 'data' };
6667

6768
logger.fromError('hi', error, { details: 'here' });
6869

@@ -75,10 +76,12 @@ describe('Logger', function() {
7576
expect(logArguments.error_name).to.eql(error.name);
7677
expect(logArguments.error_stack).to.eql(error.stack);
7778
expect(logArguments.error_message).to.eql(error.message);
79+
expect(logArguments.error_data).to.eql(JSON.stringify(error.data));
7880
});
7981

8082
it('should log error as warning with action', function() {
8183
const error = new Error('failed');
84+
error.data = { test: 'data' };
8285

8386
logger.warnFromError('hi', error, { details: 'here' });
8487

@@ -91,6 +94,42 @@ describe('Logger', function() {
9194
expect(logArguments.error_name).to.eql(error.name);
9295
expect(logArguments.error_stack).to.eql(error.stack);
9396
expect(logArguments.error_message).to.eql(error.message);
97+
expect(logArguments.error_data).to.eql(JSON.stringify(error.data));
98+
});
99+
100+
it('should not log error data when it is undefined', function() {
101+
const error = new Error('failed');
102+
103+
logger.warnFromError('hi', error, { details: 'here' });
104+
105+
const logArguments = JSON.parse(console.log.args[0]);
106+
expect(logArguments.name).to.eql('mongo');
107+
expect(logArguments.action).to.eql('hi');
108+
expect(logArguments.level).to.eql(40);
109+
expect(logArguments.details).to.eql('here');
110+
111+
expect(logArguments.error_name).to.eql(error.name);
112+
expect(logArguments.error_stack).to.eql(error.stack);
113+
expect(logArguments.error_message).to.eql(error.message);
114+
expect(logArguments).to.not.have.any.keys('error_data');
115+
});
116+
117+
it('should log only 3000 character of data', function() {
118+
const error = new Error('failed');
119+
error.data = 'exactlyTen'.repeat(400);
120+
121+
logger.warnFromError('hi', error, { details: 'here' });
122+
123+
const logArguments = JSON.parse(console.log.args[0]);
124+
expect(logArguments.name).to.eql('mongo');
125+
expect(logArguments.action).to.eql('hi');
126+
expect(logArguments.level).to.eql(40);
127+
expect(logArguments.details).to.eql('here');
128+
129+
expect(logArguments.error_name).to.eql(error.name);
130+
expect(logArguments.error_stack).to.eql(error.stack);
131+
expect(logArguments.error_message).to.eql(error.message);
132+
expect(logArguments.error_data.length).to.eql(3004);
94133
});
95134

96135
describe('#configure', function() {

0 commit comments

Comments
 (0)