You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by Lisa <li...@purpleblade.net> on 2007/01/17 01:30:18 UTC

Use category to limit by package name possible?

I have read all the documentation I could find on using an XML config file for
Log4J (slim pickings) but can not figure out how to log INFO messages to a file
that come from a specific package.

Any help would be appreciated.

I have a file appender that I want to limit to:
 1.  info
 2.  by package name

My appender looks like this:
    <!-- ******************************************************************* -->
    <!-- Appender used for all JSF debugging -->
    <appender name="JSF.INFO.LOG"
              class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="jsf.info.log"/>
        <param name="Append" value="false"/>
        <param name="ImmediateFlush" value="true"/>
        <param name="Threshold" value="info"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                   value="%d %-5p [%t] %c %C (%F:%L) - %m\n"/>
        </layout>
    </appender>

The limiting categories looks like this:
    <!-- JSF Limiting Categories -->
    <!-- myfaces-api -->
    <category
            name="org.apache.log4j.xml"
            additivity="false"
            class="javax.faces">
        <priority value="info"/>
        <appender-ref ref="JSF.INFO.LOG"/>
    </category>

    <!-- myfaces-impl -->
    <category
            name="org.apache.log4j.xml"
            additivity="false"
            class="org.apache.myfaces">
        <priority value="info"/>
        <appender-ref ref="JSF.INFO.LOG"/>
    </category>

The idea is to have all INFO messages from org.apache.myfaces & javax.faces go
to a jsf.info.log file.


---
Here is the exception that I get what I get when I launch my servlet/jsp/jsf
container (running under tomcat)


log4j: Desired logger sub-class: [javax.faces]
log4j:ERROR Could not retrieve category [org.apache.log4j.xml]. Reported error
follows.
java.lang.ClassNotFoundException: javax.faces
        at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(URLClassLoader.java:188)


---
I have the javax.faces (jar) packaged in my web application (war), which is then
deployed to Tomcat (which is now configured to use log4j)

---
Also is is possilbe to specify that the log file should go under
$CATALINA_HOME/logs?

How do I specify in the XML file where the log file should go?

Thanks

Lisa


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Use category to limit by package name possible?

Posted by Curt Arnold <ca...@apache.org>.
On Jan 17, 2007, at 12:17 AM, Lisa wrote:

>
> I was under the impression that name="org.apache.myfaces" property  
> of <logger>
> for example would be the way the logger knew to only log debug/info  
> from
> everything containing that package name.
>
> <P>
> If I name it anything I want, like name="idMyfaces" how does the  
> logger limit
> output confined to "org.apache.myfaces.*" ?
>
> <P>
> Is there something I am missing?
>
> <P>
> Lisa
>


Log4j does not impose a logging strategy, but it allows a large  
number of different strategies.  A common strategy would be to use  
loggers named with the same name as the class name which would allow  
you to segregate messages from a particular class or package.   
However, another developer might want to segregate messages by  
function or audience and could use logger names like  
"database.user.update", "audit", "finance.debit".  The essential  
aspect is not the names of the classes, but the names of the loggers  
used within the implementation (knowing that they may, but aren't  
required to be the same).

I would suggest starting with a very simple configuration file where  
a file appender is attached to the root logger with a pattern layout  
that contains %c so the logger name appears in the output.  After you  
have collected a sample log, examine the logger names that are  
actually used by your application.  At that point, you could post  
another message with excerpts of the captured log file and a  
description of what you would like suppressed.



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Use category to limit by package name possible?

Posted by Lisa <li...@purpleblade.net>.
Curt Arnold <carnold <at> apache.org> writes:


> The exception occurs since you specified that you wanted to use the  
> classes "javax.faces" and "org.apache.myfaces" as implementations of  
> the org.apache.log4j.Category contract.     As previously noted, the  
> term "logger" has replaced "category" long time ago (but "category"  
> is still there for compatibility).  Since you did not implement  
> classes named "javax.faces" or "org.apache.myfaces" that extended  
> "org.apache.log4j.Category", you were bound to get an exception.
> 
> You appear like you may be hoping that log4j can magically add  
> logging code to an implementation classes in "org.apache.myfaces" or  
> "javax.faces" packages.  Log4j and other common logging frameworks  
> require explicitly coding logging requests within a body of code and  
> the authors of the code choose their own naming convention.  The type  
> of behavior that you may be looking for would require an aspect  
> oriented language like AspectJ.
> 


I was under the impression that name="org.apache.myfaces" property of <logger>
for example would be the way the logger knew to only log debug/info from
everything containing that package name.  

<P>
If I name it anything I want, like name="idMyfaces" how does the logger limit
output confined to "org.apache.myfaces.*" ?

<P>
Is there something I am missing?

<P>
Lisa


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Use category to limit by package name possible?

Posted by Curt Arnold <ca...@apache.org>.
On Jan 16, 2007, at 8:13 PM, Lisa wrote:
>
....
> Log4J startup gives the following error message:
>   log4j:WARN Attribute value "com.presence" of type ID must be  
> unique within
> the document.
>
> So how do you have two loggers that limit to the same package but  
> different
> levels (debug,info)?
>

