Skip to content

Commit 6d12bd1

Browse files
refactor: normalize log and exception messages (#19)
1 parent 451a620 commit 6d12bd1

File tree

8 files changed

+69
-35
lines changed

8 files changed

+69
-35
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
## [Unreleased]
88
### Changed
9+
- Normalized logging and error messages using Keyple coding standards.
910
- Migrated the CI pipeline from Jenkins to GitHub Actions.
11+
### Upgraded
12+
- `keyple-util-java-lib` from `2.4.0` to `2.4.1` (source code not impacted)
13+
- `slf4j-api` from `1.7.32` to `1.7.36` (`compileOnly`)
1014

1115
## [2.5.1] - 2024-09-19
1216
### Fixed

build.gradle.kts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ plugins {
1414
///////////////////////////////////////////////////////////////////////////////
1515

1616
dependencies {
17-
implementation("org.eclipse.keyple:keyple-util-java-lib:2.4.0")
17+
implementation("org.eclipse.keyple:keyple-util-java-lib:2.4.1")
1818
implementation("com.google.code.gson:gson:2.10.1")
19-
implementation("org.slf4j:slf4j-api:1.7.32")
20-
testImplementation("org.slf4j:slf4j-simple:1.7.32")
19+
compileOnly("org.slf4j:slf4j-api:1.7.36")
20+
21+
testImplementation("org.slf4j:slf4j-simple:1.7.36")
2122
testImplementation(platform("org.junit:junit-bom:5.10.2"))
2223
testImplementation("org.junit.jupiter:junit-jupiter")
2324
testImplementation("org.junit.vintage:junit-vintage-engine")

src/main/java/org/eclipse/keyple/distributed/AbstractNodeAdapter.java

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,7 @@ final void closeSessionSilently(String sessionId) {
113113
try {
114114
closeSession(sessionId);
115115
} catch (RuntimeException e) {
116-
logger.error(
117-
"Error during the silent closing of node's session [{}]: {}",
118-
sessionId,
119-
e.getMessage(),
120-
e);
116+
logger.error("Failed to close node session safely [sessionId={}]", sessionId, e);
121117
}
122118
}
123119

@@ -319,10 +315,7 @@ synchronized void waitForState(SessionManagerState... targetStates) {
319315
}
320316
timeoutOccurred();
321317
} catch (InterruptedException e) {
322-
logger.error(
323-
"Unexpected interruption of the task associated with the node's session {}",
324-
sessionId,
325-
e);
318+
logger.error("Unexpected node session task interruption [sessionId={}]", sessionId, e);
326319
Thread.currentThread().interrupt();
327320
}
328321
}
@@ -350,9 +343,12 @@ void checkState(SessionManagerState... targetStates) {
350343
}
351344
}
352345
throw new IllegalStateException(
353-
String.format(
354-
"The status of the node's session manager [%s] should have been one of %s, but is currently [%s]",
355-
sessionId, Arrays.toString(targetStates), state));
346+
"The status of the node's session manager '"
347+
+ sessionId
348+
+ "' should have been one of "
349+
+ Arrays.toString(targetStates)
350+
+ ", but is currently "
351+
+ state);
356352
}
357353

358354
/**
@@ -363,11 +359,9 @@ void checkState(SessionManagerState... targetStates) {
363359
*/
364360
void timeoutOccurred() {
365361
state = SessionManagerState.ABORTED_SESSION;
366-
logger.error(
367-
"Timeout occurs for the task associated with the node's session [{}]", sessionId);
362+
logger.error("Node session task timed out [sessionId={}]", sessionId);
368363
throw new NodeCommunicationException(
369-
String.format(
370-
"Timeout occurs for the task associated with the node's session [%s]", sessionId));
364+
"Timeout occurs for the task associated with the node's session '" + sessionId + "'");
371365
}
372366
}
373367
}

