Logging with Log4J

It’s assumed that most of the applications developed these days, keep track of their errors and failures via a log file. It turns out to be really handy when it comes to optimizing the app in a later stage.

Project that I’m working these days is also not an exception to it. From its next phase we are planning to integrate error logging. In our case we actually want to externalize all the errors and failures and warnings to a file. So that we may refer it to study the defects and bugs on it that might required to be catered in later versions of the project.

And they say never to use System.out.println to address errors. Simply because it doesn’t give you a mechanism to reference it at a later point unless you manually write it down or get screen shots then and there.

Anyway now we may look at how to get started with error logging in java with popular logging tool, Log4J by Apache Software Foundation (ASF). I’m really sorry if you are not into java probably then this is not for you.

PreRequisites
  • Java
  • An IDE (I use Eclipse so does my project)
  • Log4J (extract the archive file)

Hoping java and eclipse are installed and they are ready to go, now you can open up your eclipse IDE.

  • Create a new Project, name it whatever name you like. (In my case it was Log4JPro).
  • Add a new folder in your project’s root folder (better to name it lib just as a convention).
  • Insert the extracted lib file to the above created folder.
  • Add log4j-xxxxxx.jar file to the classpath of your project.

That’s it now you are almost there. But this actually the most important part of all. What you need to do here is setting up a small configuration file called log4j.properties. This is where you set virtually everything you need to customize with log4j goes in.

There are lots of properties that you can start configuring which will include the type of output you need, the formatting of the report, the maximum size of it (some times), and even the mode of writing (if it’s a file) whether you want to over write the previous file or to append to it and so on and so forth.

Below I’ll just give an example log4j.properties file as a starting point. This configure log4j to give a html output. And already there are more than handful of properties customized on it. And play around with it once you start working with it.

Most importantly this file should reside in the root of your project if you are using eclipse it should be placed in your src (source folder). here is an example properties file.

log4j.rootCategory=,dest3
log4j.category.TestLogging=DEBUG, dest3

log4j.appender.dest2=org.apache.log4j.RollingFileAppender
#log4j.appender.dest2.layout=org.apache.log4j.HTMLLayout
log4j.appender.dest2.Threshold=ERROR
log4j.appender.dest2.File=log4j.log
log4j.appender.dest2.Append=false
log4j.appender.dest2.MaxFileSize=100KB
log4j.appender.dest2.MaxBackupIndex=2

log4j.appender.dest3=org.apache.log4j.DailyRollingFileAppender

log4j.appender.dest3.File=LabLog.html
# Control the maximum log file size
# Rollover log file at the start of each week
log4j.appender.dest3.DatePattern=’.’yyyy-ww

log4j.appender.dest3.layout=org.apache.log4j.HTMLLayout
log4j.appender.dest3.layout.LocationInfo=true
log4j.appender.dest3.layout.Title=ISD Log

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p | %d | %F | %L | %m%n

Below given is an example code and a screen shot of the generated html file of it. Hope you enjoyed it!

/**
* @class LogApp.java
* @author Lonely Coder
* @date 09/30/2008
*/

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class LogApp {

private static Logger logger = Logger.getLogger(LogApp.class);

public LogApp() {

}

public static void main(String[] args) {

long time = System.currentTimeMillis();

logger.info(“This is an Information..”);
logger.warn(“This one is just a Warning!”);

logger.log(Level.TRACE, “And a trace message using log() method.”);
long logTime = System.currentTimeMillis() – time;

logger.debug(“Time taken to log the previous messages: ” + logTime
+ ” milli secs”);

try {
String str = “log4j”;
char concat = str.charAt(6);
System.out.println(concat);
} catch (Exception e) {
logger.error(“Error in main() method:”, e);
}

}
}

log4j example log in html

Getting your Gmail feeds.

Download Podcast

Few days back while I was checking ma mails, I just clicked on the rss icon that appears on the right corner of the address bar (on firefox) which to me to a page where it was showing few of the most recent mails in the inbox.

Which you can check on,
http://mail.google.com/mail/feed/atom

I actually knew this quite some time back via a post of a fellow blogger. But Then I dint have much of a time to explore on it neither I was too interested on it by then. Anyway this time around I thought it’s time for me to get some insight of it.

