node-bunyan-lite/test/buffer.test.js

82 lines
2.1 KiB
JavaScript
Raw Normal View History

/*
* Copyright (c) 2012 Trent Mick. All rights reserved.
* Copyright (c) 2012 Joyent Inc. All rights reserved.
*
* Test logging with (accidental) usage of buffers.
*/
var util = require('util'),
2013-03-29 00:42:32 +00:00
inspect = util.inspect,
format = util.format;
var bunyan = require('../lib/bunyan');
2013-01-07 19:18:12 +00:00
// node-tap API
if (require.cache[__dirname + '/tap4nodeunit.js'])
2013-03-29 00:42:32 +00:00
delete require.cache[__dirname + '/tap4nodeunit.js'];
2013-01-07 19:18:12 +00:00
var tap4nodeunit = require('./tap4nodeunit.js');
var after = tap4nodeunit.after;
var before = tap4nodeunit.before;
var test = tap4nodeunit.test;
function Catcher() {
2013-03-29 00:42:32 +00:00
this.records = [];
}
Catcher.prototype.write = function (record) {
2013-03-29 00:42:32 +00:00
this.records.push(record);
}
var catcher = new Catcher();
var log = new bunyan.createLogger({
2013-03-29 00:42:32 +00:00
name: 'buffer.test',
streams: [
{
type: 'raw',
stream: catcher,
level: 'trace'
}
]
});
test('log.info(BUFFER)', function (t) {
2013-03-29 00:42:32 +00:00
var b = new Buffer('foo');
2013-04-02 00:21:01 +00:00
['trace',
'debug',
'info',
'warn',
'error',
'fatal'].forEach(function (lvl) {
2013-03-29 00:42:32 +00:00
log[lvl].call(log, b);
var rec = catcher.records[catcher.records.length - 1];
t.equal(rec.msg, inspect(b),
format('log.%s msg is inspect(BUFFER)', lvl));
t.ok(rec['0'] === undefined,
'no "0" array index key in record: ' + inspect(rec['0']));
t.ok(rec['parent'] === undefined,
'no "parent" array index key in record: ' + inspect(rec['parent']));
2013-03-29 00:42:32 +00:00
log[lvl].call(log, b, 'bar');
var rec = catcher.records[catcher.records.length - 1];
2013-04-02 00:21:01 +00:00
t.equal(rec.msg, inspect(b) + ' bar', format(
'log.%s(BUFFER, "bar") msg is inspect(BUFFER) + " bar"', lvl));
2013-03-29 00:42:32 +00:00
});
2013-03-29 00:42:32 +00:00
t.end();
});
//test('log.info({buf: BUFFER})', function (t) {
// var b = new Buffer('foo');
//
// // Really there isn't much Bunyan can do here. See
// // <https://github.com/joyent/node/issues/3905>. An unwelcome hack would
// // be to monkey-patch in Buffer.toJSON. Bletch.
// log.info({buf: b}, 'my message');
// var rec = catcher.records[catcher.records.length - 1];
//
// t.end();
//});