I agree with the other comments, but I thought it might be helpful to  
explain the causes for the warnings/exceptions that you are running  
into.

In this case, you have conflicting configurations for the same  
logger.  Loggers are identified by their names.  There can only be  
one logger named "org.apache.myfaces" in a logging hierarchy (and  
there is usually only one of those per app).  Calling Logger.getLogger 
("org.apache.myfaces") will always return the same logger.  Logger  
names are not inherently tied to class names, but it is a very common  
pattern for developers to use logger names that mimic class names.   
However, you are free to design any logger naming convention that  
fits your needs.  The only magic thing about names is that a period  
(".") is used to separate fragments of the name.

>   <!-- myfaces-impl -->
>     <category
>             name="org.apache.log4j.xml"
>             additivity="false"
>             class="org.apache.myfaces">
>         <priority value="info"/>
>         <appender-ref ref="JSF.INFO.LOG"/>
>     </category>
>
> The idea is to have all INFO messages from org.apache.myfaces &  
> javax.faces go
> to a jsf.info.log file.
>
>
> ---
> Here is the exception that I get what I get when I launch my  
> servlet/jsp/jsf
> container (running under tomcat)
>
>
> log4j: Desired logger sub-class: [javax.faces]
> log4j:ERROR Could not retrieve category [org.apache.log4j.xml].  
> Reported error
> follows.
> java.lang.ClassNotFoundException: javax.faces
>         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)


The exception occurs since you specified that you wanted to use the  
classes "javax.faces" and "org.apache.myfaces" as implementations of  
the org.apache.log4j.Category contract.     As previously noted, the  
term "logger" has replaced "category" long time ago (but "category"  
is still there for compatibility).  Since you did not implement  
classes named "javax.faces" or "org.apache.myfaces" that extended  
"org.apache.log4j.Category", you were bound to get an exception.

You appear like you may be hoping that log4j can magically add  
logging code to an implementation classes in "org.apache.myfaces" or  
"javax.faces" packages.  Log4j and other common logging frameworks  
require explicitly coding logging requests within a body of code and  
the authors of the code choose their own naming convention.  The type  
of behavior that you may be looking for would require an aspect  
oriented language like AspectJ.


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Use category to limit by package name possible?

Posted by Paul Smith <ps...@aconex.com>.
My advice:  Don't split your logs into a Debug and Info log files.   
Not that log4j can't do that (see more below) but seriously consider  
that both the debug and info logs are even more useful when you have  
the context of them together.  So my advice is to have one file  
appender, attached to all loggers you wish to capture logs from, and  
set your loggers to DEBUG.  this will give you both Debug and Info  
(and other levels) in the one file making understanding what is going  
on much simpler, since you can see them in sequence.


However... if you must... and you really want to.. Then create 2 file  
appenders, and set the _Appenders_ to have a Filter that only accepts  
the Level you are interested in (you might need to write a simple  
filter class for this), Debug or Info (for which ever file you  
wanted), then set your loggers to the lowest threshold you want to  
capture (debug) and attach BOTH appenders to all the loggers.  This  
way a logger emits a DEBUG log event, which goes to both appenders,  
but each appender decides whether to append it to the file, because  
of the Filter.

cheers,

Paul


On 17/01/2007, at 1:13 PM, Lisa wrote:

>
> James Stauffer <stauffer.james <at> gmail.com> writes:
>
>>
>> 1. <category> -> <logger>
>> 2. Remove class attribute from category/logger.
>> 3. Define a <root> node at the bottom.
>
>
> Thanks!  but I still have a few problems.  #3 <root> node was not  
> required.  The
> appender stayed the same.  the category (changed to logger) now  
> looks like this:
> (note priority is no longer an acceptable tag - changed to level)
>
>     <!-- myfaces-impl -->
>     <logger
>             name="org.apache.myfaces"
>             additivity="false">
>         <level value="debug"/>
>         <appender-ref ref="JSF.INFO.LOG"/>
>     </logger>
>
> But now I want a debug log file with debug messages and above and  
> an info file
> with info and above on the same package.  It appears to me that the
> name="<package>" is the limiting param but also functions as an id  
> for the
> <logger> tag so log4j is complaining.
>
>     <!-- presence-tech (debug) -->
>     <logger
>             name="com.presence"
>             additivity="false">
>         <level value="debug"/>
>         <appender-ref ref="PT.DEBUG.LOG"/>
>     </logger>
>
>     <!-- presence-tech (info) -->
>     <logger
>             name="com.presence"
>             additivity="false">
>         <level value="info"/>
>         <appender-ref ref="PT.INFO.LOG"/>
>     </logger>
>
> How do I create two loggers that limit to com.presence (one logger for
> debug and one for info)?
>
> Log4J startup gives the following error message:
>   log4j:WARN Attribute value "com.presence" of type ID must be  
> unique within
> the document.
>
> So how do you have two loggers that limit to the same package but  
> different
> levels (debug,info)?
>
>
> Thanks
>
> Lisa
>
>
>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>

Paul Smith
Engineering Manager

Aconex
The easy way to save time and money on your project

