Line data Source code
1 : // Copyright (C) 2020 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.testing; 16 : 17 : import com.google.common.base.Strings; 18 : import com.google.common.collect.ImmutableMap; 19 : import java.util.logging.ConsoleHandler; 20 : import java.util.logging.Level; 21 : import java.util.logging.LogManager; 22 : import java.util.logging.Logger; 23 : 24 : public class TestLoggingActivator { 25 138 : private static final ImmutableMap<String, Level> LOG_LEVELS = 26 138 : ImmutableMap.<String, Level>builder() 27 138 : .put("com.google.gerrit", getGerritLogLevel()) 28 : 29 : // Silence non-critical messages from MINA SSHD. 30 138 : .put("org.apache.mina", Level.WARNING) 31 138 : .put("org.apache.sshd.client", Level.WARNING) 32 138 : .put("org.apache.sshd.common", Level.WARNING) 33 138 : .put("org.apache.sshd.server", Level.WARNING) 34 138 : .put("org.apache.sshd.common.keyprovider.FileKeyPairProvider", Level.INFO) 35 138 : .put("com.google.gerrit.sshd.GerritServerSession", Level.WARNING) 36 : 37 : // Silence non-critical messages from mime-util. 38 138 : .put("eu.medsea.mimeutil", Level.WARNING) 39 : 40 : // Silence non-critical messages from openid4java. 41 138 : .put("org.apache.xml", Level.WARNING) 42 138 : .put("org.openid4java", Level.WARNING) 43 138 : .put("org.openid4java.consumer.ConsumerManager", Level.SEVERE) 44 138 : .put("org.openid4java.discovery.Discovery", Level.SEVERE) 45 138 : .put("org.openid4java.server.RealmVerifier", Level.SEVERE) 46 138 : .put("org.openid4java.message.AuthSuccess", Level.SEVERE) 47 : 48 : // Silence non-critical messages from apache.http. 49 138 : .put("org.apache.http", Level.WARNING) 50 : 51 : // Silence non-critical messages from Jetty. 52 138 : .put("org.eclipse.jetty", Level.WARNING) 53 : 54 : // Silence non-critical messages from JGit. 55 138 : .put("org.eclipse.jgit.transport.PacketLineIn", Level.WARNING) 56 138 : .put("org.eclipse.jgit.transport.PacketLineOut", Level.WARNING) 57 138 : .put("org.eclipse.jgit.internal.transport.sshd", Level.WARNING) 58 138 : .put("org.eclipse.jgit.util.FileUtils", Level.WARNING) 59 138 : .put("org.eclipse.jgit.internal.storage.file.FileSnapshot", Level.WARNING) 60 138 : .put("org.eclipse.jgit.util.FS", Level.WARNING) 61 138 : .put("org.eclipse.jgit.util.SystemReader", Level.WARNING) 62 138 : .build(); 63 : 64 : private static Level getGerritLogLevel() { 65 138 : String value = Strings.nullToEmpty(System.getenv("GERRIT_LOG_LEVEL")); 66 138 : if (value.isEmpty()) { 67 138 : value = Strings.nullToEmpty(System.getProperty("gerrit.logLevel")); 68 : } 69 : 70 : try { 71 0 : return Level.parse(value); 72 138 : } catch (IllegalArgumentException e) { 73 : // for backwards compatibility handle log4j log levels 74 138 : if (value.equalsIgnoreCase("FATAL") || value.equalsIgnoreCase("ERROR")) { 75 0 : return Level.SEVERE; 76 : } 77 138 : if (value.equalsIgnoreCase("WARN")) { 78 0 : return Level.WARNING; 79 : } 80 138 : if (value.equalsIgnoreCase("DEBUG")) { 81 0 : return Level.FINE; 82 : } 83 138 : if (value.equalsIgnoreCase("TRACE")) { 84 0 : return Level.FINEST; 85 : } 86 : 87 138 : return Level.INFO; 88 : } 89 : } 90 : 91 : public static void configureLogging() { 92 138 : LogManager.getLogManager().reset(); 93 138 : FloggerInitializer.initBackend(); 94 : 95 138 : ConsoleHandler dst = new ConsoleHandler(); 96 138 : dst.setLevel(Level.FINEST); 97 : 98 138 : Logger.getLogger(Logger.GLOBAL_LOGGER_NAME).addHandler(dst); 99 : 100 138 : LOG_LEVELS.entrySet().stream() 101 138 : .forEach( 102 : e -> { 103 138 : Logger logger = Logger.getLogger(e.getKey()); 104 138 : logger.setLevel(e.getValue()); 105 138 : logger.addHandler(dst); 106 138 : }); 107 138 : } 108 : 109 : private TestLoggingActivator() {} 110 : }