Sure, here's the HTML code for a comprehensive answer to the question of how to change WildFly logging levels programmatically from within a deployed application, formatted as a blog post:
Changing WildFly Logging Levels Programatically
In this blog post, we'll discuss how to change WildFly logging levels programmatically from within a deployed application. While it's generally not recommended to change logging levels programmatically, there may be cases where it's necessary. We'll explore two different approaches for achieving this: using a logging.properties file and using the WildFly client.
Using a logging.properties File
One way to change WildFly logging levels is to create a logging.properties
file and place it in your deployment. This file allows you to configure logging for your application without overriding it via management operations like CLI.
# Additional loggers to configure (the root logger is always configured) loggers=my.logger.name logger.level=INFO logger.handlers=CONSOLE,FILE logger.my.logger.name.level=DEBUG handler.CONSOLE=org.jboss.logmanager.handlers.ConsoleHandler handler.CONSOLE.formatter=COLOR-PATTERN handler.CONSOLE.properties=autoFlush,target handler.CONSOLE.autoFlush=true handler.CONSOLE.target=SYSTEM_OUT handler.FILE=org.jboss.logmanager.handlers.PeriodicRotatingFileHandler handler.FILE.formatter=PATTERN handler.FILE.properties=autoFlush,append,fileName,suffix handler.FILE.constructorProperties=fileName,append handler.FILE.autoFlush=true handler.FILE.append=true handler.FILE.fileName=${jboss.server.log.dir}/my-app.log handler.FILE.suffix=.yyyy-MM-dd formatter.COLOR-PATTERN=org.jboss.logmanager.formatters.PatternFormatter formatter.COLOR-PATTERN.properties=pattern formatter.COLOR-PATTERN.pattern=%K{level}%d{HH\:mm\:ss,SSS} %-5p [%c] (%t) %s%e%n formatter.PATTERN=org.jboss.logmanager.formatters.PatternFormatter formatter.PATTERN.properties=pattern formatter.PATTERN.pattern=%d{yyyy-MM-dd HH\:mm\:ss,SSS} %-5p [%c] (%t) %s%e%n
You can then place this file in your deployment, and every time it's deployed, it will configure a log context for your application that cannot be overridden via management operations.
Using the WildFly Client
Another way to change WildFly logging levels programmatically is to use the WildFly client to execute JBoss CLI commands. This approach gives you more fine-grained control over the logging configuration.
To use the WildFly client, you'll need to add the following dependency to your project:
<dependency> <groupId>org.wildfly.core</groupId> <artifactId>wildfly-controller-client</artifactId> <version>18.1.1.Final</version> </dependency>
Once you've added the dependency, you can use the WildFly client to execute JBoss CLI commands. Here's an example of how to change the root logger level to INFO:
//First we find out the WildFly parameters String hostname = (String) ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:interface=public"), "inet-address"); Integer managementPort = (Integer) ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:socket-binding-group=standard-sockets,socket-binding=management-http"), "port"); Integer portOffset = (Integer) ManagementFactory.getPlatformMBeanServer().getAttribute(new ObjectName("jboss.as:socket-binding-group=standard-sockets"), "port-offset"); //Then we create the client using the parameters obtained above try (ModelControllerClient client = ModelControllerClient.Factory.create(hostname, managementPort+portOffset)) { //Creates the change log level operation ModelNode op = new ModelNode(); op.get("operation").set("change-root-log-level"); //Writes the way to the root logger that will be changed ModelNode addr = op.get("address"); addr.add("subsystem", "logging"); addr.add("logging-profile", "myApp"); //myApp is my logging-profile name, yours will be different! addr.add("root-logger", "ROOT"); //Makes the level change op.get("level").set("INFO"); //Executes the operation ModelNode returnVal = client.execute(op); //If you want, you can log the results if(logger.isInfoEnabled()) { logger.info("Execution results:"); logger.info("Outcome: {}", returnVal.get("outcome")); logger.info("Result: {}", returnVal.get("result")); } }
Using the WildFly client is a more flexible approach that allows you to change the logging level for specific loggers or even create new loggers.
I hope this blog post has been helpful in understanding how to change WildFly logging levels programmatically. If you have any questions, please feel free to leave a comment below.