loglevel

loglevel is a small command line utility to show and change a logging level of Java Loggers (java.util.logging.Logger) and log4j loggers (org.apache.log4j.Logger) without restarting the target JVM. With Java 1.5 to get this to work requires that the target JVM has been started with -Dcom.sun.management.jmxremote.

With Java 6 or later things are easier. Of course, the above works perfectly well but with Java 6 even if the target JVM has been started without -Dcom.sun.management.jmxremote the JMX agent can be started for the target JVM without restarting it. This can be achieved by using the companion utility startjmx.

Using loglevel

loglevel is used from the command line and it is called like this:

java -jar loglevel.jar [-? | -h | -help | --help | --version]
java -jar loglevel.jar COMMAND [ARGS]

where COMMAND is:

  • j
    jobs , shows PIDs and names of running JVMs (jobs). PID starting with an asterisk (*) means that JMX agent is not runnig on that JVM. Start the target JVM with -Dcom.sun.management.jmxremote or if you are running JVM 1.6 or later use startjmx service.
  • l PID [j|J|4] PATTERN [| root]
    list PID [j|J|4] PATTERN [| root], lists current logging levels for all loggers matching PATTERN (Java RE) in a JVM process with PID. 'root' as PATTERN lists only the root logger(s). Java loggers are identified by (J) prefix and log4j loggers by (4). After PID argument may be an optional argument to focus 'list' command only to either logger type. J or j for Java logger and 4 for log4j logger.
  • p PID [j|J|4] PATTERN
    parent PID [j|J|4] PATTERN, shows loggers and their direct parent loggers matching PATTERN (Java RE) in a JVM process with PID. Java loggers are identified by (J) prefix and log4j loggers by (4). After PID argument may be an optional argument to focus 'parent' command only to either logger type. J or j for Java logger and 4 for log4j logger.
  • s PID [j|J|4] PATTERN [| root] LEVEL [| null]
    set PID [j|J|4] PATTERN [| root] LEVEL [| null], sets a new logging level LEVEL for all loggers matching PATTERN (Java RE) in a JVM process with PID. 'root' as PATTERN sets only the root logger(s). LEVEL is a string representing a new logging level. Value 'null' is accepted to set the logging level to follow a parent logger's logging level. Java loggers are identified by (J) prefix and log4j loggers by (4). After PID argument may be an optional argument to focus 'set' command only to either logger type. J or j for Java logger and 4 for log4j logger.

Examples:

java -jar loglevel.jar -?
java -jar loglevel.jar jobs
java -jar loglevel.jar l 50001 ^.+
java -jar loglevel.jar list 50001 root
java -jar loglevel.jar p 50001 ^com\.hapiware\..*Worker.*
java -jar loglevel.jar set 50001 j ^com\.hapiware\..*Worker.* INFO
java -jar loglevel.jar set 50001 4 .*Test null

Requirements

  • Java 5 or later
  • log4j loggers require Log4jJMXSupport utility classes (see Log4j JMX Support)
  • Be aware that a dependency to Sun's tools.jar is a system dependency and thus needs to be set accordingly if you are to compile this utility from the source. In Sun's reference implementation (1.5) this artifact is called tools.jar but, for example, on Mac OSX 10.6 needed classes are in classes.jar.

    Also, when running the utility the same artifact must be found from the class path, of course.

Setting up the support for log4j

Source code modification

If you have access to source code then you can very easily add the JMX support by replacing existing getLogger() calls with JmxLog4jLogger.getLogger() respectively. This registers the JMX support. See Log4j JMX Support.

Without modifying source code

If you don't have an access to source code you can still add the JMX support for log4j. To do that you need Log4j JMX Support Agent Delegate. This requires some additional JVM startup parameters, namely:

  • -classpath
  • -javaagent
  • -Dcom.sun.management.jmxremote

Also an XML configuration file is needed for Log4j JMX Support Agent Delegate.

This example assumes that all the required jar and configuration files are in /usr/home/me/lib directory. Files are:

  • agent-2.0.0.jar
  • agent-config.xml
  • asm-3.1.jar
  • log4j-1.2.16.jar
  • log4j-jmx-support-1.1.0.jar
  • log4j-jmx-support-delegate-1.0.0.jar
  • Log4jTest.class

Also, it assumed that the Java program to be started is in the default package and is named as Log4jTest. Here is the command:

java -Dcom.sun.management.jmxremote \
-javaagent:/usr/home/me/lib/agent-2.0.0.jar=/usr/home/me/lib/agent-config.xml \
-classpath /usr/home/me/lib/:/usr/home/me/lib/log4j-1.2.16.jar:/usr/home/me/lib/log4j-jmx-support-1.1.0.jar \
Log4jTest

Here is agent-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<agent>
    <variable name="lib-path">/usr/home/me/lib<variable>
    <delegate>com.hapiware.asm.log4j.Log4jJmxSupportAgentDelegate</delegate>
    <classpath>
        <entry>${lib-path}/log4j-jmx-support-delegate-1.0.0.jar</entry>
        <entry>${lib-path}/asm-3.1.jar</entry>
    </classpath>
    <filter>
        <!-- NOTICE! this is just a regular express pattern to mark classes or packages for instrumentation -->
        <include>Log4jTest</include>
    </filter>
</agent>

Now you are able to check and change the levels for log4j loggers using loglevel utility (or any other JMX management client like jconsole).

Download

Download from JMX Tools page.

License

MIT License

Copyright (c) 2010 Hapi, http://www.hapiware.com

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License