Line data Source code
1 : // Copyright (C) 2012 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.git; 16 : 17 : import com.google.common.flogger.backend.Platform; 18 : import com.google.gerrit.extensions.events.LifecycleListener; 19 : import com.google.gerrit.server.config.GerritServerConfig; 20 : import com.google.gerrit.server.config.SitePaths; 21 : import com.google.gerrit.server.util.SystemLog; 22 : import com.google.inject.Inject; 23 : import java.nio.file.Path; 24 : import org.apache.log4j.LogManager; 25 : import org.apache.log4j.Logger; 26 : import org.apache.log4j.PatternLayout; 27 : import org.eclipse.jgit.lib.Config; 28 : 29 : public class GarbageCollectionLogFile implements LifecycleListener { 30 : private static final String LOG_NAME = "gc_log"; 31 : 32 : @Inject 33 138 : public GarbageCollectionLogFile(SitePaths sitePaths, @GerritServerConfig Config config) { 34 138 : if (SystemLog.shouldConfigure()) { 35 8 : initLogSystem(sitePaths.logs_dir, config.getBoolean("log", "rotate", true)); 36 : } 37 138 : } 38 : 39 : @Override 40 138 : public void start() {} 41 : 42 : @Override 43 : public void stop() { 44 138 : getLogger(GarbageCollection.class).removeAllAppenders(); 45 138 : getLogger(GarbageCollectionRunner.class).removeAllAppenders(); 46 138 : } 47 : 48 : private static void initLogSystem(Path logdir, boolean rotate) { 49 8 : initGcLogger(logdir, rotate, getLogger(GarbageCollection.class)); 50 8 : initGcLogger(logdir, rotate, getLogger(GarbageCollectionRunner.class)); 51 8 : } 52 : 53 : private static Logger getLogger(Class<?> clazz) { 54 138 : return LogManager.getLogger(Platform.getBackend(clazz.getName()).getLoggerName()); 55 : } 56 : 57 : private static void initGcLogger(Path logdir, boolean rotate, Logger gcLogger) { 58 8 : gcLogger.removeAllAppenders(); 59 8 : gcLogger.addAppender( 60 8 : SystemLog.createAppender( 61 : logdir, LOG_NAME, new PatternLayout("[%d] %-5p %x: %m%n"), rotate)); 62 8 : gcLogger.setAdditivity(false); 63 8 : } 64 : }