Adding logging ability

Let's start with using the logging API in Java to log messages to the console. As an example, we want to be able to log some sample messages during the application start and completion.

What we'd typically do in Java 8 or earlier is just import the necessary logging classes and start using the logging APIs. That is, the Logger class from the java.util.logging package. However, as we've learned in the previous chapter, there's an additional step in Java 9. Using the logging APIs directly will result in a compilation error. That's because the logging APIs aren't available in the java.base module. We've seen that the best way to search for a module is using the --list-modules parameter to the java command. Let's run that and see if we find a module related to logging:

$ java --list-modules 
... 
java.jnlp@9 
java.logging@9
java.management@9
java.naming@9
java.prefs@9

As you can see, there's a module called java.logging. That looks promising! The next step is to see if that module exports the APIs we need:

$ java -d java.logging 
module java.logging@9
  exports java.util.logging 
  requires mandated java.base 
  provides jdk.internal.logger.DefaultLoggerFinder with
sun.util.logging.internal.LoggingProviderImpl contains sun.net.www.protocol.http.logging contains sun.util.logging.internal contains sun.util.logging.resources

Good news! It does. The java.logging module exports the package java.util.logging, which is just what we need. Let's add this module as a dependency in the packt.addressbook module first:

    module packt.addressbook { 
      requires java.logging; 
      requires packt.sortutil; 
    } 

Now, we are free to use the logging APIs in our code. In the Main.java class, first import the Logger class and create a static logger variable by initializing Logger with the class name:

    package packt.addressbook; 
    ... 
    import java.util.logging.Logger; 
    ... 
    public class Main { 
      private static final Logger logger = 
        Logger.getLogger(Main.class.getName()); 
      ... 
    } 

Next, we can use the logger to log a message both at the start and the end of the application:

    public static void main(String[] args) { 
      logger.info("Address book viewer application: Started"); 
      ... 
      System.out.println(contacts); 
      logger.info("Address book viewer application: Completed"); 
    } 

Compile the modules by running this command in the project root directory:

$ javac --module-source-path src -d out --module 
packt.addressbook,packt.sortui

Execute with the java command and you should get output that looks like this:

$ java --module-path out -m packt.addressbook/packt.addressbook.Main
Mar 27, 2017 7:41:51 PM packt.addressbook.Main main
INFO: Address book viewer application: Started
[Charles Babbage, Tim Berners-Lee, Edsger Dijkstra, Ada Lovelace, Alan Turing]
Mar 27, 2017 7:41:51 PM packt.addressbook.Main main
INFO: Address book viewer application: Completed

With this, we've successfully integrated logging APIs into our application. This is the simplest of the three use cases we'll be looking at in this chapter. The usage of the logging platform API involved:

  • Declaring the need for a platform module--with the requires statement
  • Usage of the platform module APIs in the Java source
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.177.86