Skip to content

Commit f231ddb

Browse files
authored
fix: improve logging (#67)
1 parent a1b985d commit f231ddb

File tree

9 files changed

+56
-41
lines changed

9 files changed

+56
-41
lines changed

src/main/java/at/ac/uibk/dps/cirrina/cirrina/Cirrina.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ import com.google.common.flogger.FluentLogger
1111
import io.opentelemetry.api.OpenTelemetry
1212
import io.opentelemetry.sdk.autoconfigure.AutoConfiguredOpenTelemetrySdk
1313
import java.net.URI
14+
import java.util.logging.LogManager
15+
import org.apache.commons.lang3.builder.ToStringBuilder
16+
import org.apache.commons.lang3.builder.ToStringStyle.SIMPLE_STYLE
1417

1518
private val logger: FluentLogger = FluentLogger.forEnclosingClass()
1619

@@ -20,6 +23,17 @@ class Cirrina {
2023
const val NATS_CONNECTION_TIMEOUT = 60000L
2124

2225
init {
26+
ToStringBuilder.setDefaultStyle(SIMPLE_STYLE)
27+
28+
runCatching {
29+
Cirrina::class.java.getResourceAsStream("/logging.properties")?.use { inputStream ->
30+
LogManager.getLogManager().readConfiguration(inputStream)
31+
} ?: logger.atWarning().log("Logging properties file not found")
32+
}
33+
.onFailure { ex ->
34+
logger.atSevere().withCause(ex).log("Could not load logging properties")
35+
}
36+
2337
logger.atFine().log("Starting health service")
2438
runCatching { HealthService(EnvironmentVariables.healthPort.get()) }
2539
.getOrElse { e -> logger.atSevere().withCause(e).log("Could not start the health service") }

src/main/java/at/ac/uibk/dps/cirrina/execution/command/ActionAssignCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public List<ActionCommand> execute() throws UnsupportedOperationException {
6464
)
6565
);
6666
} catch (IOException e) {
67-
logger.atWarning().withCause(e).log("Data assignment failed");
67+
logger.atWarning().log("Data assignment failed");
6868
}
6969

7070
return commands;

src/main/java/at/ac/uibk/dps/cirrina/execution/command/ActionCreateCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public List<ActionCommand> execute() throws UnsupportedOperationException {
6868
gauges.attributesForData("create", !isPersistent ? "local" : "persistent", size)
6969
);
7070
} catch (Exception e) {
71-
logger.atWarning().withCause(e).log("Data creation failed");
71+
logger.atWarning().log("Data creation failed");
7272
}
7373

7474
return commands;

src/main/java/at/ac/uibk/dps/cirrina/execution/command/ActionInvokeCommand.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,20 +61,19 @@ public List<ActionCommand> execute() throws UnsupportedOperationException {
6161
// Invoke (asynchronously)
6262
serviceImplementation
6363
.invoke(input, executionContext.scope().getId())
64-
.exceptionally(e -> {
65-
logger
66-
.atWarning()
67-
.withCause(e)
68-
.log(
69-
"Service invocation failed for service '%s'",
70-
serviceImplementation.getInformationString()
71-
);
72-
return null;
73-
})
74-
.thenAccept(output -> {
75-
assignServiceOutput(output, extent);
76-
raiseEvents(output, eventListener, eventHandler);
77-
measurePerformance(start, serviceImplementation);
64+
.whenComplete((output, e) -> {
65+
if (e != null) {
66+
logger
67+
.atWarning()
68+
.log(
69+
"Service invocation failed for service '%s'",
70+
serviceImplementation.getInformationString()
71+
);
72+
} else {
73+
assignServiceOutput(output, extent);
74+
raiseEvents(output, eventListener, eventHandler);
75+
measurePerformance(start, serviceImplementation);
76+
}
7877
});
7978

8079
return commands;

src/main/java/at/ac/uibk/dps/cirrina/execution/command/ActionMatchCommand.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public List<ActionCommand> execute() throws UnsupportedOperationException {
3939
}
4040
}
4141
} catch (UnsupportedOperationException e) {
42-
logger.atWarning().withCause(e).log("Could not execute match action");
42+
logger.atWarning().log("Could not execute match action");
4343
}
4444

4545
return commands;

src/main/java/at/ac/uibk/dps/cirrina/execution/object/context/NatsContext.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ class NatsContext(
6161
connection = conn
6262
} catch (e: Exception) {
6363
logger
64-
.atWarning()
64+
.atSevere()
6565
.withCause(e)
6666
.log("Failed to setup the persistent context bucket")
6767
} finally {
@@ -234,7 +234,7 @@ class NatsContext(
234234
conn.close()
235235
}
236236
}
237-
.onFailure { e -> logger.atWarning().withCause(e).log("Failed to close the NATS") }
237+
.onFailure { _ -> logger.atWarning().log("Failed to close the NATS") }
238238
}
239239
}
240240

src/main/java/at/ac/uibk/dps/cirrina/execution/object/event/NatsEventHandler.kt

