1

I have a running JSF2 application and now want to add a request logging feature.

Logging will be saved to db and consist of the standard user/page/IP trio with other generated content.

Now calling a dao method in @PostConstruct annotated methods in managed beans but this seems like code duplicate.

@PostConstruct
public void init() {
     loggingDao.save(user,page,ip);
}

How can I centralize this logging/auditing process on jsf side using managed beans? Since this is a crosscutting scenario, I do not want to add this code to every managed bean.

EDIT The question got lots of comments which implies that it is not asked in a correct way.

One last chance: I need to log/watch user interaction on my site, either it may be a login action or a button clicked to list items (which maps to a backing bean methods) or a page navigation/redirect.

I also assume that I can use this same architecture to decide if the user has the rights for a specific action on the site, but this is another story since its outcomes would be different.

Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
  • 1
    So what exactly is your question? I don't get what your problem is. – chkal Apr 01 '13 at 09:27
  • Me neither. I'd do logging in in managed bean's action method and logging out in a HTTP session listener. Servlet filter is a wrong way to handle the problem, as auditing shouldn't be done upon every request. – skuntsel Apr 01 '13 at 09:38
  • @chkal Edited the question, omitting the servlet filter part – Serkan Arıkuşu Apr 01 '13 at 09:50
  • If you want to audit login then get a hook to login action. Otherwise, I'd go for hooking up to HTTP session creation and destruction via listener to get login/logout events. – skuntsel Apr 01 '13 at 10:00
  • @skuntsel This is not just for login. This is for every jsf page request – Serkan Arıkuşu Apr 01 '13 at 10:04
  • 2
    It's quite exotic. Then you're left with servlet filter or phase listener indeed. – skuntsel Apr 01 '13 at 10:16
  • @skuntsel I am sure that you know something that I do not. Searching for the phase listener, thankx – Serkan Arıkuşu Apr 01 '13 at 10:24
  • @SerkanArıkuşu A phase listener will still result in code duplication. The servlet filter is your best and cleanest bet IMO – kolossus Apr 01 '13 at 12:56
  • @kolossus With a servlet filter, can I also log the logged in user? – Serkan Arıkuşu Apr 01 '13 at 13:09
  • @SerkanArıkuşu, yes you can get the logged in user. You just need to make sure the filter applies to URLs/resources that require the user to be logged in – kolossus Apr 01 '13 at 13:15
  • As you're probably storing current user as a session attribute, you can of course access it from within the filter, be the user object null or not. – skuntsel Apr 01 '13 at 15:15
  • What exactly needs to be logged? The HTTP POST request? Or the JSF invoke application phase? Or the backing bean method invocation? Or the business service invocation? If business service, are you using EJB for that? – BalusC Apr 01 '13 at 20:56
  • @BalusC edited the question to be more understandable. Thanks – Serkan Arıkuşu Apr 02 '13 at 07:12

1 Answers1

0

You could either use a servlet filter or a JSF phase listener for that. Use a filter if you want to log EVERY request, including CSS and JavaScript resources. If you are just interested in the JSF requests, you can use a phase listener. Just hook into "before restore view" phase and log whatever you want.

chkal
  • 5,598
  • 21
  • 26
  • Looking at http://stackoverflow.com/a/9844616/305142 (last paragraph), I understand that if I use a dao method, postconstruct is the preferred way. What is your opinion on that? – Serkan Arıkuşu Apr 01 '13 at 10:34