Skip to content

Commit

Permalink
fix: logger-not-working-in-plugins (#36231)
Browse files Browse the repository at this point in the history
Fixes #36073 

Hi @NilanshBansal 

**Issue :**

**Missing Logging Implementation :**

- Without a logging implementation (such as SLF4J Simple or Logback) in
the project's classpath, the logging statements in the plugins cannot be
executed.
- As a result, no log output is being printed to the terminal.

**Solution :**

The solution is to add a logging implementation to the plugins parent
pom. In this case, you can add the slf4j-simple dependency to your
pom.xml file. This will provide a simple logging implementation that
will output log statements to the console.

```
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.36</version> 
</dependency>
```
**Explanation:**

- slf4j-api provides the SLF4J API, which is the interface for logging.
  provides the SLF4J API, which is the interface for logging.
- slf4j-simple provides a simple implementation of the SLF4J API, which
is responsible for actually printing the log messages to the console.

**Screenshots :** 

Amazon S3 Plugin and Postgres Plugin 

![image](https://github.com/user-attachments/assets/d3e90b96-2b02-493d-8ffa-44e2aa348fc1)


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

## Summary by CodeRabbit

- **New Features**
- Enhanced logging capabilities with the integration of SLF4J API and
SLF4J Simple implementations.

- **Improvements**
- Improved log management and output formatting for better monitoring
and debugging across various plugins, transitioning from standard output
to structured logging.
- Refined logging practices in multiple plugins to support better
maintainability and performance.
- Removed method for console logging from the Stopwatch class to
streamline logging practices.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
AnnaHariprasad5123 authored Sep 17, 2024
1 parent 66c815f commit 7aae152
Show file tree
Hide file tree
Showing 27 changed files with 308 additions and 507 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,6 @@ public void stopAndLogTimeInMillis() {
log.debug("Execute time: {}, Time elapsed: {}ms", this.flow, this.watch.getTime(TimeUnit.MILLISECONDS));
}

/**
* This is a temporary function created to log stopwatch timer within the plugin package
* Due to this bug https://github.com/appsmithorg/appsmith/issues/36073 logging is not working in the plugin package
*/
public void stopAndLogTimeInMillisWithSysOut() {
if (!this.watch.isStopped()) {
this.watch.stop();
}
System.out.println(String.format(
"Execute time: %s, Time elapsed: %dms", this.flow, this.watch.getTime(TimeUnit.MILLISECONDS)));
}

public void stopTimer() {
if (!this.watch.isStopped()) {
this.watch.stop();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -416,9 +416,7 @@ public Mono<ActionExecutionResult> executeParameterized(
DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) {

String printMessage =
Thread.currentThread().getName() + ": executeParameterized() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": executeParameterized() called for AmazonS3 plugin.");
final Map<String, Object> formData = actionConfiguration.getFormData();
List<Map.Entry<String, String>> parameters = new ArrayList<>();

Expand Down Expand Up @@ -467,8 +465,7 @@ private Mono<ActionExecutionResult> executeCommon(
DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) {

String printMessage = Thread.currentThread().getName() + ": executeCommon() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": executeCommon() called for AmazonS3 plugin.");
final String[] query = new String[1];
Map<String, Object> requestProperties = new HashMap<>();
List<RequestParamDTO> requestParams = new ArrayList<>();
Expand Down Expand Up @@ -544,7 +541,7 @@ private Mono<ActionExecutionResult> executeCommon(
Object actionResult;
switch (s3Action) {
case LIST:
System.out.println(
log.debug(
Thread.currentThread().getName() + ": LIST action called for AmazonS3 plugin.");
String prefix = getDataValueSafelyFromFormData(formData, LIST_PREFIX, STRING_TYPE, "");
requestParams.add(new RequestParamDTO(LIST_PREFIX, prefix, null, null, null));
Expand Down Expand Up @@ -644,7 +641,7 @@ private Mono<ActionExecutionResult> executeCommon(

break;
case UPLOAD_FILE_FROM_BODY: {
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": UPLOAD_FILE_FROM_BODY action called for AmazonS3 plugin.");
requestParams.add(
new RequestParamDTO(ACTION_CONFIGURATION_PATH, path, null, null, null));
Expand Down Expand Up @@ -697,7 +694,7 @@ private Mono<ActionExecutionResult> executeCommon(
break;
}
case UPLOAD_MULTIPLE_FILES_FROM_BODY: {
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": UPLOAD_MULTIPLE_FILES_FROM_BODY action called for AmazonS3 plugin.");
requestParams.add(
new RequestParamDTO(ACTION_CONFIGURATION_PATH, path, null, null, null));
Expand Down Expand Up @@ -751,7 +748,7 @@ private Mono<ActionExecutionResult> executeCommon(
break;
}
case READ_FILE:
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": READ_FILE action called for AmazonS3 plugin.");
requestParams.add(
new RequestParamDTO(ACTION_CONFIGURATION_PATH, path, null, null, null));
Expand All @@ -770,7 +767,7 @@ private Mono<ActionExecutionResult> executeCommon(
actionResult = Map.of("fileData", result);
break;
case DELETE_FILE:
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": DELETE_FILE action called for AmazonS3 plugin.");
requestParams.add(
new RequestParamDTO(ACTION_CONFIGURATION_PATH, path, null, null, null));
Expand All @@ -783,7 +780,7 @@ private Mono<ActionExecutionResult> executeCommon(
actionResult = Map.of("status", "File deleted successfully");
break;
case DELETE_MULTIPLE_FILES:
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": DELETE_MULTIPLE_FILES action called for AmazonS3 plugin.");
requestParams.add(
new RequestParamDTO(ACTION_CONFIGURATION_PATH, path, null, null, null));
Expand Down Expand Up @@ -823,7 +820,7 @@ private Mono<ActionExecutionResult> executeCommon(
ActionExecutionResult actionExecutionResult = new ActionExecutionResult();
actionExecutionResult.setBody(result);
actionExecutionResult.setIsExecutionSuccess(true);
System.out.println("In the S3 Plugin, got action execution result");
log.debug("In the S3 Plugin, got action execution result");
return Mono.just(actionExecutionResult);
})
.onErrorResume(e -> {
Expand All @@ -842,7 +839,7 @@ private Mono<ActionExecutionResult> executeCommon(
})
// Now set the request in the result to be returned to the server
.map(actionExecutionResult -> {
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": Setting the actionExecutionResult for AmazonS3 plugin.");
ActionExecutionRequest actionExecutionRequest = new ActionExecutionRequest();
actionExecutionRequest.setQuery(query[0]);
Expand Down Expand Up @@ -892,8 +889,7 @@ private DeleteObjectsRequest getDeleteObjectsRequest(String bucketName, List<Str

@Override
public Mono<AmazonS3> datasourceCreate(DatasourceConfiguration datasourceConfiguration) {
String printMessage = Thread.currentThread().getName() + ": datasourceCreate() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": datasourceCreate() called for AmazonS3 plugin.");
try {
Class.forName(S3_DRIVER);
} catch (ClassNotFoundException e) {
Expand Down Expand Up @@ -921,17 +917,14 @@ public Mono<AmazonS3> datasourceCreate(DatasourceConfiguration datasourceConfigu

@Override
public void datasourceDestroy(AmazonS3 connection) {
String printMessage =
Thread.currentThread().getName() + ": datasourceDestroy() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": datasourceDestroy() called for AmazonS3 plugin.");
if (connection != null) {
Mono.fromCallable(() -> {
connection.shutdown();
return connection;
})
.onErrorResume(exception -> {
System.out.println("Error closing S3 connection.");
exception.printStackTrace();
log.error("Error closing S3 connection.", exception.getMessage());
return Mono.empty();
})
.subscribeOn(scheduler)
Expand All @@ -941,9 +934,7 @@ public void datasourceDestroy(AmazonS3 connection) {

@Override
public Set<String> validateDatasource(DatasourceConfiguration datasourceConfiguration) {
String printMessage =
Thread.currentThread().getName() + ": validateDatasource() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": validateDatasource() called for AmazonS3 plugin.");
Set<String> invalids = new HashSet<>();

if (datasourceConfiguration == null || datasourceConfiguration.getAuthentication() == null) {
Expand Down Expand Up @@ -1005,8 +996,7 @@ public Set<String> validateDatasource(DatasourceConfiguration datasourceConfigur

@Override
public Mono<DatasourceTestResult> testDatasource(DatasourceConfiguration datasourceConfiguration) {
String printMessage = Thread.currentThread().getName() + ": testDatasource() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": testDatasource() called for AmazonS3 plugin.");
if (datasourceConfiguration == null) {
return Mono.just(new DatasourceTestResult(
S3ErrorMessages.DS_AT_LEAST_ONE_MANDATORY_PARAMETER_MISSING_ERROR_MSG));
Expand All @@ -1029,7 +1019,7 @@ public Mono<DatasourceTestResult> testDatasource(DatasourceConfiguration datasou
* object with wrong credentials does not throw any exception.
* - Hence, adding a listBuckets() method call to test the connection.
*/
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": listBuckets() called for AmazonS3 plugin.");
connection.listBuckets();
return new DatasourceTestResult();
Expand Down Expand Up @@ -1068,7 +1058,7 @@ private Mono<DatasourceTestResult> testGoogleCloudStorage(DatasourceConfiguratio
return datasourceCreate(datasourceConfiguration)
.flatMap(connection -> Mono.fromCallable(() -> {
connection.listObjects(defaultBucket);
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": connection.listObjects() called for AmazonS3 plugin.");
return new DatasourceTestResult();
})
Expand All @@ -1095,12 +1085,11 @@ private Mono<DatasourceTestResult> testGoogleCloudStorage(DatasourceConfiguratio
public Mono<DatasourceStructure> getStructure(
AmazonS3 connection, DatasourceConfiguration datasourceConfiguration) {

String printMessage = Thread.currentThread().getName() + ": getStructure() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": getStructure() called for AmazonS3 plugin.");
return Mono.fromSupplier(() -> {
List<DatasourceStructure.Table> tableList;
try {
System.out.println(Thread.currentThread().getName()
log.debug(Thread.currentThread().getName()
+ ": connection.listBuckets() called for AmazonS3 plugin.");
tableList = connection.listBuckets().stream()
/* Get name of each bucket */
Expand Down Expand Up @@ -1150,9 +1139,7 @@ public Object substituteValueInInput(
Object... args) {
String jsonBody = (String) input;
Param param = (Param) args[0];
String printMessage =
Thread.currentThread().getName() + ": substituteValueInInput() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": substituteValueInInput() called for AmazonS3 plugin.");
return DataTypeStringUtils.jsonSmartReplacementPlaceholderWithValue(
jsonBody, value, null, insertedParams, null, param);
}
Expand Down Expand Up @@ -1198,9 +1185,8 @@ void uploadFileInS3(
@Override
public Mono<Void> sanitizeGenerateCRUDPageTemplateInfo(
List<ActionConfiguration> actionConfigurationList, Object... args) {
String printMessage = Thread.currentThread().getName()
+ ": sanitizeGenerateCRUDPageTemplateInfo() called for AmazonS3 plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName()
+ ": sanitizeGenerateCRUDPageTemplateInfo() called for AmazonS3 plugin.");
if (isEmpty(actionConfigurationList)) {
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.appsmith.external.models.DatasourceConfiguration;
import com.appsmith.external.models.Property;
import com.external.plugins.exceptions.S3ErrorMessages;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import java.util.List;
Expand All @@ -26,6 +27,7 @@
import static com.external.plugins.constants.S3PluginConstants.S3_SERVICE_PROVIDER_PROPERTY_INDEX;
import static com.external.utils.DatasourceUtils.S3ServiceProvider.AMAZON;

@Slf4j
public class DatasourceUtils {

/**
Expand Down Expand Up @@ -101,7 +103,7 @@ public static S3ServiceProvider fromString(String name) throws AppsmithPluginExc
*/
public static AmazonS3ClientBuilder getS3ClientBuilder(DatasourceConfiguration datasourceConfiguration)
throws AppsmithPluginException {
System.out.println(Thread.currentThread().getName() + ": getS3ClientBuilder action called.");
log.debug(Thread.currentThread().getName() + ": getS3ClientBuilder action called.");
DBAuth authentication = (DBAuth) datasourceConfiguration.getAuthentication();
String accessKey = authentication.getUsername();
String secretKey = authentication.getPassword();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ protected AnthropicPluginExecutor(SharedConfig sharedConfig) {

@Override
public Mono<DatasourceTestResult> testDatasource(DatasourceConfiguration datasourceConfiguration) {
String printMessage = Thread.currentThread().getName() + ": testDatasource() called for Anthropic plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": testDatasource() called for Anthropic plugin.");
final ApiKeyAuth apiKeyAuth = (ApiKeyAuth) datasourceConfiguration.getAuthentication();
if (!StringUtils.hasText(apiKeyAuth.getValue())) {
return Mono.error(new AppsmithPluginException(
Expand Down Expand Up @@ -113,9 +112,7 @@ public Mono<ActionExecutionResult> executeParameterized(
DatasourceConfiguration datasourceConfiguration,
ActionConfiguration actionConfiguration) {

String printMessage =
Thread.currentThread().getName() + ": executeParameterized() called for Anthropic plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": executeParameterized() called for Anthropic plugin.");
// Get prompt from action configuration
List<Map.Entry<String, String>> parameters = new ArrayList<>();

Expand Down Expand Up @@ -254,8 +251,7 @@ private Object formatResponseBodyAsCompletionAPI(String model, byte[] response)
@Override
public Mono<TriggerResultDTO> trigger(
APIConnection connection, DatasourceConfiguration datasourceConfiguration, TriggerRequestDTO request) {
String printMessage = Thread.currentThread().getName() + ": trigger() called for Anthropic plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": trigger() called for Anthropic plugin.");
final ApiKeyAuth apiKeyAuth = (ApiKeyAuth) datasourceConfiguration.getAuthentication();
if (!StringUtils.hasText(apiKeyAuth.getValue())) {
return Mono.error(new AppsmithPluginException(
Expand Down Expand Up @@ -317,9 +313,7 @@ public Mono<TriggerResultDTO> trigger(

@Override
public Set<String> validateDatasource(DatasourceConfiguration datasourceConfiguration) {
String printMessage =
Thread.currentThread().getName() + ": validateDatasource() called for Anthropic plugin.";
System.out.println(printMessage);
log.debug(Thread.currentThread().getName() + ": validateDatasource() called for Anthropic plugin.");
return RequestUtils.validateApiKeyAuthDatasource(datasourceConfiguration);
}

Expand Down
Loading

0 comments on commit 7aae152

Please sign in to comment.