https://issues.apache.org/bugzilla/show_bug.cgi?id=45368

           Summary: new log.isDebugEnabled()) like method checking whether
                    category and almost one appender are enabled for DEBUG
                    level
           Product: Log4j
           Version: 1.2
          Platform: PC
        OS/Version: Windows XP
            Status: NEW
          Severity: enhancement
          Priority: P2
         Component: Appender
        AssignedTo: log4j-dev@logging.apache.org
        ReportedBy: [EMAIL PROTECTED]


Created an attachment (id=22238)
 --> (https://issues.apache.org/bugzilla/attachment.cgi?id=22238)
diff of the modificated progect files

I have improved log4j, adding 
Category.isCategoryAndAlmostOneAppenderDebugEnabled() and its generalization
Category.isCategoryAndAlmostOneAppenderEnabledFor(Priority level) methods.

They are improvements respectively of Category.isDebugEnabled() and
Category.isEnabledFor(Priority level).

Category.isCategoryAndAlmostOneAppenderDebugEnabled checks whether category and
almost one appender are enabled for DEBUG level. 
Category.isCategoryAndAlmostOneAppenderEnabledFor(Priority level) do the same
thing for a given level.

I need these new methods because Category.isDebugEnabled() is too restrictive.
It checks only the category level, but I have to check also the appender
threshold.

To help you understand I explain my application context.

I work for a web application and I need one main log file and other
user-dedicated log files. All files must have root category, because they must
log all classes.
Main log file logs at ERROR threshold, to log only exception (for faster
application run).
User-dedicated log files log at DEBUG threshold, to log all things a particular
user do.

This mechanism is achieved using org.apache.log4j.NDC and Servlet. 
When a user logs in, the login servlet pushes username in org.apache.log4j.NDC
if user is present in a user-dedicated list; pushes “” if user is not
present. The same value is also stored in session. Each time a servlet is used,
NDC of the current thread is popped off and pushed in using the value stored in
session. 
FileNDCAppender.subAppend(LoggingEvent event), an hand-made appender who works
in a slightly different manner compared to buil-in appenders,  reads
event.getNDC() and, according to its value, it writes on main log file or on a
corresponding dedicated log file.
So a small number of final users can create log files at DEBUG threshold
without restarting application, without changing log4j.properties and without
creating delays to the normal application’s performances, but only logging
the application in by a specific username.

To do this, my log4j.properties is set as follows:


# -------------------------   main log file
log4j.rootCategory= DEBUG, main, dedicated
log4j.appender.main=Logging.FileNDCAppender
log4j.appender.main.NDC=false
log4j.appender.main.Threshold=ERROR
...

# -------------------------   dedicated log file
log4j.appender.dedicated=Logging.FileNDCAppender
log4j.appender.dedicated.NDC=true
log4j.appender.dedicated.Threshold=DEBUG
...

I have only one category and two appenders named main and dedicated. Both
appenders use Logging.FileNDCAppender. Main appender writes on main log file;
dedicated appender writes on all dedicated log files. The difference is set by
NDC attribute.

All works well, and I use this settings since 2004 at many customer.   
Recently I have wrote one method (called logIndex) very heavy. It creates a big
string reporting the state of a cache memory. 

         I want execute logIndex only if the string will be logged, 

hence only if the user is in user-dedicated list as above mentioned. So, at a
first time I thought to Category.isDebugEnabled():

if (log.isDebugEnabled())
{
        log.debug(logIndex());
}

Obviously it don’t work, because I’m forced to set category at DEBUG level
(otherwise
 dedicated appender can not write at DEBUG level). I achieved my target by

if (log.isCategoryAndAlmostOneAppenderDebugEnabled ())
{
        log.debug(logIndex());
}

It satisfies the condition:

Category is a DEBUG level and almost one appender is enabled


I means one appender is enabled when it satisfies threshold checks and a new
method called AppenderSkeleton.isEnabledFor(priority level) returns true.
AppenderSkeleton.isEnabledFor(priority level) checks appender’s threshold and
if satisfied executes AppenderSkeleton.isSubEnabledFor(priority level), who
returns true by default. If overridden it is possible to personalize appender
enable check.

In my application context I wrote
Logging.FileNDCAppender.isSubEnabledFor(Priority level) in this way:

      public boolean isSubEnabledFor(Priority level)
      {
            boolean dedicated = (org.apache.log4j.NDC.peek().length() > 0);

            if (NDC == false && dedicated)
            {
                  return false;
            }
            else if (NDC && dedicated == false)
            {
                  return false;
            }

            return true;
      }

where NDC is attribute NDC in log4j.properties.

Best Regards,

Antonio Bertuzzi


-- 
Configure bugmail: https://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to