Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6573863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:11:14+00:00 2026-05-25T15:11:14+00:00

The web application on which I am working occasionally develops data integrity issues for

  • 0

The web application on which I am working occasionally develops data integrity issues for some of the users. I’d like to turn on trace level logging, but since we are dealing with 100s of requests per second trace logging every single request is out of the question.

Is there a way with log4j to be able to log conditionally? In other words, I would like to be able to get trace logs only when specific users make a request. Since I don’t know beforehand which users will be affected, I cannot simply temporarily hard-code usernames.

Edit:

I think I need to be a little clearer. I can easily add conditions to my log statements. For example

Logger logger = Logger.getLogger("foo");
String usernameFilter = "piglet";
String username = request.getParameter("username");
logger.setLevel(usernameFilter.equals(username) ? Level.TRACE : Level.INFO);
if (logger.isTraceEnabled()) {
   logger.trace("blah blah blah");
}

The difficulty is dynamically altering the condition that sets the log level. In other words, in the example above, how can I set the value of usernameFilter, other than hard-coding it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T15:11:15+00:00Added an answer on May 25, 2026 at 3:11 pm

    You want to look at Nested Diagnostic Contexts or Mapped Diagnostic Contexts in log4j or slf4j. An NDC/MDC allows you to insert data into your session that can be filtered by log4j.

    So you would define the user name to be in the NDC and then you can change the log4j.properties to change the logging level for specific users.

    An MDC uses a Map, whereas an NDC is based upon a stack principle. If you’re using slf4j, you can even create separate log files depending upon the information in your MDC.

    For instance, we did this when users logged into a website. We wanted to trace what a particular user was doing (retrospectively), so we added the user name and session id to the NDC, and then we could post filter on those.

    The code was similar to the following:

    public class LoggingFilter implements Filter {
        @Override
        public void doFilter(ServletRequest request, ServletResponse response,
                FilterChain chain) throws IOException, ServletException {
            MDC.put("username", session.getParameter("username")); // or where ever t is stored
            chain.doFilter(request, response);
        }
    }
    

    In your log4j.xml, this filters based upon the user:

      <appender name="UserDebug" class="org.apache.log4j.RollingFileAppender">
        <param name="File" value="userdebug.log"/>
        <param name="Append" value="true"/>
        <param name="MaxFileSize" value="5000KB"/>
        <param name="maxBackupIndex" value="5"/> 
              <layout class="org.apache.log4j.PatternLayout">
                      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] user:%X{username} %-5p - %m%n" />
              </layout>
    
              <filter class="org.apache.log4j.varia.StringMatchFilter">
                      <param name="StringToMatch" value="user:fred" />
                      <param name="AcceptOnMatch" value="true" />
              </filter>
    
          <filter class="org.apache.log4j.varia.DenyAllFilter"/>
      </appender>
    

    %X{key} outputs the value of MDC.get(key) in an MDC. If you wanted a more complex filter, you can extend it yourself, and look at the values in the MDC yourself.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working profesionally on a php web application which contains contacts, among other data.
I am working on a web application in Java which gets data from servlets
We're working on some Ruby on Rails web application which is currently in English
I am working on a web-application in which dynamically-created images are used to display
I have created a Web Application in asp.net 2.0. which is working fine on
I am working on a .NET 3.5 project which consists of a web application
I'm working on an ASP .NET 2.0 site which uses a Web Application project
I have a web application which uses URLs that look like this: http://library.example.com/Register.aspx?query=academic&key=586c70bb-5683-419c-aae9-e596af9ab66a (The
I'm working on a web application which will be used for classifying photos of
I am working on a web application which requires the user to login before

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.