GitHub user paladox added a comment to the discussion: Help with migrating from
log4j to log4j2 (we also use a slf4j -> log4j)
If I use:
```
package com.google.gerrit.sshd.commands;
import static com.google.gerrit.sshd.CommandMetaData.Mode.MASTER_OR_SLAVE;
import com.google.common.collect.ImmutableList;
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.Logger;
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 existing loggers",
runsAt = MASTER_OR_SLAVE)
public class SetLoggingLevelCommand extends SshCommand {
public enum LevelOption {
ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF, RESET
}
private static final Map<String, Level> ORIGINAL_LOG4J2_LEVELS = new
HashMap<>();
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) {
snapshotOriginalLevels();
initialized = true;
}
if (level == LevelOption.RESET) {
resetToSnapshot();
return;
}
Level newLevel;
try {
newLevel = Level.valueOf(level.name());
} catch (IllegalArgumentException e) {
stderr.println("Unknown logging level: " + level);
return;
}
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
// Update all matching loggers
for (Logger logger : getAllLoggers()) {
if (name == null || logger.getName().contains(name)) {
logger.setLevel(newLevel);
stdout.println("Updated logger: " + logger.getName() + " -> " +
newLevel);
}
}
// Optionally update root logger
if (name == null || name.isEmpty()) {
ctx.getConfiguration().getRootLogger().setLevel(newLevel);
}
// Apply changes
ctx.updateLoggers();
}
private static void snapshotOriginalLevels() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
ORIGINAL_ROOT_LEVEL = ctx.getConfiguration().getRootLogger().getLevel();
for (Logger logger : getAllLoggers()) {
ORIGINAL_LOG4J2_LEVELS.put(logger.getName(), logger.getLevel());
}
}
private static void resetToSnapshot() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
for (Logger logger : getAllLoggers()) {
Level original = ORIGINAL_LOG4J2_LEVELS.get(logger.getName());
if (original != null) {
logger.setLevel(original);
}
}
if (ORIGINAL_ROOT_LEVEL != null) {
ctx.getConfiguration().getRootLogger().setLevel(ORIGINAL_ROOT_LEVEL);
}
ctx.updateLoggers();
}
private static ImmutableList<Logger> getAllLoggers() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
Set<String> seenNames = new HashSet<>();
ImmutableList.Builder<Logger> builder = ImmutableList.builder();
// From configuration
for (LoggerConfig lc : ctx.getConfiguration().getLoggers().values()) {
Logger logger = ctx.getLogger(lc.getName());
if (seenNames.add(logger.getName())) {
builder.add(logger);
}
}
// From registry (covers dynamically created loggers)
ctx.getLoggerRegistry()
.getLoggers()
.forEach(
logger -> {
if (seenNames.add(logger.getName())) {
builder.add((Logger) logger);
}
});
return builder.build();
}
}
```
It doesn't change the logger at all. When in set debug, it sticks at INFO.
GitHub link:
https://github.com/apache/logging-log4j2/discussions/3914#discussioncomment-14306852
----
This is an automatically sent email for [email protected].
To unsubscribe, please send an email to: [email protected]