The feed shows few of the most recent mails in your inbox together with few lines of the body of them as well. This becomes really handy if you are in a real hurry or even if you want to check your mails in your mobile because compared to getting there using the traditional login, this seems to be really fast.

It can be used to check whether you have received any new mails after you logged in for the last time and so on. But unfortunately, one will hardly be able read the content of the mail in whole unless the mail is so short that it will not exceed two lines or so. And getting any attachments is almost impossible as it stands now.

Just to exploit on it I have developed a simple page which makes life easier if I’m to access my feeds. The URL is given below. But I really don’t suggest you to go ahead and use it. And if you are using it remember, it’s on your own risk.

A customized Login

The page above utilizes https://username:password@mail.google.com/mail/feed/atom mode of accessing Gmail feeds, so there one only requires to give the username and the password and it take care of the rest. Otherwise it could be potentially dangerous to use this method since your password will be revealed (because the address bar will never mask your password) in that case you better stick to the very first URL on this post.

Anyway I think it’s always nice to know few other ways of getting there as it may turn out be useful one day when you are stranded and when you have no other options. Hope you enjoyed it. Cheers.!

Interactive Pages

Podcast

It’s been a long time desire of mine to develop an interactive web page with draggable content. But only in the recent times I could get hold of it. So today on my leisure, I developed a simple page to exploit it. Should say very simple at that because they say simple things should be made simple.

So I don’t think its worth writing on it. But trust me, it really worth a visit to it. A link to the page is given below. So one can follow it up to check it out.

Go Interactive

Somehow I think I have managed to do something for children. Well I actually wanted to do it that way! Anyway, should note that it was only checked on firefox. Feel free to drop in your comments, Have a good day.

It’s more than an Image – EXIF metadata

Download Podcast

One of the interesting features that impressed me a lot when I started to upload photos on Flickr is that it gives details of the image such as when it was taken, make of the camera that it was taken, exposure time, model, focal length and etc..

Some time back this was requirement in one of our projects as well. Where we allow our users to upload photos to the system and then the system should be able to track and show details of the photo to the administrator in case if it’s required to trace the person and the camera from which it was taken. Because this web based solution was actually developed for the use of police. Hence the administrator will be one of them as well.

So we actually wanted to track details of such as the make of the camera from which it was taken, the date and time it was taken and the focal length of the camera and so on. So when I first saw this on Flickr I was over the moon that it was possible. And I started digging on it. But unfortunately we couldn’t implement it on our system as we ran out of time preparing it for an exhibition, working on a tight schedule (Had to carry out this project in a span of mere two weeks amidst of lectures and all at that time).

Coming to the business end, the technology behind it was very simple yet it could be used to produce some amazing results. At the ground it’s not a technology, it’s actually a standard called EXIF (Exchangeable Image File Format).

According to the convention there are serious of attributes that can be associated with an image which if you are interested can read it on its declaration. There, these attributes are categorized into 5 catagaories.

And only the mandatory ones are a must to implement if any camera maker wants to stick to it. But more often than not you will get to see the comments of the photographer and the manufacturer of the camera as well though these are not madatory according to the Specification. And the documentation also lists of serious of attributes where those attribute values should be encrypted. And comments fall into this category.

Though it was a PHP implemented on Flickr, And we also initially wanted to do it using PHP, I have used java to check on it as I’m working with java a lot these days. So to do this one obviously requires a java API. After googling it for a while I was able to find two such libraries and chose to stick with Sanselan. As it looked simple, and it had relatively more features compared to the other such as writing such metadata to an image and so on.

That’s all what I required to start coding a tiny application to check on it. And below shown is a screenshot of it. As it was mentioned above, since comments are encrypted you want be able to read it here (it’s not in a human readable manner but if you can no harm go ahead and read it). Still it’s possible to read comments as well.

A screen shot of the Application

a screen shot of the application

Download the Application requires java runtime environment (JRE 1.4 or later).

The Image

image.jpg

Here exif image width and exif iamge length refers to the width and the height of the image in pixels.

Pixel :- Pixel comes from Picture element and it is known to be the building block of an image.

Acknowledgements

The image was taken from deviantart, thanks janati. Image looks fantastic ha!. Hope you will find it interesting, feel free to drop in your feedbacks.