Jump to content
We've recently updated our Privacy Statement, available here ×
  • How to add tenant and username to the log file


    gdmoreno
    • Features: Filters, Logging Product: JasperReports® Server

    Introduction

    The Jaspersoft out-of-the-box functionality does not include tenant and username information in the logs, but this simple customization leveraging web application filters and Log4J functionality allows you to capture this information too. You'll need to test and adapt to your particular situation.

    These are the high-level steps:
    1. Create a filter that takes the username and tenant information and adds it to the logging object
    2. Add this filter to the FilterChainProxy
    3. Modify the log4j.properties file to reference the username and tenant information you want to log
    4. Deploy filter and re-start the server

    Step 1 - Create the filter

    Either compile this Filter class or add the logic you find in the doFilter method to any other existing filter you may have developed. It leverages the MDC object from the Log4J package.

    package com.jaspersoft.custom.filters;import java.io.IOException;import javax.servlet.*;import javax.servlet.http.*;import com.jaspersoft.jasperserver.api.metadata.user.domain.impl.client.MetadataUserDetails;import org.springframework.security.core.Authentication;import org.springframework.security.core.context.SecurityContext;import org.springframework.security.core.context.SecurityContextHolder;import org.apache.log4j.MDC;/*** Created by gmoreno on 2/9/2016.*/public class AddTenantUsernameFilter implements Filter {    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)        throws IOException, ServletException {        HttpServletRequest request = (HttpServletRequest) req;        SecurityContext context = SecurityContextHolder.getContext();        if (context != null) {            MetadataUserDetails user = null;            Authentication auth = context.getAuthentication();            if (auth != null && auth.getPrincipal() != null) {                if (auth.getPrincipal() instanceof MetadataUserDetails) {                    user = (MetadataUserDetails) auth.getPrincipal();                    //NDC.push("Username = " + user.getUsername() + "|" + user.getTenantId());                    MDC.put("Username", user.getUsername() + "|" + user.getTenantId() + "n");                }            }        }        chain.doFilter(req, res);        //MDC.remove("Username");    }    public void init (FilterConfig config) throws ServletException { }    public void destroy () { }}[/code]

     


    Step 2 - add filter to Spring configuration file

    Add this:

    to the applicationContext-security-web.xml file found in the [TOMCAT_HOME]/webapps/jaspserver-pro/WEB-INF folder.
     
    Add that bean ID value to the security:filter-chain whose pattern is "/**" to the end of the filters attribute.
     
     

    Step 3 - Modify the Log4J properties file

    Find the log4j.properties file in the [TOMCAT_HOME]/webapps/jasperserver-pro/WEB-INF folder, and open in a text editor. Change this:
    log4j.appender.fileout.layout.conversionPattern=%d{ISO8601} %5p %c{1},%t:%L - %m%n[/code]
    To this:
    log4j.appender.fileout.layout.conversionPattern=%d{ISO8601} %5p %c{1},%t:%L - %m%n -- %X{Username}[/code]
    the %X{Username} references the value that's store for the Username key.
     
    An example log line will now show up as:
    2016-02-09 13:16:14,927 DEBUG JRJdbcQueryExecuter,http-nio-8080-exec-3:144 - DB is PostgreSQL version 9.3.9 (9/3)-- jasperadmin|organization_1[/code]
    The default Log4J configuration doesn't contain that information.
     

    Step 4 - Final steps

    Make sure that you've deployed the compiled class to /jasperserver-pro/WEB-INF/classes or packaged it up in JAR file in the /jasperserver-pro/WEB-INF/lib directory. Make sure to re-start your server.
     

    Resources

     
     

    User Feedback

    Recommended Comments

    There are no comments to display.



    Guest
    This is now closed for further comments

×
×
  • Create New...