2012-08-22 22:04:30 +00:00
|
|
|
/*
|
|
|
|
* 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;
|
2012-08-22 22:04:30 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
2012-08-22 22:04:30 +00:00
|
|
|
|
|
|
|
function Catcher() {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.records = [];
|
2012-08-22 22:04:30 +00:00
|
|
|
}
|
|
|
|
Catcher.prototype.write = function (record) {
|
2013-03-29 00:42:32 +00:00
|
|
|
this.records.push(record);
|
2012-08-22 22:04:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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'
|
|
|
|
}
|
|
|
|
]
|
2012-08-22 22:04:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
test('log.info(BUFFER)', function (t) {
|
2013-03-29 00:42:32 +00:00
|
|
|
var b = new Buffer('foo');
|
2012-08-22 22:04:30 +00:00
|
|
|
|
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']));
|
2012-08-22 22:04:30 +00:00
|
|
|
|
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
|
|
|
});
|
2012-08-22 22:04:30 +00:00
|
|
|
|
2013-03-29 00:42:32 +00:00
|
|
|
t.end();
|
2012-08-22 22:04:30 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
//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();
|
|
|
|
//});
|