Announcement Announcement Module
No announcement yet.
Where is the best place for logging? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Where is the best place for logging?

    Hi all,
    I've written an application that use both spring mvc and swf. We need to log activities and access to pages, and we are using commons logging.

    In mvc pages we are doing this by logging at controller classes and on web flow pages since controller are not there we have to do it at the service classes.
    Is it the correct things to log activities at controllers ? or should it be given to services or dao's? What is the best practice?

    Also is there a way to log event flows in swf to commons logging?

    Thanks in advance,

  • #2
    Where is the best place for logging?

    In my opinion, I would include logging at the Service layer. Having logging in layers below that (i.e. Domain/Model or DAO layers) is probably not worthwhile as it would be difficult to trace the calls from the service layer that is affected.

    Instead, throw an exception back till it reaches the Service layer and catch it at that point for logging with the opportunity to throw it back to the Application layer. Remember to use unchecked (runtime) exceptions.

    (I call an Application layer that holds web pages, uses Spring MVC, or Struts, or Swing... the usual UI and pageflow stuff).

    As for logging at the Application layer, I think that is worth having that also in addition to logging at the service layer (again, to help with tracability). It will also log additional exceptions that can be thrown by JSPs, Spring MVC, Struts, App Server, etc. that are isolated.

    Also, the Application Layer and Business Layer (consisting of Service + Domain/Model + DAO layers) should be decoupled, it makes sense to have two separate log files to support extensibility (i.e. moving the Application layer to .NET and exposing the Business Layer as a web service hosted across two separate servers).
    Last edited by shahnawazshahin; Dec 17th, 2008, 04:23 AM.


    • #3
      shahnawazshahin, thanks for your valuable thoughts!


      • #4
        Here is a link to something interesting, and some lessons learned.



        • #5
          Sorry if this is an old thread, but I have just read the original post and realized that the question was specifically about logging page visits while the discussion seems to have mistakenly addressed something totally different.

          Such kind of logging can and should be done on the presentation tier (Spring MVC and SWF) in an aspect-like fashion. For example, in SWF, the best place for this would be on the flow listener(s.) For example, subclass the abstract FlowExecutionListenerAdapter class ( and override the appropriate methods by placing the logging logic inside. this way, you will effectively intercept the events of the flow you are interested in, will be able to access the current flow/conversation-scope data, and log whatever you need - with all such logging consolidated in one place. You can also implement generic logging of some exception events in the "exceptionThrown" method. (i would still rather do that in the dedicated resolvers.)

          Do the similar stuff in non SWF, Spring MVC part of your application - by listening to the events instead of coding the logging logic explicitly in your conrollers, etc. Yo ucan use AOP for that.