Add support for logging HttpRequest and HttpResponse objects
Discussed in https://github.com/jongpie/NebulaLogger/discussions/239
Originally posted by guerrieroi October 26, 2021 I would like to know if there is a best practice to log status code and message, request and response body of Apex REST callout with Nebula Logger. I'm using logger.error(message) with a custom formatted message containing all above data about REST callout.
Thanks in advance.
Logger should provide the ability to log HttpRequest and HttpResponse objects. At a minimum, these should be added as new methods in LogEntryEventBuilder - each method should then map attributes like the response/request's status code (and other attributes) to new fields on LogEntryEvent__e and LogEntry__c.
Hi. I think I can help with that with code from my own logger. Since I don't have too much time I am not able to contribute directly with PR but feel free to use this code to format request and response.
/**
* @param request pass here HttpRequest type vriable.
* @param response pass here HttpResponse type variable.
* @return Parsed and formatted request and response.
*/
private static String formatHttpRequests(HttpRequest request, HttpResponse response) {
String responseStatus = '';
String httpRequestFormatted = '';
String httpResponseFormatted = '';
if (request != null) {
httpRequestFormatted += 'Endpoint: ' + request.getEndpoint() + ', Method: ' + request.getMethod();
httpRequestFormatted += '\r\nContent-Type: ' + request?.getHeader('Content-Type');
httpRequestFormatted += '\r\nRequest body: ' + request?.getBody();
} else {
httpRequestFormatted = '*Request argument was null*';
}
if (response != null) {
responseStatus = 'Status: ' + response.getStatusCode() + ' ' + response.getStatus();
httpResponseFormatted += responseStatus;
if (!response.getHeaderKeys().isEmpty()) {
httpResponseFormatted += '\r\n Response Headers: ';
for (String key: response.getHeaderKeys()) {
httpResponseFormatted += '\r\n' + key + ' = ' + response.getHeader(key);
}
}
if (response?.getBody()?.length() > 131072) { // fix maximum line width
httpResponseFormatted += '\r\nResponse body: ' + response?.getBody()?.substring(0, 131055);
} else {
httpResponseFormatted += '\r\nResponse body: ' + response?.getBody();
}
} else {
httpResponseFormatted = '*Response argument was null*';
}
return '### Request ###\r\n' + httpRequestFormatted + '\r\n### Response ###\r\n' + httpResponseFormatted;
}
Thanks @PawelWozniak for sharing your code snippet! I'm hoping to finally work on this issue next month, I'm hoping it will be fairly easy to implement this.
I'm marking this as closed - the ability to log HttpRequest and HttpResponse objects was added in v4.7.1 (PR #298) to the class LogEntryEventBuilder, but I kept this issue open because I had planned to also add overloads to Logger that accepted HttpRequest and HttpResponse parameters. However, at least for now, I don't think that's necessary, and it would really increase the number of loads needed in Logger, so I'm going to consider the builder methods to be good enough for now. If/when there is a need to add the overloads to Logger, a new issue can be created to track it.