Adding logging to your Servlet or DOTS task (By Borrowing Code)

I have to thank  Paul Withers, Nathan T. Freeman & Tim Tripcony for the core of this article because they created the Logging Class I steal, borrow and hack modify.  I have to thank the very great Julian Robichaux (and Rob McDonagh who contributed) for OpenLog.

That said if there are any mistakes in what follows they are my own 🙂  I hope I’ve respected the terms of the Apache License, let me know if I haven’t.

The use case for this is quite simple you have a Servlet, or possibly a DOTS tasks (haven’t tried that yet but soon)  beavering away quietly and you want to keep an eye on it.  I suppose you could log to files on the server but that’s not terribly accessible to QA or you perhaps when you’re already very busy.  Since we have Julian’s OpenLog I vote we log messages and errors to an OpenLog database.  If you are with me follow on for the almost step by step instructions. Unlike previous post I’m not going to provide a screenshot for each and every action, if you are here I suspect you know how to use designer.

Pre-Requisites:

  • Domino 8.5.3
  • Domino Designer 8.5.3
  • Notes & Domino Upgrade Pack 1 (Part No. CI5HUEN available from Passport Advantage)
  • Install the Domino 8.5.3 Upgrade Pack
  • OpenLog from OpenNTF.org here’s the current URL: http://www.openntf.org/projects/pmt.nsf/ProjectLookup/OpenLog (Set up OpenLog as per the instructions in the database (for this post I’m allowing anonymous to be a despositor))
  • The XPages Help Application Project from OpenNTF No need to set that up but be ready to open it in Domino Designer for Eclipse (DDE) soon.
  • Some Java knowledge 🙂

Moving on..

If you read this whole post first you can decide whether or not create your own packages, and new class names for the class we’re taking and modifying from the OpenNTF help app.

  • In your Java Servlet/DOTS application create a new package mine would be com.jasonhookonline.logging
  • Right click your new package and create a new class, call it OpenLogItem no need to worry about it’s superclass or what it extends.  The new class should open in an editor for you
  • Open the XPages Help Application in DDE, and then open the Package Explorer view. Expand the src folder and com.paulwithers.openLogClass.  Double click the OpenLogItem class. In the editor copy all of the code, you must retain the copyright notice, and we must add a note that we’ve modified the file to comply with the license.

xpageshelploggingclass

  • Go back to your OpenLogItem class and select all and delete the existing code, paste the new class from the Help App.
  • The editor will point to numerous errors, don’t be alarmed, just log them (Just kidding, don’t do that it’s a waste of time!)
  • Remove import com.timtripcony.util.JSFUtil; whilst his stuff is great we don’t need it we’re not using JSF.
  • First thing to fix is the method getThisAgent() which returns a String:
/**
 * @return the thisAgent
 */
 public String getThisAgent() {
      return "Servlet: com.jasonhookonline.servlets.Servlet1";
 }
  • Fix getSession() The OpenLog ACL would need to allow depositor, and this piece relies on you having installed the upgrade pack. The editor will still point to an error at the getCurrentSession lines.  To fix these keep following.
    private Session getSession() {
       if (_session == null) {
            _session = getCurrentSession();
        } else {
      try {
            @SuppressWarnings("unused")
             boolean pointless = _session.isOnServer();
            } catch (NotesException recycleSucks) {
                 try {
                 _session = getCurrentSession();
                } catch (Exception e) {
                debugPrint(e);
                    }
               }
         }
      return _session;
     }
  • In the project properties find build path, click on the libraries tab and add an external jar com.ibm.xsp.extlib.core_8.5.3.20111208-0717.jar located in <Notes/Domino program dir>/osgi/extlib/eclipse/plugins

logginginservlets-addjar1

  • Add an import statement at the top of your class below package: import static com.ibm.xsp.extlib.util.ExtLibUtil.*;
  • Finally change the value given to _logDbName: private String _logDbName = “OpenLog.nsf”;
  • Save your class.

All of the errors should now have been resolved.

How to use logging in your other classes

  • Open your class (the Servlet one for example) and do the following:
  • Add an import statement to import your new logging class.  My statement would be this: import com.jasonhookonline.logging.OpenLogItem;
  • Add a member variable to your class (just within the start of your class): private OpenLogItem _oli;
  • Add a new method to your class:
    private OpenLogItem getOpenLogItem() {
        if (_oli == null) {
            _oli = new OpenLogItem();
        }
    return _oli;
   }

Now you are ready to log!

    try {
       getOpenLogItem().logMsg("I am very trying");
    } catch(Exception e){
       getOpenLogItem().logError(e);
    }

What next?

You could explore some other ideas like LogLevelling (there’s examples of this if you follow the DebugPrint methods use in the OpenLogItem class.

You could (as Paul Withers suggests) create a utility class to avoid having to add the getOpenLogItem() method to each class:

    package com.jasonhookonline.utility;
    import com.jasonhookonline.logging.*;
    public class Utility {
         public static OpenLogItem _oli;

         public Utility(){
         }

         public static OpenLogItem getOpenLogItem(){
                if (_oli == null) {
                    _oli = new OpenLogItem();
                }
                    return _oli;
                }
   }

Then you can use Utility.getOpenLogItem().logMsg(“”); from any class.

Resources

The new OpenLogItem class file (due to limitations of WordPress I had to upload it as a docx rather than a txt file!)

OpenLogItem

Advertisements
Tagged with: , , , , ,
Posted in Coding, Java, Tips

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: