Comparing version 0.15.1 to 0.16.0
@@ -48,2 +48,17 @@ /** | ||
}); | ||
if (config.customLog) { | ||
var customLogPair = []; | ||
var customLogKeys = Object.keys(config.customLog); | ||
for (var i = 0; i < customLogKeys.length; i++) { | ||
var key = customLogKeys[i]; | ||
if (config.customLog[key] && typeof config.customLog[key] === 'string') { | ||
var param = config.customLog[key].split('.'); | ||
customLogPair.push({ | ||
key: key, | ||
param: param | ||
}); | ||
} | ||
} | ||
config.customLog = customLogPair; | ||
} | ||
return config; | ||
@@ -50,0 +65,0 @@ }; |
@@ -16,2 +16,3 @@ /** | ||
var clusterID = (cluster.isWorker ? cluster.worker.id : 'main'); | ||
var _ = require('underscore'); | ||
@@ -21,3 +22,4 @@ var options = { | ||
log_path: path.join(__dirname, '../logs'), | ||
app: 'yog-ral' | ||
app: 'yog-ral', | ||
logInstance: logger.getLogger.bind(logger) | ||
}; | ||
@@ -33,3 +35,3 @@ | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).notice('[yog-ral] ' + prefix + content); | ||
options.logInstance(options).notice('[yog-ral] ' + prefix + content); | ||
}, | ||
@@ -41,3 +43,3 @@ warning: function (msg) { | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).warning('[yog-ral] ' + prefix + content); | ||
options.logInstance(options).warning('[yog-ral] ' + prefix + content); | ||
}, | ||
@@ -50,3 +52,3 @@ | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).fatal('[yog-ral] ' + prefix + content); | ||
options.logInstance(options).fatal('[yog-ral] ' + prefix + content); | ||
}, | ||
@@ -59,3 +61,3 @@ | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).trace('[yog-ral] ' + prefix + content); | ||
options.logInstance(options).trace('[yog-ral] ' + prefix + content); | ||
}, | ||
@@ -68,3 +70,3 @@ | ||
var content = Array.prototype.slice.call(arguments, 0).join(''); | ||
logger.getLogger(options).debug('[yog-ral] ' + prefix + content); | ||
options.logInstance(options).debug('[yog-ral] ' + prefix + content); | ||
} | ||
@@ -75,3 +77,3 @@ }; | ||
module.exports.__defineSetter__('options', function (opts) { | ||
options = opts; | ||
options = _.extend(options, opts); | ||
}); |
@@ -168,2 +168,3 @@ /** | ||
} | ||
me.responseData = data; | ||
logger.notice('request end ' + ralUtil.qs(me.getLogInfo())); | ||
@@ -323,5 +324,4 @@ } | ||
RalRunner.prototype.getLogInfo = function () { | ||
return { | ||
var defaultLog = { | ||
service: this.serviceName, | ||
@@ -344,2 +344,23 @@ requestID: this._requestID, | ||
}; | ||
// resolve custom loginfo | ||
if (this.conf.customLog) { | ||
var data = { | ||
requestContext: this.conf, | ||
responseContext: { | ||
extras: this.extras, | ||
body: this.responseData | ||
} | ||
}; | ||
for (var i = 0; i < this.conf.customLog.length; i++) { | ||
var key = this.conf.customLog[i].key; | ||
var param = this.conf.customLog[i].param; | ||
defaultLog[key] = param.reduce(function (data, path) { | ||
if (data && typeof data === 'object') { | ||
return data[path]; | ||
} | ||
return undefined; | ||
}, data); | ||
} | ||
} | ||
return defaultLog; | ||
}; | ||
@@ -346,0 +367,0 @@ |
{ | ||
"name": "node-ral", | ||
"version": "0.15.1", | ||
"version": "0.16.0", | ||
"description": "a rpc client for node", | ||
@@ -8,4 +8,4 @@ "main": "index.js", | ||
"pub": "npm publish --registry=http://registry.npmjs.org", | ||
"test": "mocha -t 200000 --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot -t 200000 --check-leaks test/", | ||
"test": "mocha -t 5000 --check-leaks test/", | ||
"test-cov": "istanbul cover node_modules/mocha/bin/_mocha -- --reporter dot -t 5000 --check-leaks test/", | ||
"test-travis": "istanbul cover node_modules/mocha/bin/_mocha --report lcovonly -- --reporter spec --check-leaks -t 200000 --slow 0 test/" | ||
@@ -12,0 +12,0 @@ }, |
@@ -193,2 +193,15 @@ /** | ||
it('parse customLog', function () { | ||
config.clearConf(); | ||
config.load(path.join(__dirname, './config/customLogConfig')); | ||
config.getConf('CUSTOM_LOG').customLog.length.should.equal(3); | ||
config.getConf('CUSTOM_LOG').customLog[0].key.should.equal('tracecode'); | ||
config.getConf('CUSTOM_LOG').customLog[1].key.should.equal('logid'); | ||
config.getConf('CUSTOM_LOG').customLog[0].param.length.should.equal(4); | ||
config.getConf('CUSTOM_LOG').customLog[0].param[0].should.equal("responseContext"); | ||
config.getConf('CUSTOM_LOG').customLog[0].param[1].should.equal("extras"); | ||
config.getConf('CUSTOM_LOG').customLog[0].param[2].should.equal("headers"); | ||
config.getConf('CUSTOM_LOG').customLog[0].param[3].should.equal("tracecode"); | ||
}); | ||
}); |
@@ -751,2 +751,50 @@ /** | ||
}); | ||
it('log custom log', function (done) { | ||
before(function (ok) { | ||
isInited.on('done', ok); | ||
}); | ||
ral.init({ | ||
confDir: path.join(__dirname, './config/customLogConfig'), | ||
logger: { | ||
log_path: path.join(__dirname, '../logs'), | ||
app: 'yog-ral', | ||
logInstance: function () { | ||
return { | ||
notice: function (msg) { | ||
if (msg.match(/tracecode=1/) && msg.match(/logid=123/) && msg.match(/none=undefined/)) { | ||
done(); | ||
} | ||
}, | ||
warning: function (msg) { | ||
}, | ||
trace: function (msg) { | ||
}, | ||
fatal: function (msg) { | ||
}, | ||
debug: function (msg) { | ||
} | ||
} | ||
} | ||
}, | ||
currentIDC: 'tc' | ||
}); | ||
var server = require('./mock/customLogServer.js').createCustomLogServier(8399); | ||
ral('CUSTOM_LOG', { | ||
headers: { | ||
x_bd_logid: '123' | ||
} | ||
}).on('data', function (data) { | ||
data.should.be.ok | ||
server.close(); | ||
}).on('error', function (err) { | ||
server.close() | ||
err.should.be.none; | ||
}); | ||
}); | ||
}); |
371710
101
10861
13