Commit ef49db2f authored by Mohamad Bashar Desoki's avatar Mohamad Bashar Desoki

logging - file appender

parent fc503baf
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
*topics* *topics*
* [Logback vs SLF4J vs Log4J2 - what is the difference?](https://www.youtube.com/watch?v=SWHYrCXIL38&ab_channel=JavaBrains) * [Logback vs SLF4J vs Log4J2 - what is the difference?](https://www.youtube.com/watch?v=SWHYrCXIL38&ab_channel=JavaBrains)
* [SLF4J Logback Tutorial](https://mkyong.com/logging/slf4j-logback-tutorial/)
* Introduction to Service Registry & Service Discovery * Introduction to Service Registry & Service Discovery
* Implement Service Registry with Zookeeper * Implement Service Registry with Zookeeper
* Integrate into our leader-worker architecture * Integrate into our leader-worker architecture
\ No newline at end of file
2024-01-16 10:35:09,211 INFO Registration_Discovery.Application [main] Connected
2024-01-16 10:35:19,177 DEBUG Registration_Discovery.Application [main-EventThread] Successfully connected to Zookeeper
2024-01-16 10:35:19,277 INFO Registration_Discovery.LeaderElection [main] I'm a leader
2024-01-16 10:35:19,296 INFO Registration_Discovery.ServiceRegistry [main] The cluster addresses are: []
package Registration_Discovery;
import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher;
...@@ -9,6 +11,8 @@ import java.io.IOException; ...@@ -9,6 +11,8 @@ import java.io.IOException;
public class Application implements Watcher { public class Application implements Watcher {
private static final Logger logger = LoggerFactory.getLogger(Application.class);
private static final String address = "172.29.3.101:2181"; private static final String address = "172.29.3.101:2181";
private static final int SESSION_TIMEOUT = 3000; //dead client private static final int SESSION_TIMEOUT = 3000; //dead client
private static final int DEFAULT_PORT = 8080; private static final int DEFAULT_PORT = 8080;
...@@ -20,6 +24,8 @@ public class Application implements Watcher { ...@@ -20,6 +24,8 @@ public class Application implements Watcher {
Application application = new Application(); Application application = new Application();
ZooKeeper zooKeeper = application.connectToZookeeper(); ZooKeeper zooKeeper = application.connectToZookeeper();
logger.info("Connected");
ServiceRegistry serviceRegistry = new ServiceRegistry(zooKeeper); ServiceRegistry serviceRegistry = new ServiceRegistry(zooKeeper);
OnElectionAction onElectionAction = new OnElectionAction(serviceRegistry, currentServerPort); OnElectionAction onElectionAction = new OnElectionAction(serviceRegistry, currentServerPort);
...@@ -31,6 +37,7 @@ public class Application implements Watcher { ...@@ -31,6 +37,7 @@ public class Application implements Watcher {
application.run(); application.run();
application.close(); application.close();
} }
public ZooKeeper connectToZookeeper() throws IOException { public ZooKeeper connectToZookeeper() throws IOException {
...@@ -53,14 +60,17 @@ public class Application implements Watcher { ...@@ -53,14 +60,17 @@ public class Application implements Watcher {
switch (watchedEvent.getType()) { switch (watchedEvent.getType()) {
case None: case None:
if (watchedEvent.getState() == Event.KeeperState.SyncConnected) { if (watchedEvent.getState() == Event.KeeperState.SyncConnected) {
System.out.println("Successfully connected to Zookeeper"); // System.out.println("Successfully connected to Zookeeper");
logger.debug("Successfully connected to Zookeeper");
} else if (watchedEvent.getState() == Event.KeeperState.Disconnected) { } else if (watchedEvent.getState() == Event.KeeperState.Disconnected) {
synchronized (zooKeeper) { synchronized (zooKeeper) {
System.out.println("Disconnected from Zookeeper"); // System.out.println("Disconnected from Zookeeper");
logger.debug("Disconnected from Zookeeper");
zooKeeper.notifyAll(); zooKeeper.notifyAll();
} }
} else if (watchedEvent.getState() == Event.KeeperState.Closed) { } else if (watchedEvent.getState() == Event.KeeperState.Closed) {
System.out.println("Closed Successfully"); // System.out.println("Closed Successfully");
logger.debug("Closed Successfully");
} }
break; break;
} }
......
package Registration_Discovery;
import org.apache.zookeeper.*; import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class LeaderElection implements Watcher { public class LeaderElection implements Watcher {
private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class);
private static final String ELECTION_NAMESPACE = "/election"; private static final String ELECTION_NAMESPACE = "/election";
private String currentZnodeName; private String currentZnodeName;
...@@ -38,19 +43,22 @@ public class LeaderElection implements Watcher { ...@@ -38,19 +43,22 @@ public class LeaderElection implements Watcher {
String smallestChild = children.get(0); //the first element String smallestChild = children.get(0); //the first element
if (smallestChild.equals(currentZnodeName)) { if (smallestChild.equals(currentZnodeName)) {
System.out.println("I'm a leader"); // System.out.println("I'm a leader");
logger.info("I'm a leader");
onElectionCallback.onElectedToBeLeader(); onElectionCallback.onElectedToBeLeader();
return; return;
} else { } else {
System.out.println("I'm not a leader"); System.out.println("I'm not a leader");
logger.info("I'm not a leader");
int predecessorIndex = children.indexOf(currentZnodeName) - 1; int predecessorIndex = children.indexOf(currentZnodeName) - 1;
predecessorName = children.get(predecessorIndex); predecessorName = children.get(predecessorIndex);
predecessorStat = zooKeeper.exists(ELECTION_NAMESPACE + "/" + predecessorName, this); predecessorStat = zooKeeper.exists(ELECTION_NAMESPACE + "/" + predecessorName, this);
} }
} }
onElectionCallback.onWorker(); onElectionCallback.onWorker();
System.out.println("Watching znode " + predecessorName); // System.out.println("Watching znode " + predecessorName);
logger.info("Watching znode {}", predecessorName);
System.out.println(); System.out.println();
} }
......
package Registration_Discovery;
import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.KeeperException;
import java.net.InetAddress; import java.net.InetAddress;
......
package Registration_Discovery;
public interface OnElectionCallback { public interface OnElectionCallback {
void onElectedToBeLeader(); void onElectedToBeLeader();
......
package Registration_Discovery;
import org.apache.zookeeper.*; import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat; import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
public class ServiceRegistry implements Watcher { public class ServiceRegistry implements Watcher {
private static final Logger logger = LoggerFactory.getLogger(ServiceRegistry.class);
private static final String REGISTRY_ZNODE = "/service_registry"; private static final String REGISTRY_ZNODE = "/service_registry";
private final ZooKeeper zooKeeper; private final ZooKeeper zooKeeper;
...@@ -31,12 +38,14 @@ public class ServiceRegistry implements Watcher { ...@@ -31,12 +38,14 @@ public class ServiceRegistry implements Watcher {
public void registerToCluster(String metadata) throws KeeperException, InterruptedException { public void registerToCluster(String metadata) throws KeeperException, InterruptedException {
if (this.currentZnode != null) { if (this.currentZnode != null) {
System.out.println("Already registered to service registry"); // System.out.println("Already registered to service registry");
logger.debug("Already registered to service registry");
return; return;
} }
this.currentZnode = zooKeeper.create(REGISTRY_ZNODE + "/n_", metadata.getBytes(), this.currentZnode = zooKeeper.create(REGISTRY_ZNODE + "/n_", metadata.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("Registered to service registry"); // System.out.println("Registered to service registry");
logger.debug("Registered to service registry");
} }
public void registerForUpdates() { public void registerForUpdates() {
...@@ -79,7 +88,8 @@ public class ServiceRegistry implements Watcher { ...@@ -79,7 +88,8 @@ public class ServiceRegistry implements Watcher {
} }
this.allServiceAddresses = Collections.unmodifiableList(addresses); this.allServiceAddresses = Collections.unmodifiableList(addresses);
System.out.println("The cluster addresses are: " + this.allServiceAddresses); // System.out.println("The cluster addresses are: " + this.allServiceAddresses);
logger.info("The cluster addresses are: {}",this.allServiceAddresses);
} }
@Override @Override
......
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN"> <configuration>
<Appenders> <property name="HOME_LOG" value="logs/app.log"/>
<Console name="Console" target="SYSTEM_OUT"> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/> <layout class="ch.qos.logback.classic.PatternLayout">
</Console> <Pattern>
</Appenders> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
<Loggers> </Pattern>
<Root level="WARN"> </layout>
<AppenderRef ref="Console"/> </appender>
</Root>
</Loggers> <appender name="FILE" class="ch.qos.logback.core.FileAppender">
</Configuration> <file>${HOME_LOG}</file>
\ No newline at end of file <append>true</append>
<immediateFlush>true</immediateFlush>
<encoder>
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
</encoder>
</appender>
<!-- Log Level: ERROR, WARN, INFO, DEBUG, TRACE-->
<logger name="Registration_Discovery" level="DEBUG" additivity="false">
<appender-ref ref="CONSOLE"/>
<appender-ref ref="FILE"/>
</logger>
<root level="error">
<appender-ref ref="CONSOLE"/>
</root>
</configuration>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment