Adding resources

We are going to use log4j to log in to the file or console. Log4j is configured via the log4j properties file. Now let's create the properties file. To do so, navigate to src/main/resources, right-click on resources and select New | Other...; a new wizard appears. Search for file in the Filter section, select File, and click on Next, as shown in the following screenshot:

Adding resources

Next, a File resource window appears; enter the filename as log4j.properties and make sure the parent folder is MyDistance/src/main/resources and click on Finish, as shown in the following screenshot:

Adding resources

Note

Resources are placed in the src/main/resources folder.

Once the file is created, add the following piece of code to set the different properties of log4j. It attaches the pattern layout to split out information on the console, writes a log to the Mydistance.log file, and is set to the DEBUG level, as shown in the following code:

#log4j Properties 
  log4j.rootLogger=DEBUG, consoleAppender, fileAppender

  log4j.appender.consoleAppender=org.apache.log4j.ConsoleAppender
  log4j.appender.consoleAppender.layout=org.apache.log4j.PatternLayout
  log4j.appender.consoleAppender.layout.ConversionPattern=[%t] %-5p  %c{1} %x - %m%n

  log4j.appender.fileAppender=org.apache.log4j.RollingFileAppender
  log4j.appender.fileAppender.layout=org.apache.log4j.PatternLayout
  log4j.appender.fileAppender.layout.ConversionPattern=[%t] %-5p  %c{1} %x - %m%n
  log4j.appender.fileAppender.File=Mydistance.log

For more information on log4j, refer to http://logging.apache.org/log4j/1.2/.

..................Content has been hidden....................

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