Skip to content

Commit 35b171e

Browse files
KirrTapdbwiddis
andauthored
Replace String concatenation with Log4j ParameterizedMessage for readability (#943)
* Replace strings in GetWorkflowTransportAction.java Signed-off-by: Patrik Cmil <[email protected]> * Replace strings in 5 files Signed-off-by: Patrik Cmil <[email protected]> * Replace strings in 10 files Signed-off-by: Patrik Cmil <[email protected]> * Replace strings in 40 files Signed-off-by: Patrik Cmil <[email protected]> * Changed back strings, which were not be parametrized Signed-off-by: Patrik Cmil <[email protected]> * Update CHANGELOG.md Signed-off-by: Patrik Cmil <[email protected]> * Add EOF line break Signed-off-by: Daniel Widdis <[email protected]> --------- Signed-off-by: Patrik Cmil <[email protected]> Signed-off-by: Daniel Widdis <[email protected]> Co-authored-by: Daniel Widdis <[email protected]>
1 parent 219aec5 commit 35b171e

26 files changed

+297
-79
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.1.0/)
2424
### Documentation
2525
### Maintenance
2626
### Refactoring
27+
- Replace String concatenation with Log4j ParameterizedMessage for readability ([#943](https://github.com/opensearch-project/flow-framework/pull/943))

src/main/java/org/opensearch/flowframework/indices/FlowFrameworkIndicesHandler.java

+81-23
Large diffs are not rendered by default.

src/main/java/org/opensearch/flowframework/model/WorkflowNode.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.common.unit.TimeValue;
1415
import org.opensearch.core.rest.RestStatus;
1516
import org.opensearch.core.xcontent.ToXContentObject;
@@ -171,7 +172,10 @@ public static WorkflowNode parse(XContentParser parser) throws IOException {
171172
String configurationsString = ParseUtils.parseArbitraryStringToObjectMapToString(configurationsMap);
172173
userInputs.put(inputFieldName, configurationsString);
173174
} catch (Exception ex) {
174-
String errorMessage = "Failed to parse" + inputFieldName + "map";
175+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
176+
"Failed to parse {} map",
177+
inputFieldName
178+
).getFormattedMessage();
175179
logger.error(errorMessage, ex);
176180
throw new FlowFrameworkException(errorMessage, RestStatus.BAD_REQUEST);
177181
}

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

+33-10
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.action.get.GetRequest;
1516
import org.opensearch.action.search.SearchRequest;
@@ -201,7 +202,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
201202
try {
202203
validateWorkflows(templateWithUser);
203204
} catch (Exception e) {
204-
String errorMessage = "Workflow validation failed for template " + templateWithUser.name();
205+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
206+
"Workflow validation failed for template {}",
207+
templateWithUser.name()
208+
).getFormattedMessage();
205209
logger.error(errorMessage, e);
206210
listener.onFailure(
207211
e instanceof FlowFrameworkException ? e : new FlowFrameworkException(errorMessage, ExceptionsHelper.status(e))
@@ -218,7 +222,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
218222
flowFrameworkSettings.getMaxWorkflows(),
219223
ActionListener.wrap(max -> {
220224
if (FALSE.equals(max)) {
221-
String errorMessage = "Maximum workflows limit reached: " + flowFrameworkSettings.getMaxWorkflows();
225+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
226+
"Maximum workflows limit reached: {}",
227+
flowFrameworkSettings.getMaxWorkflows()
228+
).getFormattedMessage();
222229
logger.error(errorMessage);
223230
FlowFrameworkException ffe = new FlowFrameworkException(errorMessage, RestStatus.BAD_REQUEST);
224231
listener.onFailure(ffe);
@@ -307,7 +314,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
307314
}));
308315
}
309316
}, exception -> {
310-
String errorMessage = "Failed to update use case template " + request.getWorkflowId();
317+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
318+
"Failed to update use case template {}",
319+
request.getWorkflowId()
320+
).getFormattedMessage();
311321
logger.error(errorMessage, exception);
312322
if (exception instanceof FlowFrameworkException) {
313323
listener.onFailure(exception);
@@ -350,7 +360,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
350360
ActionListener.wrap(reprovisionResponse -> {
351361
listener.onResponse(new WorkflowResponse(reprovisionResponse.getWorkflowId()));
352362
}, exception -> {
353-
String errorMessage = "Reprovisioning failed for workflow " + workflowId;
363+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
364+
"Reprovisioning failed for workflow {}",
365+
workflowId
366+
).getFormattedMessage();
354367
logger.error(errorMessage, exception);
355368
if (exception instanceof FlowFrameworkException) {
356369
listener.onFailure(exception);
@@ -382,9 +395,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
382395
);
383396
listener.onResponse(new WorkflowResponse(request.getWorkflowId()));
384397
}, exception -> {
385-
String errorMessage = "Failed to update workflow "
386-
+ request.getWorkflowId()
387-
+ " in template index";
398+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
399+
"Failed to update workflow {} in template index",
400+
request.getWorkflowId()
401+
).getFormattedMessage();
388402
logger.error(errorMessage, exception);
389403
if (exception instanceof FlowFrameworkException) {
390404
listener.onFailure(exception);
@@ -399,7 +413,10 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
399413
listener.onResponse(new WorkflowResponse(request.getWorkflowId()));
400414
}
401415
}, exception -> {
402-
String errorMessage = "Failed to update use case template " + request.getWorkflowId();
416+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
417+
"Failed to update use case template {}",
418+
request.getWorkflowId()
419+
).getFormattedMessage();
403420
logger.error(errorMessage, exception);
404421
if (exception instanceof FlowFrameworkException) {
405422
listener.onFailure(exception);
@@ -411,12 +428,18 @@ private void createExecute(WorkflowRequest request, User user, ActionListener<Wo
411428
);
412429
}
413430
} else {
414-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
431+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
432+
"Failed to retrieve template ({}) from global context.",
433+
workflowId
434+
).getFormattedMessage();
415435
logger.error(errorMessage);
416436
listener.onFailure(new FlowFrameworkException(errorMessage, RestStatus.NOT_FOUND));
417437
}
418438
}, exception -> {
419-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
439+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
440+
"Failed to retrieve template ({}) from global context.",
441+
workflowId
442+
).getFormattedMessage();
420443
logger.error(errorMessage, exception);
421444
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(exception)));
422445
}));

