LCOV - code coverage report
Current view: top level - sshd/commands - SetLoggingLevelCommand.java (source / functions) Hit Total Coverage
Test: _coverage_report.dat Lines: 11 28 39.3 %
Date: 2022-11-19 15:00:39 Functions: 2 5 40.0 %

          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.common.base.Strings;
      20             : import com.google.gerrit.common.data.GlobalCapability;
      21             : import com.google.gerrit.extensions.annotations.RequiresCapability;
      22             : import com.google.gerrit.sshd.CommandMetaData;
      23             : import com.google.gerrit.sshd.SshCommand;
      24             : import java.net.MalformedURLException;
      25             : import java.net.URL;
      26             : import java.util.Collections;
      27             : import org.apache.log4j.Level;
      28             : import org.apache.log4j.LogManager;
      29             : import org.apache.log4j.Logger;
      30             : import org.apache.log4j.PropertyConfigurator;
      31             : import org.apache.log4j.helpers.Loader;
      32             : import org.kohsuke.args4j.Argument;
      33             : 
      34             : @RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
      35             : @CommandMetaData(
      36             :     name = "set-level",
      37             :     description = "Change the level of loggers",
      38             :     runsAt = MASTER_OR_SLAVE)
      39           1 : public class SetLoggingLevelCommand extends SshCommand {
      40             :   private static final String LOG_CONFIGURATION = "log4j.properties";
      41             :   private static final String JAVA_OPTIONS_LOG_CONFIG = "log4j.configuration";
      42             : 
      43           1 :   private enum LevelOption {
      44           1 :     ALL,
      45           1 :     TRACE,
      46           1 :     DEBUG,
      47           1 :     INFO,
      48           1 :     WARN,
      49           1 :     ERROR,
      50           1 :     FATAL,
      51           1 :     OFF,
      52           1 :     RESET,
      53             :   }
      54             : 
      55             :   @Argument(index = 0, required = true, metaVar = "LEVEL", usage = "logging level to set to")
      56             :   private LevelOption level;
      57             : 
      58             :   @Argument(index = 1, required = false, metaVar = "NAME", usage = "used to match loggers")
      59             :   private String name;
      60             : 
      61             :   @Override
      62             :   protected void run() throws MalformedURLException {
      63           0 :     enableGracefulStop();
      64           0 :     if (level == LevelOption.RESET) {
      65           0 :       reset();
      66             :     } else {
      67           0 :       for (Logger logger : getCurrentLoggers()) {
      68           0 :         if (name == null || logger.getName().contains(name)) {
      69           0 :           logger.setLevel(Level.toLevel(level.name()));
      70             :         }
      71           0 :       }
      72             :     }
      73           0 :   }
      74             : 
      75             :   private static void reset() throws MalformedURLException {
      76           0 :     for (Logger logger : getCurrentLoggers()) {
      77           0 :       logger.setLevel(null);
      78           0 :     }
      79             : 
      80           0 :     String path = System.getProperty(JAVA_OPTIONS_LOG_CONFIG);
      81           0 :     if (Strings.isNullOrEmpty(path)) {
      82           0 :       PropertyConfigurator.configure(Loader.getResource(LOG_CONFIGURATION));
      83             :     } else {
      84           0 :       PropertyConfigurator.configure(new URL(path));
      85             :     }
      86           0 :   }
      87             : 
      88             :   @SuppressWarnings({"unchecked", "JdkObsolete"})
      89             :   private static Iterable<Logger> getCurrentLoggers() {
      90           0 :     return Collections.list(LogManager.getCurrentLoggers());
      91             :   }
      92             : }

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