Logback Chapter 10: JMX Configurator

Posted by jwright on Fri, 04 Mar 2022 10:56:07 +0100

As the name suggests, JMX configurator allows you to configure logback through JMX. Simply put, it allows you to reconfigure the logback from the default configuration file, specified file or URL, list the logger and modify the logger level.

Using JMX Configurator

If your is running JDK 1.6 or later, you just need to call jconsole on the command line and connect to MBeanServer on your server. If you are running on an older JVM, you need to check Using JMX on services.

Only one line is required to open JMX configurator in the configuration file. As follows:

<configuration>
  <jmxConfigurator />
  
  <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>%date [%thread] %-5level %logger{25} - %msg%n</Pattern>
    </layout>
  </appender>

  <root level="debug">
    <appender-ref ref="console" />
  </root>  
</configuration>

After you connect to the server through jconsole, on the MBeans panel, you can see several options under the "ch.qos.logback.classic.jmx.Configurator" folder. As shown in the figure below:

View a screenshot of JMX configurator in jconsole

So you can

  • Reload the configuration of logback using the default configuration file
  • Reload the configuration with the specified URL
  • Reload the configuration from the specified file
  • Sets the level of the specified logger. If you want to set it to null, just pass the "null" string
  • Gets the level of the specified logger. The return value can be null
  • Or specify the of the logger Effective level

JMX configurator presents the existing logger and status as attributes.

This status list can help you diagnose the internal status of the logger.

Avoid memory leaks

If your application is deployed on a web server or application server, the registered JMX configurator instance will create a reference from the system class loader to your application. When the application stops or redeploys, it will prevent garbage collection, which will lead to memory leakage.

Therefore, unless your application is a stand-alone Java application, you must unregister the JMXConfigurator instance from the Mbeans service of the JVM. Calling the reset() method through LoggerContext will automatically unregister any JMXConfigurator instance. A good way to reset the logger context is through javax servlet. The contextDestroyed() method in servletcontextlistener. The example code is as follows:

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.LoggerContext;

public class MyContextListener implements ServletContextListener {

  public void contextDestroyed(ServletContextEvent sce) {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    lc.stop();
  }

  public void contextInitialized(ServletContextEvent sce) {
  }
} 

JMX configurator and multiple web applications

If you deploy multiple web applications on the same server and you don't override the default contextual selector , and you put logback - * Jar and slf4j API Jar is placed in the WEB-INF/lib folder of each web application. After that, by default, each JMX configurator instance will be registered under the same name. That is, "ch.qos.logback.classic:Name=default,Type=ch.qos.logback.classic.jmx.JMXConfigurator". In other words, in each web application, the logger context associated with various JMX configurator instances will conflict by default.

To avoid this unnecessary conflict, you just need to Set the log context of the application , JMX configurator will automatically use the name you set.

For example, if you deploy two web applications named "Koala" and "Wombat", you can write this in Koala's configuration file:

<configuration>
  <contextName>Koala</contextName>
  <jmxConfigurator/>
  ...
<configuration>

In Wombat's configuration file, you can write:

<configuration>
  <contextName>Wombat</contextName>x
  <jmxConfigurator/>
  ...
<configuration>

In the MBeans panel of jconsole, you can see two different JMX configurator instances:

Through the "objectName" attribute of the element, you can fully control the name of the JMX configurator registered in the MBeans service.

Turn on JMX in the server

If your server is running JDK 1.6 or later, JMX is turned on by default.

For older JVM s, we recommend that you refer to the JMX related documentation on the web server you use. These documents are in Tomcat as well as Jetty Can be obtained in. In this document, we will describe the configuration steps related to Tomcat and Jetty in detail.

Start JMX in Jetty (tested in JDK 1.5 and JDK 1.6)

The following has been tested in JDK 1.5 and 1.6. In JDK 1.6 and later versions, your server is enabled by default. You can, but do not need to follow the following discussion. Under JDK 1.5, add JMX support, just in $jetty_ HOME/etc/jetty. Add an additional support to the XML configuration file. Here are the elements to be added:

<Call id="MBeanServer" class="java.lang.management.ManagementFactory" 
      name="getPlatformMBeanServer"/>

