You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by bu...@apache.org on 2008/07/09 17:39:06 UTC

DO NOT REPLY [Bug 45368] New: new log.isDebugEnabled()) like method checking whether category and almost one appender are enabled for DEBUG level

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: abertuzzi@datamanagement.it


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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


DO NOT REPLY [Bug 45368] new log.isDebugEnabled() like method checking whether category and at least one appender are enabled for DEBUG level

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45368





--- Comment #3 from Curt Arnold <ca...@apache.org>  2008-08-14 08:37:34 PST ---
I think your situation might be better addressed by a combination of LogMF from
the extras companion (with possible enhancements for deferring construction of
messages that require evaluating Object.toString()), the long dormant
MultiFileAppender project and use of message objects that can delay evaluation
until message formatting.  Those should not require changes to the core log4j
API and should offer similar performance to your approach.

If your performance issue is the unnecessary evaluation of functions in the
message parameter, you can delay those evaluations until message layout time by
passing a class whose toString() evaluates the expensive function.

private static class LogIndexEvaluator {
    private final MyClass obj;
    public LogIndexEvaluator(MyClass o) {
        if (o == null) {
           throw NullPointerException();
        }
       obj = o;
    }
    public String toString() {
       return String.valueOf(o.logIndex());
    }
}

....


if (log.isDebugEnabled()) {
    log.debug(new LogIndexEvaluator(this));
}

Construction of a small short lived object is pretty inexpensive in modern
JVM's.


-- 
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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


DO NOT REPLY [Bug 45368] new log.isDebugEnabled() like method checking whether category and at least one appender are enabled for DEBUG level

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45368


Curt Arnold <ca...@apache.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |WONTFIX




--- Comment #2 from Curt Arnold <ca...@apache.org>  2008-08-12 10:36:35 PST ---
I appreciate the patch, but it doesn't seem generally useful enough to justify
the changes to the log4j API at this point in the release cycle.  Adding as a
wish list item to log4j 2.0 (https://issues.apache.org/jira/browse/LOG4J2-24).


-- 
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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


DO NOT REPLY [Bug 45368] new log.isDebugEnabled() like method checking whether category and at least one appender are enabled for DEBUG level

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45368


Antonio Bertuzzi <ab...@datamanagement.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|new log.isDebugEnabled()    |new log.isDebugEnabled()
                   |like method checking whether|like method checking whether
                   |category and almost one     |category and at least one
                   |appender are enabled for    |appender are enabled for
                   |DEBUG level                 |DEBUG level




--- Comment #1 from Antonio Bertuzzi <ab...@datamanagement.it>  2008-07-15 07:41:21 PST ---
I made a mistake. In all the names of methods I wrote ALMOST in place AT LEAST. 
Feel free to correct 


-- 
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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org


DO NOT REPLY [Bug 45368] new log.isDebugEnabled() like method checking whether category and almost one appender are enabled for DEBUG level

Posted by bu...@apache.org.
https://issues.apache.org/bugzilla/show_bug.cgi?id=45368


Antonio Bertuzzi <ab...@datamanagement.it> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|new log.isDebugEnabled())   |new log.isDebugEnabled()
                   |like method checking whether|like method checking whether
                   |category and almost one     |category and almost one
                   |appender are enabled for    |appender are enabled for
                   |DEBUG level                 |DEBUG level




-- 
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: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org