src/main/java/org/opensearch/flowframework/transport/DeprovisionWorkflowTransportAction.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.OpenSearchStatusException;
1516
import org.opensearch.action.support.ActionFilters;
@@ -166,7 +167,10 @@ private void executeDeprovisionRequest(
166167
)
167168
);
168169
}, exception -> {
169-
String errorMessage = "Failed to get workflow state for workflow " + workflowId;
170+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
171+
"Failed to get workflow state for workflow {}",
172+
workflowId
173+
).getFormattedMessage();
170174
logger.error(errorMessage, exception);
171175
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(exception)));
172176
}));

src/main/java/org/opensearch/flowframework/transport/GetWorkflowStateTransportAction.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.action.get.GetRequest;
1516
import org.opensearch.action.support.ActionFilters;
@@ -96,7 +97,8 @@ protected void doExecute(Task task, GetWorkflowStateRequest request, ActionListe
9697
);
9798

9899
} catch (Exception e) {
99-
String errorMessage = "Failed to get workflow: " + workflowId;
100+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage("Failed to get workflow: {}", workflowId)
101+
.getFormattedMessage();
100102
logger.error(errorMessage, e);
101103
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(e)));
102104
}
@@ -123,7 +125,8 @@ private void executeGetWorkflowStateRequest(
123125
WorkflowState workflowState = WorkflowState.parse(parser);
124126
listener.onResponse(new GetWorkflowStateResponse(workflowState, request.getAll()));
125127
} catch (Exception e) {
126-
String errorMessage = "Failed to parse workflowState: " + r.getId();
128+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage("Failed to parse workflowState: {}", r.getId())
129+
.getFormattedMessage();
127130
logger.error(errorMessage, e);
128131
listener.onFailure(new FlowFrameworkException(errorMessage, RestStatus.BAD_REQUEST));
129132
}
@@ -134,7 +137,8 @@ private void executeGetWorkflowStateRequest(
134137
if (e instanceof IndexNotFoundException) {
135138
listener.onFailure(new FlowFrameworkException("Fail to find workflow status of " + workflowId, RestStatus.NOT_FOUND));
136139
} else {
137-
String errorMessage = "Failed to get workflow status of: " + workflowId;
140+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage("Failed to get workflow status of: {}", workflowId)
141+
.getFormattedMessage();
138142
logger.error(errorMessage, e);
139143
listener.onFailure(new FlowFrameworkException(errorMessage, RestStatus.NOT_FOUND));
140144
}

src/main/java/org/opensearch/flowframework/transport/GetWorkflowTransportAction.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.action.get.GetRequest;
1516
import org.opensearch.action.support.ActionFilters;
@@ -104,7 +105,10 @@ protected void doExecute(Task task, WorkflowRequest request, ActionListener<GetW
104105
xContentRegistry
105106
);
106107
} catch (Exception e) {
107-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
108+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
109+
"Failed to retrieve template ({}) from global context.",
110+
workflowId
111+
).getFormattedMessage();
108112
logger.error(errorMessage, e);
109113
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(e)));
110114
}
@@ -134,7 +138,10 @@ private void executeGetRequest(
134138
context.restore();
135139

136140
if (!response.isExists()) {
137-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
141+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
142+
"Failed to retrieve template ({}) from global context.",
143+
workflowId
144+
).getFormattedMessage();
138145
logger.error(errorMessage);
139146
listener.onFailure(new FlowFrameworkException(errorMessage, RestStatus.NOT_FOUND));
140147
} else {
@@ -144,7 +151,10 @@ private void executeGetRequest(
144151
listener.onResponse(new GetWorkflowResponse(template));
145152
}
146153
}, exception -> {
147-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
154+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
155+
"Failed to retrieve template ({}) from global context.",
156+
workflowId
157+
).getFormattedMessage();
148158
logger.error(errorMessage, exception);
149159
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(exception)));
150160
}));

