Log4j JMX Support

Adds a JMX support for log4j loggers by implementing java.util.logging.LoggingMXBean interface. This way JMX clients can manage log4j similarly than Java loggers. Log4j has it's own JMX support but according to the documentation it is not at the production level.

Usage

Modifying source code

JmxLog4jLogger is a factory class to get or register org.apache.log4j.Logger objects. You either use one of the getLogger() methods or the register() method, not both. The easiest way is to replace your current log4j getLogger() calls with respective methods from this class and this is the preferred way. Here are some examples. So, instead of this:

private final static Logger LOGGER = Logger.getLogger(MyClass.class);

a logger is fetched (created) like this:

private final static Logger LOGGER = JmxLog4jLogger.getLogger(MyClass.class);

The other option is to use JmxLog4jLogger#register(Logger) method in static block like this:

private final static Logger LOGGER = Logger.getLogger(MyClass.class);
...
static {
    JmxLog4jLogger.register(LOGGER);
}
...

Without modifying source code

If you have no access to source code (or does not want to change it) you can use Log4j JMX Support Agent Delegate.

Object name

Object name for the MBean is "com.hapiware.log4j:type=Logging".

Root logger

Notice that using this library unifies the root logger handling with Java logging. In practice this means that if logger name is an empty String ("") it is then interpreted as a root logger.

Requirements

  • Java 5 or later
  • log4j v. 1.2 or later

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