Wednesday, 2 November 2011

log4j - power logging at your fingertips

log4j has been a popular logging framework in Java applications for several years. It is simple to implement, thread safe and light weight. While there are no current major releases planned, it is still a popular download among the Apache Logging Services toolset.
To add log4j to your project using Maven, add the following dependency snippet to your code


  log4j
  log4j
  1.2.16
 
Maven will download and add the log4j jar to your respository. Next, you need to specify a configuration file, which could be either a .properties file or a .xml file.The configuration file is usually named as log4j.properties or log4j.xml and need to be placed within the classpath for the application to find it.
If you are building an application that ships as an executable JAR, place this file within your src/main/resources or your src/test/resources directory depending upon whether you need your logging framework in the production code or not
The log4j manual describes a sample properties file.If you don't have access to a .properties file, you can use the entries shown in the example to create a .properties file for yourself.Now that you have a log4j properties file, you need to make sure that you have a Appender defined within it.
For logging messages to the console, use a ConsoleAppender. For logging to a file, use a FileAppender.
To control what should be logged with the log statement, set up a Layout and initialise it to a particular Pattern. A popular Layout is the PatternLayout that formats the output line with meta data using a pre-defined pattern.

For example, here is a snippet from the manual.

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

The ConversionPattern can be used to specify meta-data about the logged statement, such as the Class from were the log originated, the date and time in different formats, the severity level etc.An exhaustive list is available in the API docs. It is worth noting the several warnings posted in the API docs regarding speed and efficiency before choosing a ConversionPattern character.
Another aspect of log4j logging performance that one needs to be aware of is the cost of creating a log statement that may not be used.To get around the parameter construction code, wrap the logging statements within a check:
if(logger.isDebugEnabled() {
    logger.debug("Log : " +  " String 1 " + "String 2"));
}
If the check is not place, the log statement will create 4 Strings (yes , we could have used a StringBuffer), but a simple check prevents this overhead. Ofcourse, there is now the cost of checking the LEVEL at which the logger is set, but this is a miniscule overhead.
Finally, now that the log4j is setup and has been configured, instantiate it 

static final Logger logger = Logger.getLogger(MyClass.class);
and start logging:
logger.info("Starting the APP");
Exceptions can be sent to the log file as an argument to the logger.error method.
catch (Exception exception) {
logger.error("Error in loading application! ", exception);
To conclude, if you need an easy and fast logging framework, log4j will fit your bill.However, if you are in the market for the next generation of logging, then have a look at Logback

Saturday, 29 October 2011

Using 'R' for Statistical Computing

I recently tried out the 'R' toolkit for performing some statistical operations and was impressed with the power and the extensibility of the toolkit in manipulating terra-byte sized data and its ability to produce publication ready graphs. Being open-source, it is available under the GNU GPL and can be downloaded from a CRAN site and set up to run on Windows, UNIX or Mac OS.
The toolkit basically provides a command-line enviroment for manipulating and loading data into memory arrays which can be subject to further statistical analysis. While the majority of the users see 'R' as a statistical package, it can be also be used as a modelling tool for linear / non-linear models.Apart from providing some high level functions for statistical computing, 'R' also has a well-developed suite for looping and conditional execution. It  also allows extensibility so that users can write their own functions.While the user-interface is easy to get used to, if you are coming from a UNIX background, it might take some getting used to for Windows users.

The 'Help' module is a well designed and is easy to use. Documentation on any function can be loaded from the command-line by preceeding the function name with a '?'. Overall, 'R' is a powerful toolkit for statistical computing and an excellent choice for manipulating large data sets.

Tuesday, 18 October 2011

GeoTools - GIS for Java Developers

GeoTools is an excellent Geographical Information System (GIS) toolkit for Java. It is open-source and has regular updates. The last released version at the time of writing is 2.7 released on the 7th of October 2011. The toolkit also serves as an engine for some other GIS based open-source tools such as uDig, GeoMajas and GeoServer.GeoTools also has some good supporting documentation with tutorials for setting up the toolkit in Eclipse / Maven. The tutorials also give some pointers on generic Geospatial concepts, such as Features, Layers and Maps

A Feature in GIS world is any real world entity that can be represented on a Map. Valid examples of such entities are rivers, buildings, roads, etc. In GeoTools, Features are an instance of a FeatureType. Thus, Features can be considered analogous to (Java) Objects and FeatureTypes can be modelled as (Java) Classes. Features have attributes. for example,  a river may have length, depth, water salinity as its attributes similar to the Field concept in Java. Features also define operations which are analogous to Methods in the Java world. Thus, this close analogy between Java concepts and the GIS toolkit implementation  makes it an easy for Java programmers to work with the toolkit.

Data in GIS (& GeoTools) can be represented in two forms, Raster or Vector type. Raster type data refers to digital images that can be transformed to a grid representation. Vector type data use Geometry to represent real-world elements.Geometry can be reduced to three main forms: Point, Line and Ploygon. Geometry also gives a location attribute to the element it refers to.

Thus, in GeoTools, defining a Feature using a Vector datatype requires a Geometry component specification.  A Feature also has a Style component associated with it. The Style component defines the rendering and look and feel of the Feature on the Map. Features are connected to a Map using Layers. A Map can have several Layers. A Layer contains a set of Features and their associated Styles. It is possible to overlay several Layers on a Map and thus have different objects visible in the same view.Usually, this entire information schema is stored within a file, known as a ShapeFile.

To summarise the process of rendering a Map using GeoTools, the following steps need to be followed:

1. Create a Map, using a DefaultMapContext
2. Load the ShapeFile 
3. Extract the FeatureSource from the ShapeFile  [FileDataStoreFinder.getDataStore(shapeFileLocation)]
4. Create a Style using the FeatureSource. GeoTools has some good tutorials on getting this done.
  A Style involves creating FeatureTypeStyles and associating Symbolizers and Rules with the defined FeatureTypeStyles.
5. Add the Style to a Layer of the Map.
6. Add the Map and a Renderer, to a JMapPane which extends JPanel.
7. Display the JMapPane.

GeoTools also has tools available for creating ShapeFiles from CSV files.Thus, GeoTools simplifies the process of creating GIS applications in Java and enables applications to be up and running within a matter of hours.

Saturday, 24 September 2011

Maven-ise your Eclipse (Indigo)

With the transition of the Maven2Eclipse plugin project from the Sonatype boys to the Eclipse umbrella, support for running Maven from within Eclipse has become smoother and definitely easier. To get started, install the Maven2Eclipse plugin from within Eclipse using the Help->Install new software menu option. The link to the plugin download is http://download.eclipse.org/technology/m2e/releases but verify the same from the  plugin site.
After successfully installing Maven, you will have access to several Maven project wizards that will help you get started with a Maven project with a few clicks as shown in the example below as I did for Eclipse Indigo.

  • Select a New project and choose Maven project. Click through the next few steps. You will get an option to choose the Maven archetype and specify the GroupId, Artifact Id, Version and Package






















  • After you select the required options, the wizard will generate a project file structure and populate it with some skeleton code including a Junit test.











  • That's it. You have created a Maven based project which is now awaiting functionality. Adding new dependencies, plugins to the project and the POM is made easy using the Maven project menu, available by right clicking on the project folder and selecting the required option.























  • A Maven Build can be set-up and initiated from the Run- As menu option. 













  • Selecting the Maven build option for the first time which will bring up a set-up configuration dialog that will enable you to specify the goals, profiles and set up any required parameters.

















  • Once the Run configuration is ready, you can build your Maven project and deploy your snapshot.