Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Portable Logger Names with Java 7 Method Handles

DZone's Guide to

Portable Logger Names with Java 7 Method Handles

· Java Zone
Free Resource

Learn how our document data model can map directly to how you program your app, and native database features like secondary indexes, geospatial and text search give you full access to your data. Brought to you in partnership with MongoDB.

Java 7 introduced the Method Handles with the java.lang.invoke.MethodHandle and java.lang.invoke.MethodHandles classes. The Well-Grounded Java Developer (which I have previously reviewed) covers method handles in Chapter 5 (Section 5.2) and concludes with a trivial but highly useful example of what one can do with method handles and logger naming. I briefly explain this idea in this blog post.

When acquiring a logger for a particular Java class, it is common for the Java developer to use that very class to name the logger. In Log4j, this is typically done with a reference to the class itself. For java.util.logging, this is typically done with a reference to the fully qualified name of the class. Although this is easy to do, it is possible to mistakenly provide the wrong class, especially when the incorrectly provided class is in the same package and a logger definition statement is copied and pasted from an existing class to a new class. As the authors of The Well-Grounded Java Developer suggest, the newly introduced MethodHandles class can make this declaration more portable and always based contextually on the class in which the declaration resides.

The MethodHandes.lookup() method returns an instance of MethodHandles.Lookup, from which the lookupClass() method can be invoked. This latter method's return value can be used directly in naming the Log4j logger or can have that returned Class's getCanonicalName() method return its name as a String for naming the java.util.logging logger.

The following two code snippets show this for a class specifying its Log4j logger and for a class specifying its java.util.logging logger. Note that there are more characters in these examples than might normally be present when hard-coding the class or class name into the logger declaration, but this code ensures that the actual class or class name is always used.

MethodHandles Approach to Naming log4j Logger
package dustin.examples.methodhandles;

import java.lang.invoke.MethodHandles;
import org.apache.log4j.Logger;

/**
 * Demonstrating use of MethodHandles with Log4j.
 * 
 * @author Dustin
 */
public class MethodHandlesLoggerLog4j
{
   /** Use Java 7 MethodHandles to get my class for logger. */
   private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass());

   public static void main(final String[] arguments)
   {
      final String msg = "Hello World: Using MethodHandles for Class to associate with Log4j logger.";
      LOGGER.info(msg);
   }
}

MethodHandles Approach to Naming java.util.logging Logger
package dustin.examples.methodhandles;

import java.lang.invoke.MethodHandles;
import java.util.logging.Logger;

/**
 * Demonstrating use of MethodHandles with java.util.logging.
 * 
 * @author Dustin
 */
public class MethodHandlesLoggerJavaUtilLogging
{
   /** Use Java 7 MethodHandles to get my class name for logger. */
   private static final Logger LOGGER =
           Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());

   public static void main(final String[] arguments)
   {
      final String msg = "Hello World: Using MethodHandles for Class to associate with java.util.logging logger.";
      LOGGER.info(msg);
   }  
}

This post has looked at an idea for a very simple use of the newly available Java 7 MethodHandles class as covered in The Well-Grounded Java Developer. Although this is a small thing, it is illustrative of a convenient use of a lesser advertised Java 7 language feature that provides many more and far more powerful (and more complex) uses.

 

Discover when your data grows or your application performance demands increase, MongoDB Atlas allows you to scale out your deployment with an automated sharding process that ensures zero application downtime. Brought to you in partnership with MongoDB.

Topics:

Published at DZone with permission of Dustin Marx, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}