Skip to content

Commit

Permalink
[[FEAT]] Use dynamic context on pipes
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Mendiara Cañardo committed Mar 30, 2016
1 parent a5aab63 commit f710cfb
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 9 deletions.
34 changes: 26 additions & 8 deletions lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -134,14 +134,32 @@ function colorize(color, str) {
* @deprecated
*/
function formatTrace(level, context, message, args, err) {
args.unshift(
templateTrace + message,
(new Date()).toISOString(),
level,
context.corr || notAvailable,
context.trans || notAvailable,
context.op || notAvailable
);

var recontext = {
time: (new Date()).toISOString(),
lvl: level,
corr: context.corr || notAvailable,
trans: context.trans || notAvailable,
op: context.op || notAvailable
};

Object.keys(context)
.filter(function(key) {
return !(context[key] && Object.prototype.toString.call(context[key]) === '[object Function]');
})
.forEach(function(key) {
recontext[key] = context[key] || notAvailable;
});

recontext.msg = message;

var str = Object.keys(recontext)
.map(function(key) {
return key + '=' + recontext[key];
})
.join(' | ');

args.unshift(str);
if (err && message !== '' + err) {
args.push(err);
}
Expand Down
26 changes: 25 additions & 1 deletion test/format.pipe.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('Pipe format', function() {

describe('Logging Messages', function() {
beforeEach(function() {
sandbox.stub(logger, 'getContext').returns({});
sandbox.stub(logger, 'getContext').returns();
});

it('should log empty strings', function() {
Expand Down Expand Up @@ -154,6 +154,30 @@ describe('Pipe format', function() {
);
});

it('should log dynamic context properties', function() {
logger.info({ srv: 'Service', subsrv: 'Subservice'}, 'Format %s', 'works');
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=n/a | trans=n/a | op=n/a | srv=Service | subsrv=Subservice | msg=Format works'
);
});

it('should use corr, trans, op from context properties', function() {
logger.info({ corr: 1, trans: 2, op: '3', srv: 'Service' }, 'Format %s', 'works');
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=1 | trans=2 | op=3 | srv=Service | msg=Format works'
);
});

it('should use corr, trans, op from global context', function() {
logger.getContext = function() {
return { corr: 1, trans: 2, op: 'original'};
};
logger.info({ corr: 999, op: 'override', srv: 'Service' }, 'Format %s', 'works');
expect(logger._lastTrace).to.be.eql(
'time=1970-01-01T00:00:00.000Z | lvl=INFO | corr=999 | trans=2 | op=override | srv=Service | msg=Format works'
);
});

it('should be able to set custom not available', function() {
logger.formatters.setNotAvailable('NOTAVAILABLE');
logger.info('');
Expand Down

0 comments on commit f710cfb

Please sign in to comment.