Lines changed: 9 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
5454
}
5555
connection = conn
5656
} catch (e: Exception) {
57-
logger.atWarning().withCause(e).log("Failed to setup the NATS event handler")
57+
logger.atSevere().withCause(e).log("Failed to setup the NATS event handler")
5858
} finally {
5959
connectedLatch.countDown()
6060
}
@@ -97,8 +97,8 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
9797
.onFailure { e ->
9898
when (e) {
9999
is UnsupportedOperationException ->
100-
logger.atFiner().withCause(e).log("A message could not be read as an event")
101-
else -> logger.atWarning().withCause(e).log("Unexpected error while handling a message")
100+
logger.atFiner().log("A message could not be read as an event")
101+
else -> logger.atWarning().log("Unexpected error while handling a message")
102102
}
103103
}
104104
}
@@ -133,9 +133,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
133133
synchronized(lock) {
134134
connection?.let { conn ->
135135
runCatching { conn.publish(subject, EventExchange(event).toBytes()) }
136-
.onFailure { e ->
137-
logger.atWarning().withCause(e).log("Failed to publish event '$subject'", e)
138-
}
136+
.onFailure { _ -> logger.atWarning().log("Failed to publish event '$subject'") }
139137
} ?: logger.atWarning().log("Not sending event, not connected to the NATS server")
140138
}
141139
}
@@ -153,7 +151,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
153151
dispatcher?.subscribe(subject)
154152
?: logger.atFiner().log("Dispatcher unavailable; queued subscription: $subject")
155153
}
156-
.onFailure { e -> logger.atWarning().withCause(e).log("Could not subscribe to $eventName") }
154+
.onFailure { _ -> logger.atWarning().log("Could not subscribe to $eventName") }
157155
}
158156
}
159157

@@ -167,9 +165,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
167165
synchronized(lock) {
168166
subscriptions.remove(subject)
169167
runCatching { dispatcher?.unsubscribe(subject) }
170-
.onFailure { e ->
171-
logger.atWarning().withCause(e).log("Could not unsubscribe from $eventName", e)
172-
}
168+
.onFailure { _ -> logger.atWarning().log("Could not unsubscribe from $eventName") }
173169
}
174170
}
175171

@@ -187,9 +183,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
187183
dispatcher?.subscribe(subject)
188184
?: logger.atFiner().log("Dispatcher unavailable; queued subscription: $subject")
189185
}
190-
.onFailure { e ->
191-
logger.atWarning().withCause(e).log("Could not subscribe to $subject", e)
192-
}
186+
.onFailure { _ -> logger.atWarning().log("Could not subscribe to $subject") }
193187
}
194188
}
195189

@@ -204,9 +198,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
204198
synchronized(lock) {
205199
subscriptions.remove(subject)
206200
runCatching { dispatcher?.unsubscribe(subject) }
207-
.onFailure { e ->
208-
logger.atWarning().withCause(e).log("Could not unsubscribe from $subject", e)
209-
}
201+
.onFailure { _ -> logger.atWarning().log("Could not unsubscribe from $subject") }
210202
}
211203
}
212204

@@ -221,7 +213,7 @@ class NatsEventHandler(natsUrl: String) : EventHandler() {
221213
dispatcher?.let { connection?.closeDispatcher(it) }
222214
connection?.close()
223215
}
224-
.onFailure { e -> logger.atWarning().withCause(e).log("Failed to close the NATS") }
216+
.onFailure { _ -> logger.atWarning().log("Failed to close the NATS") }
225217
}
226218
}
227219

src/main/java/at/ac/uibk/dps/cirrina/execution/object/statemachine/StateMachine.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -487,7 +487,7 @@ private void switchActiveState(State state) throws IllegalArgumentException {
487487
}
488488

489489
logger
490-
.atFine()
490+
.atFiner()
491491
.log("State machine '%s': Switching state '%s' to '%s'", this, activeState, state);
492492

493493
// Update the active state
@@ -620,7 +620,7 @@ private void handleInternalTransition(
620620
@NotNull Transition transition,
621621
@Nullable Event raisingEvent
622622
) throws UnsupportedOperationException {
623-
logger.atFine().log("State machine '%s': Handling internal transition '%s'", this, transition);
623+
logger.atFiner().log("State machine '%s': Handling internal transition '%s'", this, transition);
624624

625625
// Only perform the transition
626626
doTransition(transition, raisingEvent);
@@ -637,7 +637,7 @@ private void handleExternalTransition(
637637
@NotNull Transition transition,
638638
@Nullable Event raisingEvent
639639
) throws UnsupportedOperationException {
640-
logger.atFine().log("State machine '%s': Handling external transition '%s'", this, transition);
640+
logger.atFiner().log("State machine '%s': Handling external transition '%s'", this, transition);
641641

642642
final var targetStateName = transition.getTargetStateName().get();
643643

@@ -723,7 +723,7 @@ private Optional<Transition> handleEvent(Event event)
723723
);
724724
}
725725
} catch (IOException e) {
726-
logger.atWarning().withCause(e).log("Failed to set event data");
726+
logger.atWarning().log("Failed to set event data");
727727
}
728728
},
729729
() -> logger.atFiner().log("State machine '%s': No on transition selected", this)
@@ -822,7 +822,10 @@ public String getId() {
822822

823823
@Override
824824
public String toString() {
825-
return new ToStringBuilder(this).append("id", stateMachineId).toString();
825+
return new ToStringBuilder(this)
826+
.append("id", stateMachineId)
827+
.append("name", stateMachineClass.getName())
828+
.toString();
826829
}
827830

828831
/**
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
handlers = java.util.logging.ConsoleHandler
2+
3+
java.util.logging.ConsoleHandler.level = FINE
4+
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
5+
6+
.level = INFO
7+
at.ac.uibk.dps.cirrina.level = FINE

0 commit comments

Comments
 (0)