updated loggers to write the actual url

This commit is contained in:
Spencer Alger
2014-01-28 16:41:55 -07:00
parent a8e9645183
commit 513486f372
35 changed files with 211 additions and 147 deletions

16
test/unit/browser.js Normal file
View File

@ -0,0 +1,16 @@
var exclude = [
'file_logger.js',
'http_connector.js',
'stdio_logger.js',
'console_logger.js',
'stream_logger.js',
'tracer_logger.js',
'transport_with_server.js'
];
var _ = require('lodash');
var specDir = __dirname + '/specs';
_(require('fs').readdirSync(specDir)).difference(exclude).each(function (file) {
require(specDir + '/' + file);
});

View File

@ -0,0 +1,3 @@
require('./angular');
require('./generic');
require('./jquery');

View File

@ -230,22 +230,8 @@ module.exports = function (makeLogger) {
stub(logger, 'write', function (label, msg) {
expect(label).to.eql('TRACE');
});
logger.onTrace('message');
logger.onTrace(Log.normalizeTraceArgs('GET', 'http://place/thing?me=true', '{}', '{"ok": true}', 200));
expect(logger.write.callCount).to.eql(1);
});
it('joins the message and curl call with a newline', function () {
var logger = makeLogger();
// tracer logger has custom trace logic...
if (!(logger instanceof TracerLogger)) {
stub(logger, 'write', function (label, msg) {
expect(msg).to.eql('curlcall\nmessage');
});
logger.onTrace('message', 'curlcall');
expect(logger.write.callCount).to.eql(1);
}
});
});
};

View File

@ -1,3 +1,5 @@
require('fs').readdirSync(__dirname + '/tests').forEach(function (file) {
require(__dirname + '/tests/' + file);
var specDir = __dirname + '/specs';
require('fs').readdirSync(specDir).forEach(function (file) {
require(specDir + '/' + file);
});

View File

@ -182,12 +182,13 @@ describe('Log class', function () {
expect(call.args[0]).to.eql('here');
});
it('should emit a trace event for trace events, with message and curlCall args', function () {
it('should emit a trace event for trace events, with normalized request details arg', function () {
log.trace('GET', 'http://localhost:9200/_cluster/nodes', '', '', 200);
expect(call.event).to.eql('trace');
expect(call.args.length).to.be(2);
expect(call.args[0]).to.match(/^<- 200/);
expect(call.args[1]).to.match(/^curl /);
expect(call.args.length).to.be(1);
expect(call.args[0]).to.have.property('method', 'GET');
expect(call.args[0]).to.have.property('url', 'http://localhost:9200/_cluster/nodes');
expect(call.args[0]).to.have.property('status', 200);
});
});

View File

@ -57,10 +57,10 @@ describe('Stdio Logger', function () {
var logger = makeLogger();
stub(process.stdout, 'write');
var withoutColor = 'Elasticsearch TRACE: ' + now + '\n curl\n msg\n\n';
var withoutColor = 'Elasticsearch DEBUG: ' + now + '\n be weary\n\n';
logger.color = true;
logger.onTrace('msg', 'curl');
logger.onDebug('be weary');
expect(process.stdout.write.lastCall.args[0]).to.not.eql(withoutColor);
expect(chalk.stripColor(process.stdout.write.lastCall.args[0])).to.eql(withoutColor);
});

View File

@ -0,0 +1,74 @@
describe('Tracer Logger', function () {
var Log = require('../../../src/lib/log');
var TracerLogger = require('../../../src/lib/loggers/tracer');
var sinon = require('sinon');
var expect = require('expect.js');
var parentLog;
beforeEach(function () {
parentLog = new Log();
});
afterEach(function () {
parentLog.close();
});
function makeLogger(parent, levels) {
parent = parent || parentLog;
var config = {
levels: Log.parseLevels(levels || 'trace')
};
return new TracerLogger(parent, config);
}
var stub = require('../../utils/auto_release_stub').make();
// require('../generic_logger_tests')(makeLogger);
describe('#formatTraceMessage', function () {
it('includes the original host', function () {
var logger = makeLogger();
var formatted = logger._formatTraceMessage({
method: 'DELETE',
url: 'https://originalHost.com:9522/path/to/thing?qs=100',
body: '{ "yes": true }',
status: 333,
response: '{ "ok": false }'
});
expect(formatted.curl).to.match(/-XDELETE/);
expect(formatted.curl).to.match(/https:\/\/originalHost\.com:9522\/?\s+/i);
expect(formatted.curl).to.match(/https:\/\/localhost:9200\/path\/to\/thing\?(pretty=true|qs=100|&){3}/);
});
});
describe('#write', function () {
var logger;
beforeEach(function () {
logger = makeLogger();
stub(logger.stream, 'write');
});
it('detects tracer logs by listening messages wrapped in objects', function () {
logger.write('TRACE', { msg: 'msgtext', curl: 'curlcall' });
expect(logger.stream.write.callCount).to.be(1);
expect(logger.stream.write.lastCall.args[0]).to.contain('msgtext');
expect(logger.stream.write.lastCall.args[0]).to.contain('curlcall');
});
it('comments everthing except the curl call', function () {
logger.write('TRACE', { msg: 'comment me', curl: 'no comment' });
expect(logger.stream.write.callCount).to.be(1);
expect(logger.stream.write.lastCall.args[0]).to.match(/^# +comment me/m);
expect(logger.stream.write.lastCall.args[0]).to.match(/^no comment/m);
});
it('just comments when it gets a string', function () {
logger.write('TRACE', 'log me');
expect(logger.stream.write.callCount).to.be(1);
expect(logger.stream.write.lastCall.args[0]).to.match(/^# +log me/m);
});
});
});

View File

@ -508,14 +508,15 @@ describe('Transport Class', function () {
it('promise is always pulled from the defer created by this.defer()', function () {
var fakePromise = {};
var origDefer = Transport.prototype.defer;
Transport.prototype.defer = function () {
return {
resolve: _.noop,
reject: _.noop,
promise: fakePromise
};
};
var tran = new Transport({});
var tran = new Transport({
defer: function () {
return {
resolve: _.noop,
reject: _.noop,
promise: fakePromise
};
}
});
shortCircuitRequest(tran);
var ret = tran.request({});
Transport.prototype.defer = origDefer;

View File

@ -1,43 +0,0 @@
describe('Tracer Logger', function () {
var Log = require('../../../src/lib/log');
var TracerLogger = require('../../../src/lib/loggers/tracer');
var sinon = require('sinon');
var expect = require('expect.js');
var parentLog;
beforeEach(function () {
parentLog = new Log();
});
afterEach(function () {
parentLog.close();
});
function makeLogger(parent, levels) {
parent = parent || parentLog;
var config = {
levels: Log.parseLevels(levels || 'trace')
};
return new TracerLogger(parent, config);
}
var stub = require('../../utils/auto_release_stub').make();
require('../generic_logger_tests')(makeLogger);
describe('#write', function () {
it('comments out everything accept the curlCall', function () {
var logger = makeLogger();
stub(logger.stream, 'write', function (string) {
expect(string.replace(/(^#.*$|^curlcall$)/mg, '').trim()).to.be('');
});
logger.onTrace('message', 'curlcall');
expect(logger.stream.write.callCount).to.eql(1);
});
});
});