696 Bourke Street, Melbourne,
VIC 3000, Australia
Tel: +61 3 9240 0200  Fax: +61 3 9240 0299
Email: psmith@aconex.com  www.aconex.com

This email and any attachments are intended solely for the addressee.  
The contents may be privileged, confidential and/or subject to  
copyright or other applicable law. No confidentiality or privilege is  
lost by an erroneous transmission. If you have received this e-mail  
in error, please let us know by reply e-mail and delete or destroy  
this mail and all copies. If you are not the intended recipient of  
this message you must not disseminate, copy or take any action in  
reliance on it. The sender takes no responsibility for the effect of  
this message upon the recipient's computer system.




Re: Use category to limit by package name possible?

Posted by Lisa <li...@purpleblade.net>.
James Stauffer <stauffer.james <at> gmail.com> writes:

> 
> 1. <category> -> <logger>
> 2. Remove class attribute from category/logger.
> 3. Define a <root> node at the bottom.


Thanks!  but I still have a few problems.  #3 <root> node was not required.  The
appender stayed the same.  the category (changed to logger) now looks like this:
(note priority is no longer an acceptable tag - changed to level)

    <!-- myfaces-impl -->
    <logger
            name="org.apache.myfaces"
            additivity="false">
        <level value="debug"/>
        <appender-ref ref="JSF.INFO.LOG"/>
    </logger>

But now I want a debug log file with debug messages and above and an info file
with info and above on the same package.  It appears to me that the
name="<package>" is the limiting param but also functions as an id for the
<logger> tag so log4j is complaining. 

    <!-- presence-tech (debug) -->
    <logger
            name="com.presence"
            additivity="false">
        <level value="debug"/>
        <appender-ref ref="PT.DEBUG.LOG"/>
    </logger>

    <!-- presence-tech (info) -->
    <logger
            name="com.presence"
            additivity="false">
        <level value="info"/>
        <appender-ref ref="PT.INFO.LOG"/>
    </logger>

How do I create two loggers that limit to com.presence (one logger for 
debug and one for info)?

Log4J startup gives the following error message:
  log4j:WARN Attribute value "com.presence" of type ID must be unique within 
the document.

So how do you have two loggers that limit to the same package but different
levels (debug,info)?


Thanks

Lisa






---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Use category to limit by package name possible?

Posted by James Stauffer <st...@gmail.com>.
1. <category> -> <logger>
2. Remove class attribute from category/logger.
3. Define a <root> node at the bottom.

On 1/16/07, Lisa <li...@purpleblade.net> wrote:
> I have read all the documentation I could find on using an XML config file for
> Log4J (slim pickings) but can not figure out how to log INFO messages to a file
> that come from a specific package.
>
> Any help would be appreciated.
>
> I have a file appender that I want to limit to:
>  1.  info
>  2.  by package name
>
> My appender looks like this:
>     <!-- ******************************************************************* -->
>     <!-- Appender used for all JSF debugging -->
>     <appender name="JSF.INFO.LOG"
>               class="org.apache.log4j.DailyRollingFileAppender">
>         <param name="File" value="jsf.info.log"/>
>         <param name="Append" value="false"/>
>         <param name="ImmediateFlush" value="true"/>
>         <param name="Threshold" value="info"/>
>         <layout class="org.apache.log4j.PatternLayout">
>             <param name="ConversionPattern"
>                    value="%d %-5p [%t] %c %C (%F:%L) - %m\n"/>
>         </layout>
>     </appender>
>
> The limiting categories looks like this:
>     <!-- JSF Limiting Categories -->
>     <!-- myfaces-api -->
>     <category
>             name="org.apache.log4j.xml"
>             additivity="false"
>             class="javax.faces">
>         <priority value="info"/>
>         <appender-ref ref="JSF.INFO.LOG"/>
>     </category>
>
>     <!-- myfaces-impl -->
>     <category
>             name="org.apache.log4j.xml"
>             additivity="false"
>             class="org.apache.myfaces">
>         <priority value="info"/>
>         <appender-ref ref="JSF.INFO.LOG"/>
>     </category>
>
> The idea is to have all INFO messages from org.apache.myfaces & javax.faces go
> to a jsf.info.log file.
>
>
> ---
> Here is the exception that I get what I get when I launch my servlet/jsp/jsf
> container (running under tomcat)
>
>
> log4j: Desired logger sub-class: [javax.faces]
> log4j:ERROR Could not retrieve category [org.apache.log4j.xml]. Reported error
> follows.
> java.lang.ClassNotFoundException: javax.faces
>         at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
>         at java.security.AccessController.doPrivileged(Native Method)
>         at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
>
>
> ---
> I have the javax.faces (jar) packaged in my web application (war), which is then
> deployed to Tomcat (which is now configured to use log4j)
>
> ---
> Also is is possilbe to specify that the log file should go under
> $CATALINA_HOME/logs?
>
> How do I specify in the XML file where the log file should go?
>
> Thanks
>
> Lisa
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>


-- 
James Stauffer        http://www.geocities.com/stauffer_james/
Are you good? Take the test at http://www.livingwaters.com/good/

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org