-
Could I have some help to migrate:
please? I've come up with:
but it doesn't work properly. I'm not seeing the log level take effect, if the logger was created under slf4j (the logger will also only return under E.g. I'm trying to set I migrated:
to
which lists the loggers similar to log4j. I just can't get SetLoggingLevelCommand to work properly. |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments 23 replies
-
If I use:
It doesn't change the logger at all. When in set debug, it sticks at INFO. |
Beta Was this translation helpful? Give feedback.
-
https://gerrit-review.googlesource.com/c/gerrit/+/259202/ is my change for updating to log4j2. |
Beta Was this translation helpful? Give feedback.
-
I think I may have figured it out. It's due to |
Beta Was this translation helpful? Give feedback.
-
Hi @paladox, The only supported API for changing log levels at runtime in Log4j Core is Configurator.setLevel(name, newLevel); This works only if Log4j Core is the actual implementation of the Log4j API. Since that might not always be the case, it’s good practice to guard the call: try {
Configurator.setLevel(name, newLevel);
} catch (LinkageError | ClassCastException e) {
// Log4j Core is not available or not the active implementation
} What this does:
About your current codeconfig.getLoggers().forEach((loggerName, loggerConfig) -> {
if (!loggerName.isEmpty() && (name == null || loggerName.contains(name))) {
loggerConfig.setLevel(newLevel);
stdout.println("Updated Log4j2 LoggerConfig: " + loggerName + " -> " + newLevel);
}
});
ctx.updateLoggers(); This approach differs from
For example, if
Adjusting whole hierarchiesIf you want to update an entire logger hierarchy, try {
Configurator.setAllLevels(name, newLevel);
} catch (LinkageError | ClassCastException e) {
// Log4j Core is not available or not the active implementation
} Key difference from Log4j 1This is probably the root cause of your migration issues:
are created. So, to affect loggers that don’t have an explicit configuration, you need to use the Note A Logger Admin API is planned (see apache/logging-admin) to provide a standard, implementation-independent way of managing logger levels. However, development capacity is currently limited, so this is not yet available. |
Beta Was this translation helpful? Give feedback.
-
For some reason doing:
And then setting debug to all, doesn't apply it to the log4j 1.x / slf4j loggers. Any ideas @ppkarwasz ? Another question is reset. With log4j 1.x we did:
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). |
Beta Was this translation helpful? Give feedback.
-
@ppkarwasz do you know why the following won't work properly: https://gist.github.com/paladox/149db37be61a1f0f412b96d7c140a18e. It causes The original code is https://github.com/GerritCodeReview/gerrit/blob/master/java/com/google/gerrit/sshd/SshLog.java. |
Beta Was this translation helpful? Give feedback.
Log4j 1.x didn’t have a separate
C…