src/main/java/org/opensearch/flowframework/transport/ProvisionWorkflowTransportAction.java

+21-5
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.apache.logging.log4j.LogManager;
1212
import org.apache.logging.log4j.Logger;
13+
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
1314
import org.opensearch.ExceptionsHelper;
1415
import org.opensearch.action.get.GetRequest;
1516
import org.opensearch.action.support.ActionFilters;
@@ -138,7 +139,10 @@ protected void doExecute(Task task, WorkflowRequest request, ActionListener<Work
138139
xContentRegistry
139140
);
140141
} catch (Exception e) {
141-
String errorMessage = "Failed to retrieve template from global context for workflow " + workflowId;
142+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
143+
"Failed to retrieve template from global context for workflow {}",
144+
workflowId
145+
).getFormattedMessage();
142146
logger.error(errorMessage, e);
143147
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(e)));
144148
}
@@ -169,7 +173,10 @@ private void executeProvisionRequest(
169173
context.restore();
170174

171175
if (!response.isExists()) {
172-
String errorMessage = "Failed to retrieve template (" + workflowId + ") from global context.";
176+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
177+
"Failed to retrieve template ({}) from global context.",
178+
workflowId
179+
).getFormattedMessage();
173180
logger.error(errorMessage);
174181
listener.onFailure(new FlowFrameworkException(errorMessage, RestStatus.NOT_FOUND));
175182
return;
@@ -212,7 +219,10 @@ private void executeProvisionRequest(
212219
ActionListener.wrap(templateResponse -> {
213220
listener.onResponse(new WorkflowResponse(request.getWorkflowId()));
214221
}, exception -> {
215-
String errorMessage = "Failed to update use case template " + request.getWorkflowId();
222+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
223+
"Failed to update use case template {}",
224+
request.getWorkflowId()
225+
).getFormattedMessage();
216226
logger.error(errorMessage, exception);
217227
if (exception instanceof FlowFrameworkException) {
218228
listener.onFailure(exception);
@@ -224,7 +234,10 @@ private void executeProvisionRequest(
224234
true
225235
);
226236
}, exception -> {
227-
String errorMessage = "Failed to update workflow state: " + workflowId;
237+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
238+
"Failed to update workflow state: {}",
239+
workflowId
240+
).getFormattedMessage();
228241
logger.error(errorMessage, exception);
229242
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(exception)));
230243
})
@@ -244,7 +257,10 @@ private void executeProvisionRequest(
244257
logger.error("Workflow validation failed for workflow {}", workflowId);
245258
listener.onFailure(exception);
246259
} else {
247-
String errorMessage = "Failed to retrieve template from global context for workflow " + workflowId;
260+
String errorMessage = ParameterizedMessageFactory.INSTANCE.newMessage(
261+
"Failed to retrieve template from global context for workflow {}",
262+
workflowId
263+
).getFormattedMessage();
248264
logger.error(errorMessage, exception);
249265
listener.onFailure(new FlowFrameworkException(errorMessage, ExceptionsHelper.status(exception)));
250266
}

0 commit comments

Comments
 (0)