Search in sources :

Example 1 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class AbstractHttpclientRequestSender method logRequest.

public <T extends HttpRequestBase> void logRequest(int retryCount, T httpReq) {
    LogConfiguration logConfiguration = request.getLogConfiguration();
    if (!logConfiguration.isLogEnabled() || !logConfiguration.isLogRequest()) {
        return;
    }
    RequestLogMessage logMessage = getRequestLogMessage(retryCount, httpReq);
    logMessage.setRequest(request);
    request.setRequestLogMessage(logMessage);
    ForestLogHandler logHandler = request.getLogConfiguration().getLogHandler();
    if (logHandler != null) {
        logHandler.logRequest(logMessage);
    }
}
Also used : RequestLogMessage(com.dtflys.forest.logging.RequestLogMessage) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 2 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class SyncHttpclientRequestSender method logResponse.

public void logResponse(ForestResponse response) {
    LogConfiguration logConfiguration = request.getLogConfiguration();
    if (!logConfiguration.isLogEnabled() || response.isLogged()) {
        return;
    }
    response.setLogged(true);
    ResponseLogMessage logMessage = new ResponseLogMessage(response, response.getStatusCode());
    ForestLogHandler logHandler = logConfiguration.getLogHandler();
    if (logHandler != null) {
        if (logConfiguration.isLogResponseStatus()) {
            logHandler.logResponseStatus(logMessage);
        }
        if (logConfiguration.isLogResponseContent()) {
            logHandler.logResponseContent(logMessage);
        }
    }
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ResponseLogMessage(com.dtflys.forest.logging.ResponseLogMessage)

Example 3 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class TestPostClient method testPostJsonObjectWithLog_content_noStatus2.

@Test
public void testPostJsonObjectWithLog_content_noStatus2() {
    server.enqueue(new MockResponse().setBody(EXPECTED));
    ForestLogHandler logHandler = configuration.getLogHandler();
    ForestLogger logger = Mockito.mock(ForestLogger.class);
    logHandler.setLogger(logger);
    JsonTestUser2 user = new JsonTestUser2();
    user.setUsername("foo");
    assertThat(postClient.postJsonObjectWithLog_content_noStatus(user)).isNotNull().isEqualTo(EXPECTED);
    Mockito.verify(logger).info("[Forest] Response Content:\n\t" + EXPECTED);
}
Also used : MockResponse(okhttp3.mockwebserver.MockResponse) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestLogger(com.dtflys.forest.logging.ForestLogger) JsonTestUser2(com.dtflys.test.http.model.JsonTestUser2) Test(org.junit.Test)

Example 4 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class BaseLogHandlerLifeCycle method onProxyHandlerInitialized.

@Override
public void onProxyHandlerInitialized(InterfaceProxyHandler interfaceProxyHandler, LogHandler annotation) {
    ForestConfiguration configuration = interfaceProxyHandler.getConfiguration();
    LogConfiguration logConfiguration = interfaceProxyHandler.getBaseLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        logConfiguration.setLogEnabled(configuration.isLogEnabled());
        logConfiguration.setLogRequest(configuration.isLogRequest());
        logConfiguration.setLogResponseStatus(configuration.isLogResponseStatus());
        logConfiguration.setLogResponseContent(configuration.isLogResponseContent());
        interfaceProxyHandler.setBaseLogConfiguration(logConfiguration);
    }
    Class<? extends ForestLogHandler> logHandlerClass = annotation.value();
    ForestLogHandler logHandler = null;
    try {
        logHandler = logHandlerClass.newInstance();
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    }
    if (logHandler != null) {
        logConfiguration.setLogHandler(logHandler);
    } else {
        logConfiguration.setLogHandler(configuration.getLogHandler());
    }
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 5 with ForestLogHandler

use of com.dtflys.forest.logging.ForestLogHandler in project forest by dromara.

the class DownloadLifeCycle method onSuccess.

@Override
public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
    String dirPath = getAttributeAsString(request, "dir");
    String filename = getAttributeAsString(request, "filename");
    Type resultType = getAttribute(request, "__resultType", Type.class);
    if (StringUtils.isBlank(filename)) {
        filename = response.getFilename();
    }
    LogConfiguration logConfiguration = request.getLogConfiguration();
    ForestLogHandler logHandler = logConfiguration.getLogHandler();
    File dir = new File(dirPath);
    if (!dir.exists()) {
        try {
            dir.mkdirs();
            if (logConfiguration.isLogEnabled()) {
                logHandler.logContent("Created directory '" + dirPath + "' successful.");
            }
        } catch (Throwable th) {
            throw new ForestRuntimeException(th);
        }
    }
    InputStream in = null;
    if (data != null && data instanceof byte[]) {
        in = new ByteArrayInputStream((byte[]) data);
    } else {
        try {
            in = response.getInputStream();
        } catch (Exception e) {
            throw new ForestRuntimeException(e);
        }
    }
    String path = dir.getAbsolutePath() + File.separator + filename;
    File file = new File(path);
    try {
        FileUtils.copyInputStreamToFile(in, file);
        FileUtils.waitFor(file, 10);
        if (logConfiguration.isLogEnabled() || !file.exists()) {
            logHandler.logContent("Saved file '" + path + "' successful.");
        }
        request.addAttachment(ATTACHMENT_NAME_FILE, file);
        if (resultType != null) {
            ForestConverter converter = request.getConfiguration().getConverterMap().get(ForestDataType.AUTO);
            data = converter.convertToJavaObject(file, resultType);
            response.setResult(data);
        }
    } catch (IOException e) {
        throw new ForestRuntimeException(e);
    }
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Type(java.lang.reflect.Type) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestConverter(com.dtflys.forest.converter.ForestConverter) DownloadFile(com.dtflys.forest.extensions.DownloadFile)

Aggregations

ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)9 LogConfiguration (com.dtflys.forest.logging.LogConfiguration)6 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)5 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)3 ResponseLogMessage (com.dtflys.forest.logging.ResponseLogMessage)2 Map (java.util.Map)2 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)2 ManagedMap (org.springframework.beans.factory.support.ManagedMap)2 ForestConverter (com.dtflys.forest.converter.ForestConverter)1 DownloadFile (com.dtflys.forest.extensions.DownloadFile)1 ForestLogger (com.dtflys.forest.logging.ForestLogger)1 RequestLogMessage (com.dtflys.forest.logging.RequestLogMessage)1 MetaRequest (com.dtflys.forest.reflection.MetaRequest)1 ForestConvertProperties (com.dtflys.forest.springboot.properties.ForestConvertProperties)1 ForestSSLKeyStoreProperties (com.dtflys.forest.springboot.properties.ForestSSLKeyStoreProperties)1 ForestDataType (com.dtflys.forest.utils.ForestDataType)1 JsonTestUser2 (com.dtflys.test.http.model.JsonTestUser2)1 Method (java.lang.reflect.Method)1 Type (java.lang.reflect.Type)1 Collections (java.util.Collections)1