Commit d40e26df authored by Sergey Razuvaev's avatar Sergey Razuvaev

add logger

parent 4c368700
...@@ -5,10 +5,16 @@ var commands = require('./command'); ...@@ -5,10 +5,16 @@ var commands = require('./command');
//base context //base context
var Context = function (stream, debug) { var Context = function (stream, loggerOptions) {
EventEmitter.call(this); EventEmitter.call(this);
this.debug = debug; function consoleDecorator(data){
return console.log(JSON.stringify (data))
}
this.debug = loggerOptions.debug
this.log = loggerOptions.logger.debug || consoleDecorator;
this.stream = new Readable(); this.stream = new Readable();
this.stream.setEncoding('utf8'); this.stream.setEncoding('utf8');
...@@ -113,11 +119,11 @@ Context.prototype.end = function () { ...@@ -113,11 +119,11 @@ Context.prototype.end = function () {
}; };
Context.prototype.sendCommand = function (command) { Context.prototype.sendCommand = function (command) {
if (this.debug) console.log('command', command); if (this.debug) this.log({ command: 'command' });
var self = this; var self = this;
return new Promise(function (resolve, reject) { return new Promise(function (resolve, reject) {
self.send(command + '\n', function (err, result) { self.send(command + '\n', function (err, result) {
if (self.debug) console.log('err:', err, 'result:', result); if (self.debug) this.log({ err: err, result: result });
if (err) { if (err) {
reject(err); reject(err);
} else { } else {
......
...@@ -8,11 +8,12 @@ var agi = function (handler, optionsIn) { ...@@ -8,11 +8,12 @@ var agi = function (handler, optionsIn) {
var settings = { var settings = {
port: options.port || 3000, port: options.port || 3000,
debug: options.debug || false, debug: options.debug || false,
logger: options.logger || false,
host: options.host, host: options.host,
}; };
var handle = function (stream) { var handle = function (stream) {
var context = new Context(stream, settings.debug); var context = new Context(stream, { debug: settings.debug, logger: options.logger});
handler(context); handler(context);
}; };
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment