Skip to content

Commit

Permalink
Merge pull request #38 from emarsys/issue/37
Browse files Browse the repository at this point in the history
feat(log): switch logger framework
  • Loading branch information
sonicoder86 committed Mar 7, 2018
2 parents 936d133 + c659769 commit e59184d
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 46 deletions.
4 changes: 2 additions & 2 deletions lib/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Escher = require('escher-auth');
const Options = require('./requestOption');
const Wrapper = require('./wrapper');
const SuiteRequestError = require('./requestError');
const logger = require('logentries-logformat')('suiterequest');
const logger = require('@emartech/json-logger')('suiterequest');

class SuiteRequest {

Expand Down Expand Up @@ -44,7 +44,7 @@ class SuiteRequest {
const payload = data ? this._getPayload(data) : '';
const signedOptions = this._signRequest(options, payload);

logger.log('send', this._getLogParameters(options));
logger.info('send', this._getLogParameters(options));
return this._getRequestFor(signedOptions, payload).send();
}

Expand Down
27 changes: 15 additions & 12 deletions lib/wrapper.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
'use strict';

const SuiteRequestError = require('./requestError');
const logger = require('logentries-logformat')('suiterequest');
const debugLogger = require('logentries-logformat')('suiterequest-debug');
const logger = require('@emartech/json-logger')('suiterequest');
const debugLogger = require('@emartech/json-logger')('suiterequest-debug');
const axios = require('axios');

class RequestWrapper {
Expand All @@ -11,9 +11,11 @@ class RequestWrapper {
this.requestOptions = requestOptions;
this.protocol = protocol;
this.payload = payload;
debugLogger.log('request_options', requestOptions);
debugLogger.log('protocol', protocol);
debugLogger.log('payload', payload);
debugLogger.info('request_options', {
request_options: requestOptions,
protocol: protocol,
payload: payload
});
}

send() {
Expand Down Expand Up @@ -41,7 +43,7 @@ class RequestWrapper {
)
.then(response => {
const endTime = this._getTime();
logger.success('send', this._getLogParameters({ time: startTime - endTime }));
logger.info('send', this._getLogParameters({ time: startTime - endTime }));

return this._handleResponse(response);
});
Expand All @@ -57,14 +59,15 @@ class RequestWrapper {
}

_handleResponseError(error) {
logger.error('fatal_error', error.message, this._getLogParameters());
logger.fromError('fatal_error', error, this._getLogParameters());
throw new SuiteRequestError(error.message, 500);
}

_handleResponse(response) {
if (response.statusCode >= 400) {
logger.error('server_error', response.body.replyText, this._getLogParameters({
code: response.statusCode
logger.error('server_error',this._getLogParameters({
code: response.statusCode,
reply_text: response.body.replyText
}));
throw new SuiteRequestError(
'Error in http response (status: ' + response.statusCode + ')',
Expand All @@ -74,7 +77,7 @@ class RequestWrapper {
}

if (!this.requestOptions.allowEmptyResponse && !response.body) {
logger.error('server_error', 'empty response data', this._getLogParameters());
logger.error('server_error empty response data', this._getLogParameters());
throw new SuiteRequestError('Empty http response', 500, response.statusMessage);
}

Expand Down Expand Up @@ -118,7 +121,7 @@ class RequestWrapper {
headers: headers,
timeout: this.requestOptions.timeout
};
debugLogger.log('wrapper_options', reqOptions);
debugLogger.info('wrapper_options', reqOptions);

if (this.payload) {
reqOptions.body = this.payload;
Expand All @@ -135,7 +138,7 @@ class RequestWrapper {
try {
return JSON.parse(response.body);
} catch (ex) {
logger.error('fatal_error', ex, this._getLogParameters());
logger.fromError('fatal_error', ex, this._getLogParameters());
throw new SuiteRequestError(ex.message, 500);
}
}
Expand Down
91 changes: 61 additions & 30 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
},
"homepage": "https://github.com/emartech/escher-suiteapi-js",
"dependencies": {
"@emartech/json-logger": "3.0.1",
"axios": "0.17.1",
"escher-auth": "1.0.0",
"logentries-logformat": "0.2.0"
"escher-auth": "1.0.0"
},
"devDependencies": {
"chai": "4.1.2",
Expand Down

0 comments on commit e59184d

Please sign in to comment.