<Get id="Container" name="container">
  <Call name="addEventListener">
    <Arg>
      <New class="org.mortbay.management.MBeanContainer">
        <Arg><Ref id="MBeanServer"/></Arg>
        <Call name="start" />
      </New>
    </Arg>
  </Call>
</Get>

If you want to access MBeans in Jetty through jconsole, you need to set the system property "com.sun.management.jmxremote" before starting Jetty.

For the stand-alone version of Jetty, use:

java -Dcom.sun.management.jmxremote -jar start.jar [config files]

If you want to start Jetty as a maven plug-in, you need to use Maven_ The opts shell variable sets the system property "com.sun.management.jmxremote":

MAVEN_OPTS="-Dcom.sun.management.jmxremote"
mvn jetty:run

You can access Jetty's MBeans and logback's JMX configurator through jconsole.

After you connect, you can access JMX configurator, as shown above screenshot Same.

MX4j and Jetty (tested on JDK 1.5 and 1.6)

Suppose you have downloaded it MX4J , if you want to access JMX configurator through the HTTP interface of MX4J, you need to add the configuration discussed before and set the managementPort.

<Call id="MBeanServer"
    class="java.lang.management.ManagementFactory"
    name="getPlatformMBeanServer"/>

<Get id="Container" name="container">
  <Call name="addEventListener">
    <Arg>
      <New class="org.mortbay.management.MBeanContainer">
        <Arg><Ref id="MBeanServer"/></Arg>
        <Set name="managementPort">8082</Set>
        <Call name="start" />
      </New>
    </Arg>
  </Call>
</Get> 

And, mx4j tools Jar needs to be added to Jetty's classpath.

If you want to run Jetty as a Maven plug-in, you need to add mx4j tools as a dependency.

<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <configuration>
    <jettyConfig>path/to/jetty.xml</jettyConfig>
    ...
  </configuration>
  <dependencies>
    <dependency>
      <groupId>mx4j</groupId>
      <artifactId>mx4j-tools</artifactId>
      <version>3.0.1</version>
    </dependency>
  </dependencies>
</plugin>

After Jetty is started through the above configuration, you can access JMX configurator through the following URL (find "ch.qos.logback.classic"):

http://localhost:8082/

The following is the screenshot information accessed through the MX4J interface:

Configure JMX in Tomcat (tested in JDK 1.5 and 1.6)

If you use JDK 1.6 and later versions, your server is turned on JMX by default. You can, but you don't need to follow the following discussion. Under JDK 1.5, $Tomcat_ HOME/bin/catalina. Add the following line to the bat / sh shell script:

CATALINA_OPTS="-Dcom.sun.management.jmxremote"

Once started through these configurations, you can obtain the MBeans of Tomcat and the JMX configurator of logback by entering the following commands on the command line:

jconsole

After you connect, you can access JMX configurator, as shown above screenshot Same.

MX4J and Tomcat (tested in JDK 1.5 and 1.6)

You may want to access JMX components through the web interface provided by MX4J. In this case, the following are the necessary steps:

Suppose you have downloaded it MX4J , set mx4j tools The jar file is placed in the T O M C A T H O M E / b i n / ∗ writing piece Clip lower . that Do you , add plus as lower of that 's ok reach ∗ TOMCAT_HOME/bin / * folder. Then, add the following line to* Tompath * OME/bin / * folder. Then, add the following line to * TOMCAT_HOME/bin/catalina.sh configuration file:

<!-- at the beginning of the file -->
CATALINA_OPTS="-Dcom.sun.management.jmxremote"

<!-- in the "Add on extra jar files to CLASSPATH" section -->
CLASSPATH="$CLASSPATH":"$CATALINA_HOME"/bin/mx4j-tools.jar

Finally, at $Tomcat_ HOME/conf/server. Declare a new Connector in the XML file:

<Connector port="0" 
  handler.list="mx"
  mx.enabled="true" 
  mx.httpHost="localhost" 
  mx.httpPort="8082" 
  protocol="AJP/1.3" />

Once Tomcat is started, you can find JMX configurator (find "ch.qos.logback.classic") by visiting the following URL:

The following is a screenshot obtained through MX4J interface access:

Topics: Logback jmx