GitHub user paladox added a comment to the discussion: Help with migrating from 
log4j to log4j2 (we also use a slf4j -> log4j)

For some reason doing:

```
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package com.google.gerrit.sshd.commands;

import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;

import com.google.gerrit.common.data.GlobalCapability;
import com.google.gerrit.extensions.annotations.RequiresCapability;
import com.google.gerrit.sshd.CommandMetaData;
import com.google.gerrit.sshd.SshCommand;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.config.LoggerConfig;
import org.kohsuke.args4j.Argument;

@RequiresCapability(GlobalCapability.ADMINISTRATE_SERVER)
@CommandMetaData(
    name = "set-level",
    description = "Change the level of loggers",
    runsAt = MASTER_OR_SLAVE)
public class SetLoggingLevelCommand extends SshCommand {

  private enum LevelOption {
    ALL,
    TRACE,
    DEBUG,
    INFO,
    WARN,
    ERROR,
    FATAL,
    OFF,
    RESET
  }

  private static final Map<String, Level> ORIGINAL_LEVELS = new HashMap<>();
  private static final Set<String> ORIGINAL_LOGGER_NAMES = new HashSet<>();
  private static Level ORIGINAL_ROOT_LEVEL = null;
  private static boolean initialized = false;

  @Argument(index = 0, required = true, metaVar = "LEVEL", usage = "logging 
level to set to")
  private LevelOption level;

  @Argument(index = 1, required = false, metaVar = "NAME", usage = "used to 
match loggers")
  private String name;

  @Override
  protected void run() {
    enableGracefulStop();

    if (!initialized) {
      copyOriginalLevels();
      initialized = true;
    }

    if (level == LevelOption.RESET) {
      reset();
      return;
    }

    final Level newLevel;
    try {
      newLevel = Level.valueOf(level.name());
    } catch (IllegalArgumentException e) {
      stderr.println("Unknown logging level: " + level);
      return;
    }

    //LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    //org.apache.logging.log4j.core.config.Configuration config = 
ctx.getConfiguration();

    /*for (Map.Entry<String, LoggerConfig> entry : 
config.getLoggers().entrySet()) {
      String loggerName = entry.getKey();
      if (loggerName.isEmpty()) continue;
      if (name == null || loggerName.contains(name)) {
        entry.getValue().setLevel(newLevel);
      }
    }*/

    org.apache.logging.log4j.core.config.Configurator.setLevel(name, newLevel);
    /*ctx.getLoggerRegistry()
        .getLoggers()
        .forEach(
            logger -> {
              String loggerName = logger.getName();
              if (loggerName.isEmpty()) return;
              if (name == null || loggerName.contains(name)) {
                
org.apache.logging.log4j.core.config.Configurator.setLevel(loggerName, 
newLevel);
                /*LoggerConfig lc = config.getLoggerConfig(loggerName);
                if (!lc.getName().equals(loggerName)) {
                  LoggerConfig newLc = new LoggerConfig(loggerName, newLevel, 
true);
                  config.addLogger(loggerName, newLc);
                } else {
                  lc.setLevel(newLevel);
                }*/
              //}
            //});*/

    //ctx.updateLoggers();
  }

  private static void copyOriginalLevels() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    org.apache.logging.log4j.core.config.Configuration config = 
ctx.getConfiguration();

    ORIGINAL_ROOT_LEVEL = config.getRootLogger().getLevel();

    for (Map.Entry<String, LoggerConfig> e : config.getLoggers().entrySet()) {
      String loggerName = e.getKey();
      ORIGINAL_LOGGER_NAMES.add(loggerName);
      ORIGINAL_LEVELS.put(loggerName, e.getValue().getLevel());
    }
  }

  private static void reset() {
    LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
    org.apache.logging.log4j.core.config.Configuration config = 
ctx.getConfiguration();

    for (LoggerConfig lc : config.getLoggers().values()) {
      lc.setLevel(null);
    }
    if (ORIGINAL_ROOT_LEVEL != null) {
      config.getRootLogger().setLevel(null);
    }

    for (Map.Entry<String, Level> e : ORIGINAL_LEVELS.entrySet()) {
      LoggerConfig lc = config.getLoggers().get(e.getKey());
      if (lc != null) {
        lc.setLevel(e.getValue());
      }
    }
    if (ORIGINAL_ROOT_LEVEL != null) {
      config.getRootLogger().setLevel(ORIGINAL_ROOT_LEVEL);
    }

    ctx.updateLoggers();
  }
}

```

And then setting debug to all, doesn't apply it to the log4j 1.x / slf4j 
loggers but does if you specify a name. Any ideas @ppkarwasz ? 

Another question is reset. With log4j 1.x we did:

```

  private static void reset() throws MalformedURLException {
    for (Logger logger : getCurrentLoggers()) {
      logger.setLevel(null);
    }

    String path = System.getProperty(JAVA_OPTIONS_LOG_CONFIG);
    if (Strings.isNullOrEmpty(path)) {
      PropertyConfigurator.configure(Loader.getResource(LOG_CONFIGURATION));
    } else {
      PropertyConfigurator.configure(URI.create(path).toURL());
    }
  }

  @SuppressWarnings({"unchecked", "JdkObsolete"})
  private static ImmutableList<Logger> getCurrentLoggers() {
    return 
ImmutableList.copyOf(Iterators.forEnumeration(LogManager.getCurrentLoggers()));
  }
```

which when I convert to log4j 2 doesn't really work properly. Setting the log 
level to null, causes all the loggers to be INFO and not the original. Unless 
you have to store an original in the Map now? (I found a workaround per the 
above code, just was wondering if was doing anything wrong, as preferably I 
don't really want to store a map of the original loggers).

GitHub link: 
https://github.com/apache/logging-log4j2/discussions/3914#discussioncomment-14308470

----
This is an automatically sent email for dev@logging.apache.org.
To unsubscribe, please send an email to: dev-unsubscr...@logging.apache.org

Reply via email to