Fix replication_log no longer logging after plugin reload

ReplicationLogFile uses the LifecycleListener start and stop methods to
create and remove the file appender. The issue is that gerrit load the
new plugin and then unload the old one when a plugin is reloaded so
because loggers are static, the unload of the old plugin was removing
the appender just created by the new plugin.

Only remove the appender when the stop method is called during a server
shutdown and also only create the appender the first time the start
method is called.

Change-Id: I4e2f3a7d966acf3f6549bca9b14457805329982a
(cherry picked from commit 52e48e3ce1eea6f24926dd6a2928bb0bb080e3f3)
diff --git a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
index aeb0519..1079981 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/replication/ReplicationLogFile.java
@@ -15,6 +15,7 @@
 package com.googlesource.gerrit.plugins.replication;
 
 import com.google.gerrit.extensions.events.LifecycleListener;
+import com.google.gerrit.extensions.systemstatus.ServerInformation;
 import com.google.gerrit.server.util.SystemLog;
 import com.google.inject.Inject;
 
@@ -25,25 +26,40 @@
 public class ReplicationLogFile implements LifecycleListener {
 
   private final SystemLog systemLog;
+  private final ServerInformation serverInfo;
+  private static boolean started;
 
   @Inject
-  public ReplicationLogFile(final SystemLog systemLog) {
+  public ReplicationLogFile(final SystemLog systemLog,
+      ServerInformation serverInfo) {
     this.systemLog = systemLog;
+    this.serverInfo = serverInfo;
   }
 
   @Override
   public void start() {
-    Logger replicationLogger =
-        LogManager.getLogger(ReplicationQueue.REPLICATION_LOG_NAME);
-    replicationLogger.removeAllAppenders();
-    replicationLogger.addAppender(systemLog.createAsyncAppender(
-        replicationLogger.getName(), new PatternLayout("[%d] [%X{"
-            + PushOne.ID_MDC_KEY + "}] %m%n")));
-    replicationLogger.setAdditivity(false);
+    if (!started) {
+      Logger replicationLogger =
+          LogManager.getLogger(ReplicationQueue.REPLICATION_LOG_NAME);
+      replicationLogger.removeAllAppenders();
+      replicationLogger.addAppender(systemLog.createAsyncAppender(
+          replicationLogger.getName(), new PatternLayout("[%d] [%X{"
+              + PushOne.ID_MDC_KEY + "}] %m%n")));
+      replicationLogger.setAdditivity(false);
+      started = true;
+    }
   }
 
   @Override
   public void stop() {
-    LogManager.getLogger(ReplicationQueue.repLog.getName()).removeAllAppenders();
+    // stop is called when plugin is unloaded or when the server shutdown.
+    // Only clean up when the server is shutting down to prevent issue when a
+    // plugin is reloaded. The issue is that gerrit load the new plugin and then
+    // unload the old one so because loggers are static, the unload of the old
+    // plugin would remove the appenders just created by the new plugin.
+    if (serverInfo.getState() == ServerInformation.State.SHUTDOWN) {
+      LogManager.getLogger(ReplicationQueue.repLog.getName())
+          .removeAllAppenders();
+    }
   }
 }