Skip to content

Commit

Permalink
Lazy load hmpo-logger to not initialise too early
Browse files Browse the repository at this point in the history
  • Loading branch information
HughePaul committed Jul 28, 2022
1 parent 4992d08 commit 3ea03ef
Show file tree
Hide file tree
Showing 3 changed files with 4,459 additions and 908 deletions.
21 changes: 13 additions & 8 deletions lib/remote-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const ModelError = require('./model-error');

const DEFAULT_TIMEOUT = 60000;

const consoleLogger = { outbound: console.log, trimHtml: html => html };

class RemoteModel extends LocalModel {
constructor(attributes, options) {
super(attributes, options);
Expand All @@ -19,14 +21,17 @@ class RemoteModel extends LocalModel {

setLogger() {
try {
const hmpoLogger = require('hmpo-logger');
this.logger = hmpoLogger.get(':' + this.options.label);
this.logger = require('hmpo-logger');
} catch (e) {
console.error('Error setting logger, using console instead!', e);
this.logger = { outbound: console.log, trimHtml: html => html };
/* istanbul ignore next */
console.warn('Warning: ' + this.options.label + ': Unable to find hmpo-logger for logging, using console instead!');
}
}

getLogger() {
return this.logger ? this.logger.get(':' + this.options.label) : consoleLogger;
}

fetch(args, callback) {
if (typeof args === 'function') {
callback = args;
Expand Down Expand Up @@ -264,7 +269,7 @@ class RemoteModel extends LocalModel {
if (outError) data.outError = outError;

if (tokenData.err) {
data.outErrorBody = this.logger.trimHtml(tokenData.err.body);
data.outErrorBody = this.getLogger().trimHtml(tokenData.err.body);
}

Object.assign(data, this.options.logging);
Expand All @@ -274,15 +279,15 @@ class RemoteModel extends LocalModel {


logSync() {
this.logger.outbound('Model request sent :outVerb :outRequest', this.logMeta.apply(this, arguments));
this.getLogger().outbound('Model request sent :outVerb :outRequest', this.logMeta.apply(this, arguments));
}

logSuccess() {
this.logger.outbound('Model request success :outVerb :outRequest :outResponseCode', this.logMeta.apply(this, arguments));
this.getLogger().outbound('Model request success :outVerb :outRequest :outResponseCode', this.logMeta.apply(this, arguments));
}

logError() {
this.logger.outbound('Model request failed :outVerb :outRequest :outResponseCode :outError', this.logMeta.apply(this, arguments));
this.getLogger().outbound('Model request failed :outVerb :outRequest :outResponseCode :outError', this.logMeta.apply(this, arguments));
}

hookSync() {
Expand Down
Loading

0 comments on commit 3ea03ef

Please sign in to comment.