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

logging - file appender

parent fc503baf
......@@ -2,6 +2,7 @@
*topics*
* [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
* Implement Service Registry with Zookeeper
* 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.WatchedEvent;
import org.apache.zookeeper.Watcher;
......@@ -9,6 +11,8 @@ import java.io.IOException;
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 int SESSION_TIMEOUT = 3000; //dead client
private static final int DEFAULT_PORT = 8080;
......@@ -20,6 +24,8 @@ public class Application implements Watcher {
Application application = new Application();
ZooKeeper zooKeeper = application.connectToZookeeper();
logger.info("Connected");
ServiceRegistry serviceRegistry = new ServiceRegistry(zooKeeper);
OnElectionAction onElectionAction = new OnElectionAction(serviceRegistry, currentServerPort);
......@@ -31,6 +37,7 @@ public class Application implements Watcher {
application.run();
application.close();
}
public ZooKeeper connectToZookeeper() throws IOException {
......@@ -53,14 +60,17 @@ public class Application implements Watcher {
switch (watchedEvent.getType()) {
case None:
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) {
synchronized (zooKeeper) {
System.out.println("Disconnected from Zookeeper");
// System.out.println("Disconnected from Zookeeper");
logger.debug("Disconnected from Zookeeper");
zooKeeper.notifyAll();
}
} else if (watchedEvent.getState() == Event.KeeperState.Closed) {
System.out.println("Closed Successfully");
// System.out.println("Closed Successfully");
logger.debug("Closed Successfully");
}
break;
}
......
package Registration_Discovery;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
public class LeaderElection implements Watcher {
private static final Logger logger = LoggerFactory.getLogger(LeaderElection.class);
private static final String ELECTION_NAMESPACE = "/election";
private String currentZnodeName;
......@@ -38,19 +43,22 @@ public class LeaderElection implements Watcher {
String smallestChild = children.get(0); //the first element
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();
return;
} else {
System.out.println("I'm not a leader");
logger.info("I'm not a leader");
int predecessorIndex = children.indexOf(currentZnodeName) - 1;
predecessorName = children.get(predecessorIndex);
predecessorStat = zooKeeper.exists(ELECTION_NAMESPACE + "/" + predecessorName, this);
}
}
onElectionCallback.onWorker();
System.out.println("Watching znode " + predecessorName);
// System.out.println("Watching znode " + predecessorName);
logger.info("Watching znode {}", predecessorName);
System.out.println();
}
......
package Registration_Discovery;
import org.apache.zookeeper.KeeperException;
import java.net.InetAddress;
......
package Registration_Discovery;
public interface OnElectionCallback {
void onElectedToBeLeader();
......
package Registration_Discovery;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class ServiceRegistry implements Watcher {
private static final Logger logger = LoggerFactory.getLogger(ServiceRegistry.class);
private static final String REGISTRY_ZNODE = "/service_registry";
private final ZooKeeper zooKeeper;
......@@ -31,12 +38,14 @@ public class ServiceRegistry implements Watcher {
public void registerToCluster(String metadata) throws KeeperException, InterruptedException {
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;
}
this.currentZnode = zooKeeper.create(REGISTRY_ZNODE + "/n_", metadata.getBytes(),
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() {
......@@ -79,7 +88,8 @@ public class ServiceRegistry implements Watcher {
}
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
......
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="WARN">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
\ No newline at end of file
<configuration>
<property name="HOME_LOG" value="logs/app.log"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
<file>${HOME_LOG}</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