Skip to content

Commit

Permalink
feat(core): Improved logging (#217)
Browse files Browse the repository at this point in the history
  • Loading branch information
offirgolan authored Jun 23, 2019
1 parent 8ca5b6e commit 3e876a8
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions packages/@pollyjs/core/src/-private/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ export default class Logger {
connect() {
this._middleware = this.polly.server
.any()
.on('error', (...args) => this.logError(...args))
.on('response', (...args) => this.logRequest(...args));
}

disconnect() {
this.groupEnd();
this._middleware.off('error');
this._middleware.off('response');
}

Expand All @@ -42,19 +44,40 @@ export default class Logger {

groupEnd() {
if (this.groupName) {
console.groupEnd(this.groupName);
console.groupEnd();
}
}

logRequest(request) {
if (request.config.logging) {
this.groupStart(request.recordingName);
console.log(

console.groupCollapsed(
`${FORMATTED_ACTIONS[request.action]}${request.method} ${
request.url
} ${request.response.statusCode}${request.responseTime}ms`,
request
} ${request.response.statusCode}${request.responseTime}ms`
);
console.log('Request:', request);
console.log('Response:', request.response);
console.log('Identifiers:', request.identifiers);
console.groupEnd();
}
}

logError(request, error) {
if (request.config.logging) {
this.groupStart(request.recordingName);

console.groupCollapsed(`Errored ➞ ${request.method} ${request.url}`);
console.error(error);
console.log('Request:', request);

if (request.didRespond) {
console.log('Response:', request.response);
}

console.log('Identifiers:', request.identifiers);
console.groupEnd();
}
}
}

0 comments on commit 3e876a8

Please sign in to comment.