Re: Re: T5, tapestry-spring-security, slf4j MDC
Hello, I simplified the code so it does not use RequestGlobals which was giving me NULL for this.requestGlobals.getHTTPServletRequest() AppModule.java = ... binder.bind(HttpServletRequestFilter.class, RequestLoggingFilter.class).withId(RequestLoggingFilter); ... public static void contributeHttpServletRequestHandler( OrderedConfigurationHttpServletRequestFilter configuration, @InjectService(RequestLoggingFilter) HttpServletRequestFilter myfilter) { configuration.add(myfilter, myfilter, before:*); } RequestLoggingFilter.java = public class RequestLoggingFilter implements HttpServletRequestFilter { @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, request.getRemoteAddr()); HttpSession se = request.getSession(false); if (se != null) { String id = se.getId(); if (id == null) { id = ; } MDC.put(sessionID, id); } try { return handler.service(request, response); } finally { MDC.remove(remoteIP); MDC.remove(sessionID); } } } Thanks for hints. 2009/10/9 Olle Hallin olle.hal...@hit.se Do like this: MDC.put(xxx, ...); MDC.put(yyy, ...); try { return handler.service(request, response); } finally { MDC.remove(xxx) MDC.remove(yyy) } or else you will have problems when your request throws an exception Olle Hallin Senior Java Developer and Architect olle.hal...@crisp.se www.crisp.se 2009/10/9 dirk.latterm...@bgs-ag.de Hi! Borut Bolčina borut.bolc...@gmail.com schrieb am 09.10.2009 14:55:32: this is what I did: public class RequestLoggingFilter implements HttpServletRequestFilter { public final RequestGlobals requestGlobals; public RequestLoggingFilter(final RequestGlobals requestGlobalss) { this.requestGlobals = requestGlobalss; } @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); return handler.service(request, response); } } but I feel I am missing something. Where do I put the code: MDC.remove(remoteIP); MDC.remove(sessionID); In a similar situation, I used something like @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); boolean result = handler.service(request, response); MDC.remove(remoteIP); MDC.remove(sessionID); return result; } } but I'm not sure if it does the right thing in all situations. Dirk BGS Beratungsgesellschaft Software Systemplanung AG Niederlassung Köln/Bonn Grantham-Allee 2-8 53757 Sankt Augustin Fon: +49 (0) 2241 / 166-500 Fax: +49 (0) 2241 / 166-680 www.bgs-ag.de Geschäftssitz Mainz Registergericht Amtsgericht Mainz HRB 62 50 Aufsichtsratsvorsitzender Klaus Hellwig Vorstand Hermann Kiefer Nils Manegold Thomas Reitz
Re: T5, tapestry-spring-security, slf4j MDC
We do something similar. We have implemented a RequestFilter that pushes the HttpSession ID (if any) onto the Log4j Nested Diagnostics Context (NDC) before the request, and pops it afterwards. I guess that the same approach could be used for pushing usernames in the SLF4J MDC, provided that you can get hold of them from a RequestFilter. Good to know: Tapestry 5.1 stores @SessionStateObjects in the session under the key sso: + MySessionStateObject.class.getName(). (aso: in 5.0.*) HTH, Olle Senior Java Developer and Architect olle.hal...@crisp.se www.crisp.se 2009/10/9 Borut Bolčina borut.bolc...@gmail.com Hello, has anybody implemented logging with MDC %X{username}? I would like to track users in my logs by their username and/or IP address. Before I dive in, I just wanna ask if someone has already done it? Thanks, Borut
Re: T5, tapestry-spring-security, slf4j MDC
Olle thanks, this is what I did: public class RequestLoggingFilter implements HttpServletRequestFilter { public final RequestGlobals requestGlobals; public RequestLoggingFilter(final RequestGlobals requestGlobalss) { this.requestGlobals = requestGlobalss; } @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); return handler.service(request, response); } } and in my AppModule.java public RequestLoggingFilter buildRequestLoggingFilter(final RequestGlobals requestGlobals) { return new RequestLoggingFilter(requestGlobals); } public static void contributeHttpServletRequestHandler( OrderedConfigurationHttpServletRequestFilter configuration, @InjectService(RequestLoggingFilter) HttpServletRequestFilter myfilter) { configuration.add(myfilter, myfilter, before:*); } but I feel I am missing something. Where do I put the code: MDC.remove(remoteIP); MDC.remove(sessionID); Thanks, Borut 2009/10/9 Olle Hallin olle.hal...@hit.se We do something similar. We have implemented a RequestFilter that pushes the HttpSession ID (if any) onto the Log4j Nested Diagnostics Context (NDC) before the request, and pops it afterwards. I guess that the same approach could be used for pushing usernames in the SLF4J MDC, provided that you can get hold of them from a RequestFilter. Good to know: Tapestry 5.1 stores @SessionStateObjects in the session under the key sso: + MySessionStateObject.class.getName(). (aso: in 5.0.*) HTH, Olle Senior Java Developer and Architect olle.hal...@crisp.se www.crisp.se 2009/10/9 Borut Bolčina borut.bolc...@gmail.com Hello, has anybody implemented logging with MDC %X{username}? I would like to track users in my logs by their username and/or IP address. Before I dive in, I just wanna ask if someone has already done it? Thanks, Borut
Antwort: Re: T5, tapestry-spring-security, slf4j MDC
Hi! Borut Bolčina borut.bolc...@gmail.com schrieb am 09.10.2009 14:55:32: this is what I did: public class RequestLoggingFilter implements HttpServletRequestFilter { public final RequestGlobals requestGlobals; public RequestLoggingFilter(final RequestGlobals requestGlobalss) { this.requestGlobals = requestGlobalss; } @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); return handler.service(request, response); } } but I feel I am missing something. Where do I put the code: MDC.remove(remoteIP); MDC.remove(sessionID); In a similar situation, I used something like @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); boolean result = handler.service(request, response); MDC.remove(remoteIP); MDC.remove(sessionID); return result; } } but I'm not sure if it does the right thing in all situations. Dirk BGS Beratungsgesellschaft Software Systemplanung AG Niederlassung Köln/Bonn Grantham-Allee 2-8 53757 Sankt Augustin Fon: +49 (0) 2241 / 166-500 Fax: +49 (0) 2241 / 166-680 www.bgs-ag.de Geschäftssitz Mainz Registergericht Amtsgericht Mainz HRB 62 50 Aufsichtsratsvorsitzender Klaus Hellwig Vorstand Hermann Kiefer Nils Manegold Thomas Reitz
Re: Re: T5, tapestry-spring-security, slf4j MDC
Do like this: MDC.put(xxx, ...); MDC.put(yyy, ...); try { return handler.service(request, response); } finally { MDC.remove(xxx) MDC.remove(yyy) } or else you will have problems when your request throws an exception Olle Hallin Senior Java Developer and Architect olle.hal...@crisp.se www.crisp.se 2009/10/9 dirk.latterm...@bgs-ag.de Hi! Borut Bolčina borut.bolc...@gmail.com schrieb am 09.10.2009 14:55:32: this is what I did: public class RequestLoggingFilter implements HttpServletRequestFilter { public final RequestGlobals requestGlobals; public RequestLoggingFilter(final RequestGlobals requestGlobalss) { this.requestGlobals = requestGlobalss; } @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); return handler.service(request, response); } } but I feel I am missing something. Where do I put the code: MDC.remove(remoteIP); MDC.remove(sessionID); In a similar situation, I used something like @Override public boolean service(HttpServletRequest request, HttpServletResponse response, HttpServletRequestHandler handler) throws IOException { MDC.put(remoteIP, this.requestGlobals.getHTTPServletRequest().getRemoteAddr()); String s = this.requestGlobals.getHTTPServletRequest().getRequestedSessionId(); if (s == null) { s = ; } MDC.put(sessionID, s); boolean result = handler.service(request, response); MDC.remove(remoteIP); MDC.remove(sessionID); return result; } } but I'm not sure if it does the right thing in all situations. Dirk BGS Beratungsgesellschaft Software Systemplanung AG Niederlassung Köln/Bonn Grantham-Allee 2-8 53757 Sankt Augustin Fon: +49 (0) 2241 / 166-500 Fax: +49 (0) 2241 / 166-680 www.bgs-ag.de Geschäftssitz Mainz Registergericht Amtsgericht Mainz HRB 62 50 Aufsichtsratsvorsitzender Klaus Hellwig Vorstand Hermann Kiefer Nils Manegold Thomas Reitz