Saturday, July 28, 2012

byteman example : enable debug logging

A quick introduction to byteman: Built by the jboss community, it allows you to add code to your existing java program on the fly.

It is very handy in situations like:
  • You want to print a parameter value on the method (better yet, run a getter chain on it)
  • Change return value of the method
  • Know what's going on in code that you don't own
  • ...
This demonstration is to enable debug logging for log4j. Obviously there are other ways to enable debug logging for log4j, but it is useful in situations where you don't have write access to the config file, or you don't know what config file to change.

Here is a typical call to log something in debug - protected by a isDebugEnabled() call.

if (logger.isDebugEnabled())
    logger.debug("The parameter value " + param + " is valid");

We have to make sure the isDebugEnabled returns true, and then the call to debug goes through overriding the internal log4j check to verify if debug is indeed enabled.

Byteman works with rules, here we are creating two rules to make sure debug log is written.

Rule 1: make sure isDebugEnabled() returns "true"

RULE enable debug logging
    CLASS org.apache.log4j.Category
    METHOD isDebugEnabled()
    IF true
    DO return true

Rule 2: call the forcedLog() when logger.debug() is called

RULE force debug logging
    CLASS org.apache.log4j.Category
    METHOD debug(java.lang.Object)
    IF true
    DO $0.forcedLog(org.apache.log4j.Category.FQCN, org.apache.log4j.Level.DEBUG, $1, null)

Attach byteman to your jvm: The following command will deploy byteman agent into your running jvm (pid), and listens at port 6666. More help here

java -jar lib/byteman-install.jar -h localhost -p 6666 --pid--

Deploy the rules file:
java -classpath lib/byteman.jar:lib/byteman-submit.jar \
    org.jboss.byteman.agent.submit.Submit \
    -h localhost -p 6666 -path to script-

Now, debug logging should be enabled for all categories now. If you want to filter only a certain logger, you can modify the rule which returns true on isDebugEnabled as follows

RULE Enable debug logging
    CLASS org.apache.log4j.Category
    METHOD isDebugEnabled()
    BIND constant:string = "NAME OF MY LOGGER"
    IF $0.getName().contains(constant)
    DO return true

When you are done, remove all the rules

java -classpath lib/byteman.jar:lib/byteman-submit.jar \
    org.jboss.byteman.agent.submit.Submit \
    -h localhost -p 6666 -u

That's it, download Byteman and instrument your code.

Saturday, February 12, 2011

A Train to Catch

One of my not-so-much-of-a-morning-person friends had to catch a train at 6am every Monday. The thought of the merciless train that will leave him behind, scared him enough to make sure he never missed it. That got me thinking - discipline - forced or not, he had it.

Tuesday, September 14, 2010

Only in Rome...

Had to post this picture! Found this advertisement for scooter rental on the streets of Rome! Only in Rome can anything like this can be pulled off!

For those who don't know, the picture here is one of Michael Angelo's famous paintings depicted on the ceiling of the Sistine Chapel in Vatican City.

Wednesday, September 8, 2010

I always thought I don't like ice-cream ..

And then there was Paris. Bidding 'au revoir' to Paris, all I can think of are the ice-creams and sorbets I had. The first spoon of melon sorbet was a melon explosion in my mouth. I had never tasted anything like it other than the fruit itself, and this was much better. The sugar content was just right, and surprisingly, the one thing about cold things I don't like - the coldness - didn't bother me. I was enjoying it so much that Madhu was truly jealous that she chose panna cotta instead. The panna cotta ice-cream she got was good too - light, creamy and just right.

After our first taste, we tried many many others - mango, passion fruit, pistachio, caramel - they were all scrumptiously good.  And, I enjoyed ice-cream as never before!

And we ate, and we ate and we ate...

No matter what you taste - from a blueberry muffin to a penne with tomato basil to a raisin cake to a warm apple galette with ice cream - everything tastes special - from a road side hole in the wall to a nice sit down restaurant, the quality and flavor of food is mind-blowing.
The flavors are always spot on. Not too greasy or sugary. And when your waiter says something is spicy, you better believe it. There is also a great variety of food here. As vegetarians, to say we were apprehensive of our chances for good food in Paris is saying it mild. Paris really surprised us, from the libyan galette with zatar to crepes with cheese, eggs or with nutella to falafel, there is great street food at low cost anywhere you go.

Having cash at hand definitely has its advantages, a glass of wine will cost you €3 at the bar when the same might cost you double if you order it from your table! A crepe from a small street side vendor (with a couple of tables) is better taken to go than use their table. If you want a quick picnic, hop into a neighborhood boulangerie for a baguette. Don't forget to get some cheese at the fromagerie next door. Oh, how about those macarons at the patisserie across the street? There you go, doesn't that sound perfect? A cherry on top would be to pick a great spot - the gardens in front of Eiffel tower, along the banks of Seine or just any neighborhood park. You can't go wrong in Paris.

By the way, once you get a table at a restaurant, they don't expect you to leave anytime soon, they don't give you a bill unless you ask for it. You don't really have to leave after you pay your bill! And no tips, service charge is included. We always rounded off the bill, but there is no expectation of that too.

I did not see anyone hurry anywhere except on the subway. Maybe even that was to get the best street-viewing spot at their favorite cafe? I don't know how the French do it, but I am amazed by how much they enjoy the simple pleasures of everyday lunch, always taking time for food, chatting with friends and of course wine with a cigarette alongside.