src/main/java/org/eclipse/keyple/distributed/AsyncNodeClientAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,9 @@ public void onError(String sessionId, Throwable error) {
176176
private SessionManager getManagerForEndpoint(String sessionId) {
177177
SessionManager manager = sessionManagers.get(sessionId);
178178
if (manager == null) {
179-
logger.warn("Node's session not found [{}]. It was maybe closed due to a timeout", sessionId);
179+
logger.warn(
180+
"Node session not found. It was maybe closed due to a timeout event [sessionId={}]",
181+
sessionId);
180182
}
181183
return manager;
182184
}

src/main/java/org/eclipse/keyple/distributed/AsyncNodeServerAdapter.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ void sendMessage(MessageDto message) {
7575
private SessionManager getManagerForHandler(String sessionId) {
7676
SessionManager manager = sessionManagers.get(sessionId);
7777
if (manager == null) {
78-
throw new IllegalStateException(
79-
String.format("The node's session [%s] is closed", sessionId));
78+
throw new IllegalStateException("The node's session '" + sessionId + "' is closed");
8079
}
8180
return manager;
8281
}

src/main/java/org/eclipse/keyple/distributed/MessageDto.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -398,4 +398,38 @@ public final MessageDto setBody(String body) {
398398
this.body = body;
399399
return this;
400400
}
401+
402+
/**
403+
* {@inheritDoc}
404+
*
405+
* @since 2.5.2
406+
*/
407+
@Override
408+
public String toString() {
409+
return "MessageDto{"
410+
+ "apiLevel="
411+
+ apiLevel
412+
+ ", sessionId='"
413+
+ sessionId
414+
+ '\''
415+
+ ", action='"
416+
+ action
417+
+ '\''
418+
+ ", clientNodeId='"
419+
+ clientNodeId
420+
+ '\''
421+
+ ", serverNodeId='"
422+
+ serverNodeId
423+
+ '\''
424+
+ ", localReaderName='"
425+
+ localReaderName
426+
+ '\''
427+
+ ", remoteReaderName='"
428+
+ remoteReaderName
429+
+ '\''
430+
+ ", body='"
431+
+ body
432+
+ '\''
433+
+ '}';
434+
}
401435
}

src/main/java/org/eclipse/keyple/distributed/SyncNodeClientAdapter.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ MessageDto sendRequest(MessageDto message) {
9191
return response;
9292
} else {
9393
throw new IllegalStateException(
94-
String.format(
95-
"The list returned by the client endpoint should have contained a single element but contains %s elements",
96-
responses.size()));
94+
"The list returned by the client endpoint should have contained a single element but contains "
95+
+ responses.size()
96+
+ " elements");
9797
}
9898
}
9999

@@ -246,7 +246,7 @@ public void run() {
246246
try {
247247
Thread.sleep(requestFrequencyMillis);
248248
} catch (InterruptedException e) {
249-
logger.error("Unexpected interruption of thread {}", getName(), e);
249+
logger.error("Unexpected thread interruption [thread={}]", getName(), e);
250250
Thread.currentThread().interrupt();
251251
}
252252
}
@@ -267,7 +267,10 @@ public void run() {
267267
private class EventObserverUncaughtExceptionHandler implements Thread.UncaughtExceptionHandler {
268268
@Override
269269
public void uncaughtException(Thread t, Throwable e) {
270-
logger.error("Interruption of thread {} caused by an unhandled exception", t.getName(), e);
270+
logger.error(
271+
"Unexpected thread interruption caused by an unhandled exception [thread={}]",
272+
t.getName(),
273+
e);
271274
}
272275
}
273276

@@ -306,7 +309,7 @@ private List<MessageDto> retryRequest() {
306309
try {
307310
timer = timer1 + timer2;
308311
Thread.sleep(timer);
309-
logger.info("Retry to send request after {} seconds...", timer / 1000);
312+
logger.info("Retrying to send request after {} seconds", timer / 1000);
310313
responses = sendRequestSilently();
311314
if (responses != null) {
312315
logger.info("Server connection retrieved");
@@ -316,7 +319,8 @@ private List<MessageDto> retryRequest() {
316319
timer2 = timer;
317320
}
318321
} catch (InterruptedException e) {
319-
logger.error("Unexpected interruption of thread {}", Thread.currentThread().getName(), e);
322+
logger.error(
323+
"Unexpected thread interruption [thread={}]", Thread.currentThread().getName(), e);
320324
Thread.currentThread().interrupt();
321325
}
322326
}

src/main/java/org/eclipse/keyple/distributed/SyncNodeServerAdapter.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,7 @@ private void checkEventManagers(
196196
// Check the current client node ID.
197197
if (unusedClientNodeIds.contains(clientNodeId)) {
198198
throw new IllegalStateException(
199-
String.format(
200-
"Client node ID [%s] removed because not used for at least 1 day", clientNodeId));
199+
"Client node ID '" + clientNodeId + "' removed because not used for at least 1 day");
201200
}
202201
}
203202

@@ -436,10 +435,7 @@ private synchronized void waitAtMost(int maxAwaitingTimeMillis) {
436435
wait(maxAwaitingTimeMillis);
437436
}
438437
} catch (InterruptedException e) {
439-
logger.error(
440-
"Unexpected interruption of the task associated with the node's id {}",
441-
clientNodeId,
442-
e);
438+
logger.error("Unexpected node task interruption [clientNodeId={}]", clientNodeId, e);
443439
Thread.currentThread().interrupt();
444440
}
445441
}

0 commit comments

Comments
 (0)