How it works...

In this recipe, you used the Logger class provided by the Java logging API to write log messages in a concurrent application. First of all, you implemented the MyFormatter class to assign a format to the log messages. This class extends the Formatter class that declares the abstract format() method. This method receives a LogRecord object with all of the information of the log message and returns a formatted log message. In your class, you used the following methods of the LogRecord class to obtain information about the log message:

  • getLevel(): Returns the level of a message
  • getMillis(): Returns the date when a message was sent to a Logger object
  • getSourceClassName(): Returns the name of a class that had sent the message to the Logger
  • getSourceMessageName(): Returns the name of the method that had sent the message to the Logger
  • getMessage(): Returns the log message

The MyLogger class implements the static method getLogger(). This method creates a Logger object and assigns a Handler object to write log messages of the application to the recipe6.log file, using the MyFormatter formatter. You create the Logger object with the static method getLogger() of the Logger class. This method returns a different object per name that is passed as a parameter. You only created one Handler object, so all the Logger objects will write their log messages in the same file. You also configured the logger to write all the log messages, regardless of their level.

Finally, you implemented a Task object and a main program that writes different log messages in the log file. You used the following methods:

  • entering(): To write a message with the FINER level indicating that a method has started its execution
  • exiting(): To write a message with the FINER level indicating that a method has ended its execution
  • log(): To write a message with the specified level
..................Content has been hidden....................

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