Tuesday, June 24, 2008

Logging Basics with Log4j


Why use a smart logging tool...

  • We can put log output to different presentations like console, log file.
  • We can categorize log entries to several level of importance and purpose (Debug/ Info/ Error). And then restrict log output to a certain level when we want.
  • Logger provides time, class, other information in log output.
  • You can personalize the format of log output.
  • And many other…

How to put log4j logging in my code…

  • Configuration: Before using Logger class for logging, you need to load configuraion in your program. Otherwise your log will not be printed or put anywhere.

    • write a config file "myApp.log.config". Following is a simple configuration to show log in console and store in a file "test.log"

      log4j.rootLogger=DEBUG, A1, F1

      log4j.appender.A1=org.apache.log4j.ConsoleAppender

      log4j.appender.A1.layout=org.apache.log4j.PatternLayout

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

      log4j.appender.F1=org.apache.log4j.FileAppender

      log4j.appender.F1.File=${user.home}/test.log

      log4j.appender.F1.layout=org.apache.log4j.PatternLayout

      log4j.appender.F1.layout.ConversionPattern=%p %t %c - %m%n
      details of configuration...

    • Load the configuration from code

      PropertyConfigurator.configure("myApp.log.cofig");

  • Log Now! You are ready to log.
    • Get the Logger class
    Logger logger = Logger.getLogger( this.getClass() );
    Logger logger = Logger.getLogger( MyClass.class );
    Logger logger = Logger.getLogger( "ClassName" );

    • Log your message in appropriate level
    logger.debug ( "No of content loaded : " + lenContentList );
    logger.error( "Error occurred while parsing", execption );
    logger.fatal( "Failed to connect the server" );

Appropriate use of logging levels

Here is a guideline for deciding the logging level when you put a message in the log.

  • FATAL: at which point your application or part of application is no longer functional to serve.
    • Crash of the application.
    • Crash the relevant sub-component.
  • ERROR
    • Usually when a Java exception occurs.
    • Processing of a request is postponed due to some error.
    • Processing a request has not cover all features.
  • WARN: indicates minor problems
    • Factors external to the application. Such as missing or inconsistent input parameters supplied by user.
    • Improper initialization, configuration etc.
  • INFO:
    • Significant non-error events in the normal life cycle of the application.
    • When a request is successfully processed in your application.
  • DEBUG
    • Printing value of a variable or object property in log .
    • Non-error events in the normal life cycle of the application
  • TRACE
    • Usually used for timing information.

Wednesday, June 18, 2008

JMeter : Running a flock of your client program

You want to have a performance test of your server.
So, you need to run a flock of client programs and see how server handles them.

What you can do is put some people in testing.. ( not that good idea )
another way is.. you can use some test tool like JMeter to run a good number of your client program copies.




Say, Your main concern is to r
un you client by WowClient.start();
You use main method of ClientKicker class to load configuration and start the client.



The Shepherd.


Now, you move this responsibility of running the client to a sampler class say WowClientSampler which extends JavaSamplerClient. So, it has to implement four functions. JMeter ( the shephard ) can call a flock of this WowClientSampler through these functions.




When we introduce this WowClientSampler with JMeter

  • JMeter runs a number of WowClientSampler (test).
  • On separate thread it runs one instance of WowClientSampler.
  • JMeter can iterate each test a number of times on a thread.
public class WowClientSampler implements JavaSamplerClient {

public SampleResult runTest(JavaSamplerContext arg0) {
// Client is to be kicked from this function
// JMeter calls this function on every iteration.
....
Client.start();
}

public void setupTest(JavaSamplerContext arg0) {
// this function loads initials configuration and setup.
// this is called once in every thread at start of the test.
....
}

public void teardownTest(JavaSamplerContext arg0) {
// this function can be used to free up resource at end of test
// this is called once in every thread at start of the test.
....
}

public Arguments getDefaultParameters() {
// TODO:
....
}
}

How to deal with shepherd?

