Skip to content

Commit 9f01386

Browse files
committed
fix(error): limit stack trace size
1 parent e3a3a99 commit 9f01386

File tree

18 files changed

+131
-109
lines changed

18 files changed

+131
-109
lines changed

bin/json-logger.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#! /usr/bin/env node
22
'use strict';
3-
let eventStream = require('event-stream');
4-
let transformLine = require('../src/output/transform/transform');
3+
4+
const eventStream = require('event-stream');
5+
const transformLine = require('../src/output/transform/transform');
56

67
process.stdin.setEncoding('utf8');
78
process.stdin

package-lock.json

Lines changed: 69 additions & 64 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/config.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
'use strict';
2-
let chalk = require('chalk');
32

4-
let levels = {
3+
const chalk = require('chalk');
4+
5+
const levels = {
56
trace: {
67
number: 10,
78
name: 'TRACE'
@@ -28,9 +29,9 @@ let levels = {
2829
},
2930
};
3031

31-
let availableLevels = Object.keys(levels);
32+
const availableLevels = Object.keys(levels);
3233

33-
let coloredNames = {};
34+
const coloredNames = {};
3435
availableLevels.forEach((levelName) => {
3536
coloredNames[levels[levelName].number] = levels[levelName].name;
3637
});

src/enabled/enabled.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
function isNamespaceEnabled(availableNamespaces, namespace) {
44
availableNamespaces = availableNamespaces.split(/[\s,]+/);
55
let enabled = false;
6-
let adds = [];
7-
let skips = [];
6+
const adds = [];
7+
const skips = [];
88

99
availableNamespaces.forEach(function(name) {
1010
if (!name) {

src/enabled/enabled.spec.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
let isNamespaceEnabled = require('./enabled');
2+
3+
const isNamespaceEnabled = require('./enabled');
34

45
describe('isNamespaceAvailable', function() {
56
it('should enable when variables only contain one', function() {
@@ -21,7 +22,7 @@ describe('isNamespaceAvailable', function() {
2122
});
2223

2324
it('should allow multiple available namespaces', function() {
24-
let availableNamespaces = 'mongo,redis';
25+
const availableNamespaces = 'mongo,redis';
2526

2627
expect(isNamespaceEnabled(
2728
availableNamespaces, 'mongo'

src/index.spec.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
'use strict';
2-
let logFactory = require('../index');
3-
let Logger = require('./logger/logger');
2+
3+
const logFactory = require('../index');
4+
const Logger = require('./logger/logger');
45

56
describe('LogFactory', function() {
67
beforeEach(function() {
@@ -12,14 +13,14 @@ describe('LogFactory', function() {
1213
});
1314

1415
it('should return an enabled log instance when env variable is set to same', function() {
15-
let logger = logFactory('mongo');
16+
const logger = logFactory('mongo');
1617

1718
expect(logger).to.be.an.instanceOf(Logger);
1819
expect(logger.enabled).to.be.true;
1920
});
2021

2122
it('should return a disabled log instance when different', function() {
22-
let logger = logFactory('redis');
23+
const logger = logFactory('redis');
2324

2425
expect(logger).to.be.an.instanceOf(Logger);
2526
expect(logger.enabled).to.be.false;
@@ -28,7 +29,7 @@ describe('LogFactory', function() {
2829
it('should be mockable through public interface', function() {
2930
this.sandbox.stub(logFactory.Logger.prototype, 'info');
3031

31-
let logger = logFactory('mongo');
32+
const logger = logFactory('mongo');
3233
logger.info('hello');
3334

3435
expect(logFactory.Logger.prototype.info).to.have.been.calledWith('hello');

src/logger/logger.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
'use strict';
22

3-
let config = require('../config');
3+
const config = require('../config');
44
const continuationLocalStorage = require('cls-hooked');
5-
5+
const STACK_TRACE_LIMIT = 4000;
66

77
let logMethodFactory = function(level) {
88
return function(action, data) {
@@ -35,10 +35,16 @@ class Logger {
3535
fromError(action, error, options = {}) {
3636
this.error(action, Object.assign({
3737
error_name: error.name,
38-
error_stack: error.stack,
38+
error_stack: this._shortenStackTrace(error),
3939
error_message: error.message
4040
}, options));
4141
}
42+
43+
_shortenStackTrace(error) {
44+
return error.stack.length > STACK_TRACE_LIMIT
45+
? error.stack.substring(0, STACK_TRACE_LIMIT) + ' ...'
46+
: error.stack
47+
}
4248
}
4349

4450
Logger.prototype.trace = logMethodFactory('trace');

src/logger/logger.spec.js

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use strict';
2-
let Logger = require('./logger');
2+
3+
const Logger = require('./logger');
34
const continuationLocalStorage = require('cls-hooked');
45

56
describe('Logger', function() {
@@ -13,21 +14,21 @@ describe('Logger', function() {
1314
it('should call log info method when enabled', function() {
1415
logger.info('wedidit', { details: 'forever' });
1516

16-
let logArguments = JSON.parse(console.log.args[0]);
17+
const logArguments = JSON.parse(console.log.args[0]);
1718
expect(logArguments.name).to.eql('mongo');
1819
expect(logArguments.action).to.eql('wedidit');
1920
expect(logArguments.level).to.eql(30);
2021
expect(logArguments.details).to.eql('forever');
2122
});
2223

2324
it('should log prequest id if there is namespace present', function() {
24-
let namespace = continuationLocalStorage
25+
const namespace = continuationLocalStorage
2526
.createNamespace('session');
2627

2728
namespace.run(function(){
2829
namespace.set('request_id', 'uid');
2930
logger.info('wedidit');
30-
let logArguments = JSON.parse(console.log.args[0]);
31+
const logArguments = JSON.parse(console.log.args[0]);
3132
expect(logArguments.request_id).to.eql('uid');
3233
});
3334
});
@@ -41,11 +42,11 @@ describe('Logger', function() {
4142
});
4243

4344
it('should log error with action', function() {
44-
let error = new Error('failed');
45+
const error = new Error('failed');
4546

4647
logger.fromError('hi', error, { details: 'here' });
4748

48-
let logArguments = JSON.parse(console.log.args[0]);
49+
const logArguments = JSON.parse(console.log.args[0]);
4950
expect(logArguments.name).to.eql('mongo');
5051
expect(logArguments.action).to.eql('hi');
5152
expect(logArguments.level).to.eql(50);

src/output/color-name/color-name.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
'use strict';
2-
let chalk = require('chalk');
32

4-
let colors = ['cyan', 'magenta', 'grey', 'blue', 'green', 'yellow', 'white', 'red'];
3+
const chalk = require('chalk');
4+
5+
const colors = ['cyan', 'magenta', 'grey', 'blue', 'green', 'yellow', 'white', 'red'];
6+
const names = {};
57
let colorCounter = 0;
6-
let names = {};
78

89
module.exports = function colorName(name) {
910
if (!names[name]) {

src/output/color-name/color-name.spec.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
let colorName = require('./color-name');
3+
const colorName = require('./color-name');
44

55
describe('colorName', function() {
66
it('should pick the first color for the name', function() {
@@ -12,8 +12,8 @@ describe('colorName', function() {
1212
});
1313

1414
it('should add different colors for different names', function() {
15-
let firstName = colorName('mongo');
16-
let secondName = colorName('redis');
15+
const firstName = colorName('mongo');
16+
const secondName = colorName('redis');
1717

1818
expect(secondName.replace('redis', 'mongo')).not.to.eql(firstName);
1919
});

0 commit comments

Comments
 (0)