Uploaded image for project: 'Magnolia'
  1. Magnolia
  2. MAGNOLIA-2635

Trace current URL/userid for logging

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Closed
    • Priority: Minor
    • Resolution: Fixed
    • Affects Version/s: 4.0
    • Fix Version/s: 4.0
    • Component/s: core
    • Labels:
      None

      Description

      This is something that I would like to see added by default, since we are in RC phase I'll describe it here to decide if/when it should be added.

      Using log4j (and also with slf4j) we can add useful informations to logs by adding some variables to MDC by default (mapped diagnostic context, see http://wiki.apache.org/logging-log4j/NDCvsMDC ).

      Those additional info should be added by a filter. The code below shows what the filter should do (I am not attaching a proper patch since this is only for discussion). The filter should go exactly after the context filter (or we could decide to put it into the context filter maybe.

          public void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
              throws IOException, ServletException
          {
              try
              {
                  MDC.put("requesturi", request.getRequestURI());
                  MDC.put("userid", MgnlContext.getUser().getName());
              }
              catch (Throwable e)
              {
                  // whatever it happens, only log
                  log.debug(e.getMessage(), e);
              }
              try
              {
                  chain.doFilter(request, response);
              }
              finally
              {
                  // cleanup
                  MDC.remove("requesturi");
                  MDC.remove("userid");
              }
          }
      
      

      Doing this, anywhere in the log4j.xml log patterns the following placeholders can be used in order to print out the current uri and the logged in user:

      %X{requesturi}
      %X{userid}
      

      This looks very useful also in the mail appender, if you enable it in running sites.
      This is an example of the content that can be added to error mails:

       ===================================
       My Magnolia app
       Version: 1.4.0
       Server: c139343z
       Url: /.magnolia/pages/test.html
       User: joe
       Number of occurrences: 2
       ===================================
      
      [c020890-sun01] ERROR 23.02.2009 01:07:08 info.magnolia.module.exchangesimple.SimpleSyndicator SimpleSyndicator.java(run:77) -- Failed to activate content.
      info.magnolia.cms.exchange.ExchangeException: Message received from subscriber: Activation failed | /NZ/large
             at info.magnolia.module.exchangesimple.SimpleSyndicator.activate(SimpleSyndicator.java:175)
             at info.magnolia.module.exchangesimple.SimpleSyndicator$1.run(SimpleSyndicator.java:75)
             ....
             .... (the usual stacktrace)
      
       ===================================
      
      

      Although this can be easily implemented I think that at least the filter that pushes informations to log4j MDC should be added by default. What about adding it for 4.0?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              fgiust Fabrizio Giustina
              Reporter:
              fgiust Fabrizio Giustina
              Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved:
                Date of First Response: