Monday 18 November 2013

DOTS: Logging to server console and log.nsf

In one of my current development projects I am developing a DOTS server task(let). One thing that I found very annoying is the logging part of DOTS. With logging, I essentially mean - at least in this post - an output to the server console and / or log.nsf. To achieve this, there are basically two different possibilities for logging in your DOTS projects.


Option 1:  System.out.println
As in other Java projects you can use System.out.println("your message") to print out message to the server console.
Running this little tasklet results in the following output on the server's console:

As you can see, the message is printed to the console. What you cannot see from here is that these messages are not save to log.nsf.

Option 2: logMessage
A way better logging is provided by a specific method logMessage("your message") which is part of the class com.ibm.dots.task.ServerConsole.
While your cord  resides inside the tasklet you can easily use this method instantly without any further declarations:
The output looks like that:


Using this method has several benefits:
  • Messages logged using the logMessage method are saved to log.nsf. 
  • You can see the date and time of the message.
  • In addition, the message indicates that it comes from the DOTS Tasklet "Helloworld".
In my opinion this is the clean way to go for logging from a DOTS tasklet to the console respectively log.nsf.

Moreover, the ServerConsole class provides another very useful method "logException" which accepts a throwable Java Exception which then will be printed to the console. In other words: you can use this method in any catch statemen to handle error logging to the server console / log.nsf.



4 comments:

  1. I think it's worth to say that logMessage("") only works directly in the Tasklet class. If you want to log outside of that class you can use ServerTaskManager.getInstance().logMessageText("").

    In addition I highly recommend to look at log4j2 (http://logging.apache.org/log4j/2.x/). It works like a charm in combination with DOTS and you'll see big performance gains against NSF based logging.

    ReplyDelete
    Replies
    1. Hi Rene,

      thanks for your feedback. I was going to update the post to include some info about logging outside the ServerTask today. I was not aware of the ServerTaskManager.getInstance().logMessageText() which is really straightforward to use. And logging into my own log file is another thing worth looking into. Thanks for letting me know, I will definately add this to the post!

      Delete
  2. I have developed a very simple class using ServerConsole in CollaborationToday - FeedMonster.

    https://github.com/OpenNTF/collaborationtoday/blob/master/DOTSFeedMonster/org.openntf.news.monster/src/org/openntf/news/monster/shared/TaskletLogger.java

    It extends the Logger class:

    https://github.com/OpenNTF/collaborationtoday/blob/master/DOTSFeedMonster/org.openntf.news.monster/src/org/openntf/news/monster/shared/Logger.java

    You can see how I used it here:

    https://github.com/OpenNTF/collaborationtoday/blob/master/DOTSFeedMonster/org.openntf.news.monster/src/org/openntf/news/monster/QueueManager.java

    ReplyDelete
    Replies
    1. Cool stuff. Thanks, Serdar. I will be looking into this, too :)

      Delete

Comment Form Message