Skip to content

Commit

Permalink
Add Log based transaction observer
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Aug 21, 2023
1 parent a2a99ff commit db8e3f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import convex.observer.LogObserver;
import convex.observer.StrimziKafka;
import convex.peer.Server;
import convex.peer.TransactionHandler;
Expand All @@ -26,6 +27,7 @@ public ObserverPanel(Server server) {

this.setLayout(new MigLayout("wrap 2"));

add(new JLabel("Transactions"),"span 2");
JRadioButton noneButton=addButton("Transactions",new JLabel("None"),()->{
server.getTransactionHandler().setRequestObserver(null);
server.getTransactionHandler().setResponseObserver(null);
Expand All @@ -42,6 +44,17 @@ public ObserverPanel(Server server) {
});
add(strmButton);
add(new JLabel("Strimzi"));

JRadioButton tlogButton=addButton("Transactions",new JLabel("Logs"),()->{
TransactionHandler th=server.getTransactionHandler();
LogObserver ob=new LogObserver(server);
th.setRequestObserver(ob.getTransactionRequestObserver());
th.setResponseObserver(ob.getTransactionResponseObserver());
});
add(tlogButton);
add(new JLabel("SLF4J Logging"));


}

private JRadioButton addButton(String bgName,JLabel jLabel, Runnable action) {
Expand Down
34 changes: 34 additions & 0 deletions convex-observer/src/main/java/convex/observer/LogObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package convex.observer;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import convex.core.Result;
import convex.core.data.SignedData;
import convex.core.transactions.ATransaction;
import convex.peer.Server;

public class LogObserver {
static final Logger log = LoggerFactory.getLogger(LogObserver.class.getName());

protected Server server;

public LogObserver(Server server) {
this.server=server;
}

public Consumer<SignedData<ATransaction>> getTransactionRequestObserver() {
return stx->{
log.info("TX Request: {}",stx);
};
}

public BiConsumer<SignedData<ATransaction>,Result> getTransactionResponseObserver() {
return (tx,r)->{
log.info("TX Response: {} for tx {}",r,tx);
};
}
}

0 comments on commit db8e3f3

Please sign in to comment.