/** * Logs basic information on a SOAP request. * * @param remoteCallReturn information on the SOAP call to be logged */ public void logRequest(RemoteCallReturn remoteCallReturn) { loggerDelegate.logRequestSummary(remoteCallReturn); loggerDelegate.logRequestDetails(remoteCallReturn); } }
/** * Constructor that takes an injected logger identified by name. * * @param reportLogger underlying SLF4J logger for report service interactions */ @Inject private ReportServiceLogger(@Named(AdWordsLoggingModule.REPORT_LOGGER_NAME) Logger reportLogger) { this(new RemoteCallLoggerDelegate(reportLogger, reportLogger, null, RemoteCallType.HTTP)); }
/** * Logs the specified request and response information. * * <p>Note that in order to avoid any temptation to consume the contents of the response, this * does <em>not</em> take an {@link com.google.api.client.http.HttpResponse} object, but instead * accepts the status code and message from the response. */ public void logRequest( @Nullable HttpRequest request, int statusCode, @Nullable String statusMessage) { boolean isSuccess = HttpStatusCodes.isSuccess(statusCode); if (!loggerDelegate.isSummaryLoggable(isSuccess) && !loggerDelegate.isDetailsLoggable(isSuccess)) { return; } // Populate the RequestInfo builder from the request. RequestInfo requestInfo = buildRequestInfo(request); // Populate the ResponseInfo builder from the response. ResponseInfo responseInfo = buildResponseInfo(request, statusCode, statusMessage); RemoteCallReturn.Builder remoteCallReturnBuilder = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfo); if (!isSuccess) { remoteCallReturnBuilder.withException( new ReportException(String.format("%s: %s", statusCode, statusMessage))); } RemoteCallReturn remoteCallReturn = remoteCallReturnBuilder.build(); loggerDelegate.logRequestSummary(remoteCallReturn); loggerDelegate.logRequestDetails(remoteCallReturn); }
@Test public void testTruncateThrowable_equalsMaxLength() { Throwable t = Mockito.mock(Throwable.class); String toString = "1234567890"; when(t.toString()).thenReturn(toString); assertEquals( "truncateThrowable should not truncate when length == maxLength", toString, // Use maxLength == length of toString loggerDelegate.truncateThrowable(t, 10)); }
if (!isSummaryLoggable(isSuccess)) { return; Throwable throwable = remoteCallReturn.getException(); logArgs[8] = throwable != null; logArgs[9] = truncateThrowable(throwable, MAX_SUMMARY_ERROR_LENGTH);
@Test public void testLogRequestDetails_success_infoLevelDisabled() { when(soapXmlLogger.isInfoEnabled()).thenReturn(false); when(soapXmlLogger.isWarnEnabled()).thenReturn(false); loggerDelegate.logRequestDetails(remoteCallReturn); verify(soapXmlLogger, times(0)).info(any(String.class)); verify(soapXmlLogger, times(0)).warn(any(String.class)); }
@Test public void testLogRequestSummary_success() { // Set up the request info mock. when(requestInfoLogger.isInfoEnabled()).thenReturn(true); loggerDelegate.logRequestSummary(remoteCallReturn); RequestInfo requestInfo = remoteCallReturn.getRequestInfo(); ResponseInfo responseInfo = remoteCallReturn.getResponseInfo(); verify(requestInfoLogger) .info( RemoteCallLoggerDelegate.SUMMARY_TEMPLATE, requestInfo.getServiceName(), requestInfo.getMethodName(), requestInfo.getContextName(), requestInfo.getContextValue(), requestInfo.getUrl(), responseInfo.getRequestId(), responseInfo.getResponseTime(), responseInfo.getOperationsCount(), false, null); }
public void logRequestDetails(RemoteCallReturn remoteCallReturn) { if (remoteCallReturn == null) { return; } boolean isSuccess = remoteCallReturn.getException() == null; if (!isDetailsLoggable(isSuccess)) { return; } String prettyRequest = null; if (remoteCallReturn.getRequestInfo() != null) { prettyRequest = prettyPrinter.prettyPrint(remoteCallReturn.getRequestInfo().getPayload()); } String prettyResponse = null; if (remoteCallReturn.getResponseInfo() != null) { prettyResponse = prettyPrinter.prettyPrint(remoteCallReturn.getResponseInfo().getPayload()); } if (isSuccess) { detailLogger.debug(DETAILS_REQUEST_TEMPLATE, remoteCallType, prettyRequest); detailLogger.debug(DETAILS_RESPONSE_TEMPLATE, remoteCallType, prettyResponse); } else { detailLogger.info(DETAILS_REQUEST_TEMPLATE, remoteCallType, prettyRequest); detailLogger.info(DETAILS_RESPONSE_TEMPLATE, remoteCallType, prettyResponse); } }
/** * Logs the specified request and response information. * * <p>Note that in order to avoid any temptation to consume the contents of the response, this * does <em>not</em> take an {@link com.google.api.client.http.HttpResponse} object, but instead * accepts the status code and message from the response. */ public void logRequest( @Nullable HttpRequest request, int statusCode, @Nullable String statusMessage) { boolean isSuccess = HttpStatusCodes.isSuccess(statusCode); if (!loggerDelegate.isSummaryLoggable(isSuccess) && !loggerDelegate.isDetailsLoggable(isSuccess)) { return; } // Populate the RequestInfo builder from the request. RequestInfo requestInfo = buildRequestInfo(request); // Populate the ResponseInfo builder from the response. ResponseInfo responseInfo = buildResponseInfo(request, statusCode, statusMessage); RemoteCallReturn.Builder remoteCallReturnBuilder = new RemoteCallReturn.Builder().withRequestInfo(requestInfo).withResponseInfo(responseInfo); if (!isSuccess) { remoteCallReturnBuilder.withException( new ReportException(String.format("%s: %s", statusCode, statusMessage))); } RemoteCallReturn remoteCallReturn = remoteCallReturnBuilder.build(); loggerDelegate.logRequestSummary(remoteCallReturn); loggerDelegate.logRequestDetails(remoteCallReturn); }
@Test public void testTruncateThrowable_lessThanMaxLength() { Throwable t = Mockito.mock(Throwable.class); String toString = "1234567890"; when(t.toString()).thenReturn(toString); assertEquals( "truncateThrowable should not truncate when length < maxLength", toString, // Use maxLength > length of toString loggerDelegate.truncateThrowable(t, 11)); }
if (!isSummaryLoggable(isSuccess)) { return; Throwable throwable = remoteCallReturn.getException(); logArgs[8] = throwable != null; logArgs[9] = truncateThrowable(throwable, MAX_SUMMARY_ERROR_LENGTH);
@Test public void testLogRequestDetails_success_debugLevelEnabled() { when(soapXmlLogger.isDebugEnabled()).thenReturn(true); loggerDelegate.logRequestDetails(remoteCallReturn); verify(soapXmlLogger) .debug( RemoteCallLoggerDelegate.DETAILS_REQUEST_TEMPLATE, RemoteCallType.SOAP, prettyRequest); verify(soapXmlLogger) .debug( RemoteCallLoggerDelegate.DETAILS_RESPONSE_TEMPLATE, RemoteCallType.SOAP, prettyResponse); }
@Test public void testLogRequestSummary_failure() { Throwable returnedException = new Throwable(); remoteCallReturn = new RemoteCallReturn.Builder() .withRequestInfo(remoteCallReturn.getRequestInfo()) .withResponseInfo(remoteCallReturn.getResponseInfo()) .withException(returnedException) .build(); when(requestInfoLogger.isWarnEnabled()).thenReturn(true); loggerDelegate.logRequestSummary(remoteCallReturn); RequestInfo requestInfo = remoteCallReturn.getRequestInfo(); ResponseInfo responseInfo = remoteCallReturn.getResponseInfo(); verify(requestInfoLogger) .warn( RemoteCallLoggerDelegate.SUMMARY_TEMPLATE, requestInfo.getServiceName(), requestInfo.getMethodName(), requestInfo.getContextName(), requestInfo.getContextValue(), requestInfo.getUrl(), responseInfo.getRequestId(), responseInfo.getResponseTime(), responseInfo.getOperationsCount(), true, returnedException.toString()); }
public void logRequestDetails(RemoteCallReturn remoteCallReturn) { if (remoteCallReturn == null) { return; } boolean isSuccess = remoteCallReturn.getException() == null; if (!isDetailsLoggable(isSuccess)) { return; } String prettyRequest = null; if (remoteCallReturn.getRequestInfo() != null) { prettyRequest = prettyPrinter.prettyPrint(remoteCallReturn.getRequestInfo().getPayload()); } String prettyResponse = null; if (remoteCallReturn.getResponseInfo() != null) { prettyResponse = prettyPrinter.prettyPrint(remoteCallReturn.getResponseInfo().getPayload()); } if (isSuccess) { detailLogger.debug(DETAILS_REQUEST_TEMPLATE, remoteCallType, prettyRequest); detailLogger.debug(DETAILS_RESPONSE_TEMPLATE, remoteCallType, prettyResponse); } else { detailLogger.info(DETAILS_REQUEST_TEMPLATE, remoteCallType, prettyRequest); detailLogger.info(DETAILS_RESPONSE_TEMPLATE, remoteCallType, prettyResponse); } }
/** * Logs basic information on a SOAP request. * * @param remoteCallReturn information on the SOAP call to be logged */ public void logRequest(RemoteCallReturn remoteCallReturn) { loggerDelegate.logRequestSummary(remoteCallReturn); loggerDelegate.logRequestDetails(remoteCallReturn); } }
@Test public void testTruncateThrowable_trimsNewLines() { Throwable t = Mockito.mock(Throwable.class); String toString = String.format("lineA%nlineB%nlineC%n"); when(t.toString()).thenReturn(toString); assertEquals( "truncateThrowable should remove all new lines", "lineAlineBlineC", loggerDelegate.truncateThrowable(t, 15)); }
@Test public void testLogRequestDetails_failure_infoLevelEnabled() { Throwable returnedException = new Throwable(); remoteCallReturn = new RemoteCallReturn.Builder() .withRequestInfo(remoteCallReturn.getRequestInfo()) .withResponseInfo(remoteCallReturn.getResponseInfo()) .withException(returnedException) .build(); when(soapXmlLogger.isInfoEnabled()).thenReturn(true); when(soapXmlLogger.isWarnEnabled()).thenReturn(true); loggerDelegate.logRequestDetails(remoteCallReturn); verify(soapXmlLogger) .info( RemoteCallLoggerDelegate.DETAILS_REQUEST_TEMPLATE, RemoteCallType.SOAP, prettyRequest); verify(soapXmlLogger) .info( RemoteCallLoggerDelegate.DETAILS_RESPONSE_TEMPLATE, RemoteCallType.SOAP, prettyResponse); }
/** * Constructor that takes an injected logger identified by name. * * @param batchJobLogger underlying SLF4J logger for batch job uploads/downloads */ @Inject BatchJobLogger(@Named(AdWordsLoggingModule.BATCH_JOB_LOGGER_NAME) Logger batchJobLogger) { this(new RemoteCallLoggerDelegate(batchJobLogger, batchJobLogger, null, RemoteCallType.HTTP)); }
@Test public void testLogRequest() { adsServiceLoggers.logRequest(remoteCallReturn); verify(loggerDelegate).logRequestSummary(remoteCallReturn); verify(loggerDelegate).logRequestDetails(remoteCallReturn); } }
@Test public void testTruncateThrowable_ignoresNewLinesWhenTrimming() { Throwable t = Mockito.mock(Throwable.class); String toString = String.format("123%n456%n789%n0"); when(t.toString()).thenReturn(toString); assertEquals( "truncateThrowable should ignore new lines when trimming", "123456...", loggerDelegate.truncateThrowable(t, 9)); } }