Line data Source code
1 : // Copyright (C) 2018 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.pgm.init; 16 : 17 : import com.google.gerrit.pgm.init.api.InitStep; 18 : import com.google.gerrit.pgm.init.api.Section; 19 : import com.google.inject.Inject; 20 : import java.util.ArrayList; 21 : import java.util.Arrays; 22 : import java.util.List; 23 : 24 : public class InitLogging implements InitStep { 25 : private static final String CONTAINER = "container"; 26 : private static final String JAVA_OPTIONS = "javaOptions"; 27 : private static final String FLOGGER_BACKEND_PROPERTY = "flogger.backend_factory"; 28 : private static final String FLOGGER_LOGGING_CONTEXT = "flogger.logging_context"; 29 : 30 : private final Section container; 31 : 32 : @Inject 33 15 : public InitLogging(Section.Factory sections) { 34 15 : this.container = sections.get(CONTAINER, null); 35 15 : } 36 : 37 : @Override 38 : public void run() throws Exception { 39 15 : List<String> javaOptions = new ArrayList<>(Arrays.asList(container.getList(JAVA_OPTIONS))); 40 15 : if (!isSet(javaOptions, FLOGGER_BACKEND_PROPERTY)) { 41 15 : javaOptions.add( 42 15 : getJavaOption( 43 : FLOGGER_BACKEND_PROPERTY, 44 : "com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance")); 45 : } 46 15 : if (!isSet(javaOptions, FLOGGER_LOGGING_CONTEXT)) { 47 15 : javaOptions.add( 48 15 : getJavaOption( 49 : FLOGGER_LOGGING_CONTEXT, 50 : "com.google.gerrit.server.logging.LoggingContext#getInstance")); 51 : } 52 15 : container.setList(JAVA_OPTIONS, javaOptions); 53 15 : } 54 : 55 : private static boolean isSet(List<String> javaOptions, String javaOptionName) { 56 15 : return javaOptions.stream() 57 15 : .anyMatch( 58 : o -> 59 15 : o.startsWith("-D" + javaOptionName + "=") 60 15 : || o.startsWith("\"-D" + javaOptionName + "=")); 61 : } 62 : 63 : private static String getJavaOption(String javaOptionName, String value) { 64 15 : return String.format("-D%s=%s", javaOptionName, value); 65 : } 66 : }