What you have ...
  • your WowClient in client.jar
  • your other libraries needed by WowClient are dpnd1.jar, logging.jar
  • You have downloaded JMeter from apache site.. download jmeter and extracted to folder jmeter
What you need to do...
  • put client.jar in jmeter/lib/ext ( this is
  • put you dependency libraries ( dpnd1.jar, logging.jar ) in jmeter/lib.
  • run jmeter/bin/jmeter or jmeer/bin/jmeter.bat
  • Jmeter test run window appears
  • Test Plan > Add > Thread Group
  • Thread Group > Add > Sampler > Java Request
  • Here you can find your WowClientSampler in a combo box.
  • Select you test class ( WowClientSampler ). Select other parameters
  • Run Menu > Start.
  • Do you find JMeter is making a flood of your client program.. :D

Configuration parameter's
  • Number of threads (users) - how many threads you want to run. each thread will be represent one instance of your client program.
  • Ramp-up time - In this period, all of you threads will be up and running.
  • Loop count - how many times each thread will run the client program.

Sunday, June 15, 2008

Approaching patterns: Our mistakes

By definition patterns are to give solution of design problems.
Sometimes we don’t understand context and intent of problem that a design pattern conveys. Then we start using this pattern in a wrong way, in a wrong place, sometimes we got over enthusiastic about patterns. That results in a bad design.

Only mistake we did is we didn’t approached the pattern in right way. So it is very important how we meet Mr. X pattern. Definitely, we have to follow a pattern for studying a design pattern : D

Big Mistake :
Almost Every writer describes the purpose of the pattern in the first line. And readers push it hard to understand this very first sentence. And this causes the greatest disaster. (Applicable for newbies).

Bigger Mistake :
After understanding the purpose... (I don’t how many people get this actually) we directly move into the class diagram that shows the solution of the design problem. (Wow man, I got the problem in one sentence and solution in one diagram… I know 60% of the design pattern). Now let’s go through some example and then Mr. X pattern will be transparent to me.

This approach works for a few patterns (for Mr. Factory, Mr. Singleton…)

But reality is.. When we face real life problems….

  • They are sometimes so hidden that you cannot get into it in right time.
  • Some patterns are so related that you cannot distinguish.
  • There are alternative patterns for almost same problem.
  • Sometimes you are so enthusiastic about patterns, that you decide I must use this pattern then put the cork in wrong bottle.

Okay, don’t take it so hard. I have some pattern on learning pattern.

Did you wash your hand before eating?? You have to learn and realize some basic concepts to a good extent before you move to design patterns. If you are serious about a good design, you are gonna consider your pillar. Preferably, you should have nice understanding with OO concepts, interface, abstract, delegation. Can you please revise although you learnt what is interface?

(Poth pothiker srishti korena) Pedestrians create the path. Don’t try to map pattern names with problem definition at first. First face the problem, solve it your own way. Then look for the actual solution what pattern gurus do? That is how you become capable of solving a problem with help of Mr. X Pattern.

Are you doing well? Now, you have to consider these…

Ratul and Zico are twins. Both of them in love with Emmy? Nope. There are some patterns that are very common in structure but different in their use and intent. ( Strategy & Bridge Pattern )

On whom will you take the bet in wrestling? The agile one or the strong one? You will find many problems that have multiple solutions. You have to choose the right one so that you can appreciate yourself in the long run. ( Template Vs Strategy Pattern)

Dr. Zekyl &Mr Hyde. Get double minded. Convince yourself when you use a pattern in your design. When you have your own logic for why you’ll use this pattern, try to argue with yourself why you shouldn’t use this pattern. Believe me, this ensures that you don’t misuse patterns and you realize the intent of the pattern in the long run.

How about a cherry on your ice-cream. You may have understood the naive version of a pattern. There may exist improvements of the pattern. You can handle your problem smartly when you have known different version of Patterns. ( applying hook on Template pattern)

Now, if you ask me about a good design pattern book to start with, I will not tell GOF book. For starter “Head First Design patterns” is cool. And when you learn a design pattern don’t forget to google them.