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);
}
}
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);
}
}
}
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);
}
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());
}
}
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);
}
}
Aggregations