Reputation: 902
I am looking for a way to change the log level of one or multiple classes/packages of a Quarkus app (JVM) during runtime. Is there an API I can use to programmatically change the levels, e.g. by exposing a REST API or does there already exist some other solution?
I am aware of https://quarkus.io/guides/logging but this only discusses changing the log levels statically via a JVM property or applications.properties.
Upvotes: 13
Views: 4699
Reputation: 4747
For future readers: I created a Quarkus extension that can be seen here: https://github.com/quarkiverse/quarkiverse-logging-ui
And can be used by adding this dependency
<dependency>
<groupId>io.quarkiverse.loggingui</groupId>
<artifactId>quarkus-logging-ui</artifactId>
<version>0.0.2</version>
</dependency>
Upvotes: 6
Reputation: 902
Apparently Quarkus uses java.util.logging
under the hood, so I created a simple REST resource like this:
import javax.ws.rs.*;
import java.util.logging.*;
@Path("/logging")
public class LoggingResource {
private static Level getLogLevel(Logger logger) {
for (Logger current = logger; current != null;) {
Level level = current.getLevel();
if (level != null)
return level;
current = current.getParent();
}
return Level.INFO;
}
@GET
@Path("/{logger}")
@Produces("text/plain")
public String logger(@PathParam("logger") String loggerName, @QueryParam("level") String level) {
// get the logger instance
Logger logger = Logger.getLogger(loggerName);
// change the log-level if requested
if (level != null && level.length() > 0)
logger.setLevel(Level.parse(level));
// return the current log-level
return getLogLevel(logger);
}
}
Now I can get the current log level like this:
curl http://myserver:8080/logging/com.example.mypackage
And set the log level like this:
curl http://myserver:8080/logging/com.example.mypackage?level=DEBUG
Upvotes: 8