LCOV - code coverage report
Current view: top level - server/util - PluginLogFile.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 16 17 94.1 %
Date: 2022-11-19 15:00:39 Functions: 3 3 100.0 %

          Line data    Source code
       1             : // Copyright (C) 2015 The Android Open Source Project
       2             : //
       3             : // Licensed under the Apache License, Version 2.0 (the "License");
       4             : // you may not use this file except in compliance with the License.
       5             : // You may obtain a copy of the License at
       6             : //
       7             : // http://www.apache.org/licenses/LICENSE-2.0
       8             : //
       9             : // Unless required by applicable law or agreed to in writing, software
      10             : // distributed under the License is distributed on an "AS IS" BASIS,
      11             : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      12             : // See the License for the specific language governing permissions and
      13             : // limitations under the License.
      14             : 
      15             : package com.google.gerrit.server.util;
      16             : 
      17             : import com.google.gerrit.extensions.events.LifecycleListener;
      18             : import com.google.gerrit.extensions.systemstatus.ServerInformation;
      19             : import org.apache.log4j.Layout;
      20             : import org.apache.log4j.LogManager;
      21             : import org.apache.log4j.Logger;
      22             : 
      23             : public abstract class PluginLogFile implements LifecycleListener {
      24             : 
      25             :   private final SystemLog systemLog;
      26             :   private final ServerInformation serverInfo;
      27             :   private final String logName;
      28             :   private final Layout layout;
      29             : 
      30             :   public PluginLogFile(
      31           1 :       SystemLog systemLog, ServerInformation serverInfo, String logName, Layout layout) {
      32           1 :     this.systemLog = systemLog;
      33           1 :     this.serverInfo = serverInfo;
      34           1 :     this.logName = logName;
      35           1 :     this.layout = layout;
      36           1 :   }
      37             : 
      38             :   @Override
      39             :   public void start() {
      40           1 :     Logger logger = LogManager.getLogger(logName);
      41           1 :     if (logger.getAppender(logName) == null) {
      42           1 :       synchronized (systemLog) {
      43           1 :         if (logger.getAppender(logName) == null) {
      44           1 :           logger.addAppender(systemLog.createAsyncAppender(logName, layout, true, true));
      45             :         }
      46           1 :       }
      47             :     }
      48           1 :     logger.setAdditivity(false);
      49           1 :   }
      50             : 
      51             :   @Override
      52             :   public void stop() {
      53             :     // stop is called when plugin is unloaded or when the server shutdown.
      54             :     // Only clean up when the server is shutting down to prevent issue when a
      55             :     // plugin is reloaded. The issue is that gerrit load the new plugin and then
      56             :     // unload the old one so because loggers are static, the unload of the old
      57             :     // plugin would remove the appenders just created by the new plugin.
      58           1 :     if (serverInfo.getState() == ServerInformation.State.SHUTDOWN) {
      59           0 :       LogManager.getLogger(logName).removeAllAppenders();
      60             :     }
      61           1 :   }
      62             : }

Generated by: LCOV version 1.16+git.20220603.dfeb750