Skip to content

Commit

Permalink
fix: logging
Browse files Browse the repository at this point in the history
  • Loading branch information
zetxx committed Nov 19, 2019
1 parent 5565b57 commit 2f1cad8
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions lib/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ class Brid {
}

async start() {
this.log('debug', {in: 'base.start'});
this.log('info', {in: 'base.start'});
}

async stop() {
this.log('debug', {in: 'base.stop'});
this.log('info', {in: 'base.stop'});
}

getApiRequestId() {
Expand All @@ -28,7 +28,7 @@ class Brid {
}

findMethod({method, channel, direction = 'out'} = {}) {
this.log('debug', {in: 'base.findMethod', description: `try find method ${channel}.${method}.${direction || ''}`, method, channel, direction});
this.log('info', {in: 'base.findMethod', description: `try find method ${channel}.${method}.${direction || ''}`, method, channel, direction});
let found = ((direction && [`${method}.${direction}`, direction, method]) || [method]).find((v) => this.apiMethods[channel] && this.apiMethods[channel][v]);

if (!found) {
Expand All @@ -48,7 +48,7 @@ class Brid {
}

registerMethod({method, channel, fn, meta}) {
this.log('debug', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta});
this.log('info', {in: 'base.registerMethod', description: `register method: ${channel}.${method}`, method, channel, meta});
!this.apiMethods[channel] && (this.apiMethods[channel] = {});
this.apiMethods[channel][method] = async(ctx, ...args) => {
let [message, meta] = args;
Expand All @@ -58,12 +58,12 @@ class Brid {
}

registerApiMethod({method, fn, meta = {}}) {
this.log('debug', {in: 'base.registerApiMethod', method, meta});
this.log('info', {in: 'base.registerApiMethod', method, meta});
this.registerMethod({method, fn, channel: 'api', meta});
}

registerExternalMethod({method, fn, meta = {}}) {
this.log('debug', {in: 'base.registerExternalMethod', method, meta});
this.log('info', {in: 'base.registerExternalMethod', method, meta});
this.registerMethod({method, fn, channel: 'external', meta});
}

Expand Down Expand Up @@ -143,7 +143,7 @@ class Brid {

// when someone hits api method
apiRequestReceived({message, meta}) {
this.log('debug', {in: 'base.apiRequestReceived', count: 1, message, meta});
this.log('info', {in: 'base.apiRequestReceived', count: 1, message, meta});
return new Promise(async(resolve, reject) => {
// create meta
const m = Object.assign({}, meta, {resolve, reject, apiRequestId: this.getApiRequestId()});
Expand Down Expand Up @@ -194,7 +194,7 @@ class Brid {
// when external response received, response to previously api request, its is called when response from external are matched
async apiResponseReceived({result, error, meta: {apiRequestId = -1}} = {}) {
const message = (result && {result}) || (error && {error}) || undefined;
this.log('debug', {in: 'base.apiResponseReceived', result, error, apiRequestId});
this.log('info', {in: 'base.apiResponseReceived', result, error, apiRequestId});
if (this.isApiRequest(apiRequestId) >= 0) { // request exists
var {meta} = this.findApiRequest(apiRequestId); // find request
if (meta.timeoutId) { // clear timeout if any
Expand All @@ -208,7 +208,7 @@ class Brid {
try {
let fn = this.findApiMethod({method: meta.method, direction: 'out'}); // find api method
let fnResult = await fn(this.getInternalCommunicationContext(meta), message, Object.assign({}, meta)); // execute api method
this.log('debug', {in: 'base.apiResponseReceived', description: 'resolve request', result, error, apiRequestId});
this.log('info', {in: 'base.apiResponseReceived', description: 'resolve request', result, error, apiRequestId});
return meta.resolve(fnResult);
} catch (e) {
this.log('error', {in: 'base.apiResponseReceived', result, error, apiRequestId});
Expand All @@ -219,7 +219,7 @@ class Brid {
}
// when someone hits external
async externalIn({result, error, meta = {}} = {}) {
this.log('debug', {in: 'base.externalIn', result, error, meta});
this.log('info', {in: 'base.externalIn', result, error, meta});
var {method, apiRequestId, isNotification} = meta;
const message = (result && {result}) || (error && {error}) || undefined;
// find if there is api request pending by id
Expand Down Expand Up @@ -255,10 +255,10 @@ class Brid {
let fnExt = this.findExternalMethod({method});
let transformedMsg = await fnExt(this.getInternalCommunicationContext(meta), message, Object.assign({}, meta));
if (transformedMsg) { // respond to external if result from transform function is not false.
this.log('debug', {in: 'base.externalIn', description: 'external request is NOT response of api request > send response to external', result, error, meta});
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > send response to external', result, error, meta});
return this.externalOut({result: transformedMsg, meta});
}
this.log('debug', {in: 'base.externalIn', description: 'external request is NOT response of api request > don\'t send response to external because user transform function returned false', result, error, meta, transformedMsg});
this.log('info', {in: 'base.externalIn', description: 'external request is NOT response of api request > don\'t send response to external because user transform function returned false', result, error, meta, transformedMsg});
return {...message, meta: Object.assign({deadIn: 1}, meta)};
} catch (e) {
this.log('error', {in: 'base.externalIn', result, error, meta});
Expand Down

0 comments on commit 2f1cad8

Please sign in to comment.