Line data Source code
1 : // Copyright (C) 2014 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.sshd.commands; 16 : 17 : import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE; 18 : 19 : import com.google.gerrit.common.data.GlobalCapability; 20 : import com.google.gerrit.extensions.annotations.RequiresCapability; 21 : import com.google.gerrit.sshd.CommandMetaData; 22 : import com.google.gerrit.sshd.SshCommand; 23 : import java.util.Collections; 24 : import java.util.Map; 25 : import java.util.TreeMap; 26 : import org.apache.log4j.LogManager; 27 : import org.apache.log4j.Logger; 28 : import org.kohsuke.args4j.Argument; 29 : 30 : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER) 31 : @CommandMetaData( 32 : name = "ls-level", 33 : description = "list the level of loggers", 34 : runsAt = MASTER_OR_SLAVE) 35 1 : public class ListLoggingLevelCommand extends SshCommand { 36 : 37 : @Argument(index = 0, required = false, metaVar = "NAME", usage = "used to match loggers") 38 : private String name; 39 : 40 : @Override 41 : protected void run() { 42 0 : enableGracefulStop(); 43 0 : Map<String, String> logs = new TreeMap<>(); 44 0 : for (Logger logger : getCurrentLoggers()) { 45 0 : if (name == null || logger.getName().contains(name)) { 46 0 : logs.put(logger.getName(), logger.getEffectiveLevel().toString()); 47 : } 48 0 : } 49 0 : for (Map.Entry<String, String> e : logs.entrySet()) { 50 0 : stdout.println(e.getKey() + ": " + e.getValue()); 51 0 : } 52 0 : } 53 : 54 : @SuppressWarnings({"unchecked", "JdkObsolete"}) 55 : private static Iterable<Logger> getCurrentLoggers() { 56 0 : return Collections.list(LogManager.getCurrentLoggers()); 57 : } 58 : }