Skip to content

Commit c2fb05c

Browse files
committed
feat(transformers): add ability to add global formatters
1 parent a8ee4a1 commit c2fb05c

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

src/logger/logger.js

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const DATA_LIMIT = 3000;
77
const Timer = require('../timer/timer');
88
const jsonFormatter = require('../formatter/json');
99
const consoleOutput = require('../output/console');
10-
const allowedKeys = ['output', 'formatter'];
10+
const allowedKeys = ['output', 'formatter', 'transformers'];
1111

1212
const getContextStorage = function() {
1313
const contextNamespace = continuationLocalStorage.getNamespace('session');
@@ -43,21 +43,11 @@ class Logger {
4343
}
4444

4545
fromError(action, error, data = {}) {
46-
this.error(action, Object.assign({
47-
error_name: error.name,
48-
error_stack: this._shortenStackTrace(error.stack),
49-
error_message: error.message,
50-
error_data: this._shortenData(error.data)
51-
}, data));
46+
this.error(action, Object.assign(this._getErrorDetails(error), data));
5247
}
5348

5449
warnFromError(action, error, data = {}) {
55-
this.warn(action, Object.assign({
56-
error_name: error.name,
57-
error_stack: this._shortenStackTrace(error.stack),
58-
error_message: error.message,
59-
error_data: this._shortenData(error.data)
60-
}, data));
50+
this.warn(action, Object.assign(this._getErrorDetails(error), data));
6151
}
6252

6353
timer() {
@@ -81,11 +71,21 @@ class Logger {
8171
? stringifiedData.substring(0, DATA_LIMIT) + ' ...'
8272
: stringifiedData
8373
}
74+
75+
_getErrorDetails(error) {
76+
return {
77+
error_name: error.name,
78+
error_stack: this._shortenStackTrace(error.stack),
79+
error_message: error.message,
80+
error_data: this._shortenData(error.data)
81+
}
82+
}
8483
}
8584

8685
Logger.config = {
8786
formatter: jsonFormatter,
88-
output: consoleOutput
87+
output: consoleOutput,
88+
transformers: []
8989
};
9090

9191
const logMethodFactory = function(level) {
@@ -94,7 +94,7 @@ const logMethodFactory = function(level) {
9494
return;
9595
}
9696

97-
const dataToLog = Object.assign(
97+
let dataToLog = Object.assign(
9898
{
9999
name: this._namespace,
100100
action: action,
@@ -105,6 +105,10 @@ const logMethodFactory = function(level) {
105105
data
106106
);
107107

108+
Logger.config.transformers.forEach((transform) => {
109+
dataToLog = transform(dataToLog)
110+
});
111+
108112
Logger.config.output(
109113
Logger.config.formatter(dataToLog)
110114
);

src/logger/logger.spec.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ describe('Logger', function() {
1616
afterEach(function() {
1717
Logger.configure({
1818
formatter: jsonFormatter,
19-
output: consoleOutput
19+
output: consoleOutput,
20+
transformers: []
2021
});
2122
});
2223

@@ -167,5 +168,19 @@ describe('Logger', function() {
167168
expect(e.message).to.eql('Only the following keys are allowed: formatter, output');
168169
}
169170
});
171+
172+
it('should modify logged data based on transformers', function() {
173+
Logger.configure({
174+
transformers: [
175+
log => Object.assign({ debug: true }, log)
176+
]
177+
});
178+
179+
logger.info('hi');
180+
181+
const logArguments = JSON.parse(console.log.args[0]);
182+
expect(logArguments.action).to.eql('hi');
183+
expect(logArguments.debug).to.eql(true);
184+
});
170185
});
171186
});

0 commit comments

Comments
 (0)