You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@avalon.apache.org by Leif Mortenson <le...@tanukisoftware.com> on 2002/09/12 08:36:26 UTC

Problem with Logger Categories using the ECM

(Mainly to Peter)
I am having a problem with logger Categories using the ECM. Not sure
exactly when this started happening, but I just noticed it.

If I specify an attribute, logger="something", for a component in my
components.xml file then the logger assigned to that component is
"something.something" rather than just "something".

It appears that this only happens in cases where Selectors are used.
To make the ECM work with both Loggable as well as LogEnabled components,
a compatability layer was added to make thigs work cleanly. The ECM
internally creates and instance of a LogkitLoggerManager from the component
package. (Note this is not LogKitLoggerManager from the logger package)
This class appears to be acting as a proxy so that all requests for
either type
of logger are handled correctly.

The problem arrises as soon as LogkitLoggerManager.getLogKitManager() is
called.
This creates an instance of a Logger2LogKitManager class which is then
stored
in the m_logKitManager field of the LogkitLoggerManager class. All
future calls
to LogkitLoggerManager.getLogKitLoggerForCategory() now make use of that
object to get the LogKit loggers.

Ok. Internally, when Logger2LogKitManager.getLogger() is called (See
modified code below):
---
public org.apache.log.Logger getLogger( final String categoryName )
{
final Logger logger =
m_loggerManager.getLoggerForCategory( categoryName );
logger.info( "Logger2LogKitManager.getLogger( " + categoryName + " )
logger" );
final org.apache.log.Logger logkitLogger =
getHierarchy().getLoggerFor( categoryName );
logkitLogger.info( "Logger2LogKitManager.getLogger( " + categoryName + "
) logkitLogger 1" );
final LogKit2LoggerTarget target =
new LogKit2LoggerTarget( logger );
logkitLogger.setLogTargets( new LogTarget[ ] { target } );
logkitLogger.info( "Logger2LogKitManager.getLogger( " + categoryName + "
) logkitLogger 2" );
return logkitLogger;
}
---
You will get the following output:
---
09-12T14:24:28.519 INFO [something ]: Logger2LogKitManager.getLogger(
something ) logger
09-12T14:24:28.669 INFO [something ]: Logger2LogKitManager.getLogger(
something ) logkitLogger 1
09-12T14:24:28.749 INFO [something.something]:
Logger2LogKitManager.getLogger( something ) logkitLogger 2
---

Notice how logging to the logKitLogger works correctly until
setLogTargets() is called.

Looking at the source of the LogKit2LoggerTarget in the
getLoggerForEvent method, you
see the following code, once again with debug output added. Also added
'+ "LTK"' to the
generated category names to make it clearer what was happening.:
---
private Logger getLoggerForEvent( final LogEvent event )
{
final String category = event.getCategory();
Logger logger = m_logger;
logger.info( "LogKit2LoggerTarget.getLoggerForEvent(event) category='" +
category + "' 1" );
if( !"".equals( category ) )
{
logger = m_logger.getChildLogger( category + "LKT" );
}
logger.info( "LogKit2LoggerTarget.getLoggerForEvent(event) category='" +
category + "' 2" );
return logger;
}
---
Then someplaces, I would get this:
---
09-12T15:00:32.060 INFO [ ]:
LogKit2LoggerTarget.getLoggerForEvent(event) category='something' 1
09-12T15:00:32.120 INFO [somethingLKT]:
LogKit2LoggerTarget.getLoggerForEvent(event) category='something' 2
09-12T15:00:32.140 DEBUG [somethingLKT]: Message
---
But in others, I would get:
---
09-12T15:00:32.340 INFO [something ]:
LogKit2LoggerTarget.getLoggerForEvent(event) category='something' 1
09-12T15:00:32.350 INFO [something.somethingLKT]:
LogKit2LoggerTarget.getLoggerForEvent(event) category='something' 2
09-12T15:00:32.530 INFO [something.somethingLKT]: Message
---
So it does not appear to be possible to do much in this method.

Looking back at the Logger2LogKitManager.getLogger( categoryName ) method,
It looked like the following change would work. The actual category that
we want to
log to is provided in the LogEvent as seen above. So it seems like we
always want
to be getting a logger relative to the root.

This led me to make the following extremely simple fix:
---
RCS file:
/home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/Logger2LogKitManager.java,v
retrieving revision 1.3
diff -u -r1.3 Logger2LogKitManager.java
--- Logger2LogKitManager.java 29 Jul 2002 09:53:40 -0000 1.3
+++ Logger2LogKitManager.java 12 Sep 2002 06:31:29 -0000
@@ -36,7 +36,7 @@
public org.apache.log.Logger getLogger( final String categoryName )
{
final Logger logger =
- m_loggerManager.getLoggerForCategory( categoryName );
+ m_loggerManager.getLoggerForCategory( "" );
final org.apache.log.Logger logkitLogger =
getHierarchy().getLoggerFor( categoryName );
final LogKit2LoggerTarget target =
---

This seems to work great for me, but I wanted to check with the author
(Peter?)
to make sure that this will also work with other Logger implementations.
I am
using the LogKitLoggerManager.
It this looks Ok, then I will go ahead and commit it.

Figures ... ~4 hours for a 1 line fix :-/

Cheers,
Leif


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Problem with Logger Categories using the ECM

Posted by Peter Donald <pe...@apache.org>.
On Thu, 12 Sep 2002 16:36, Leif Mortenson wrote:
> This led me to make the following extremely simple fix:
> ---
> RCS file:
> /home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/exc
>alibur/component/Logger2LogKitManager.java,v retrieving revision 1.3
> diff -u -r1.3 Logger2LogKitManager.java
> --- Logger2LogKitManager.java 29 Jul 2002 09:53:40 -0000 1.3
> +++ Logger2LogKitManager.java 12 Sep 2002 06:31:29 -0000
> @@ -36,7 +36,7 @@
> public org.apache.log.Logger getLogger( final String categoryName )
> {
> final Logger logger =
> - m_loggerManager.getLoggerForCategory( categoryName );
> + m_loggerManager.getLoggerForCategory( "" );
> final org.apache.log.Logger logkitLogger =
> getHierarchy().getLoggerFor( categoryName );
> final LogKit2LoggerTarget target =
> ---
>
> This seems to work great for me, but I wanted to check with the author
> (Peter?)
> to make sure that this will also work with other Logger implementations.
> I am
> using the LogKitLoggerManager.
> It this looks Ok, then I will go ahead and commit it.

It looks good to me! Thanks for spotting it ;)

> Figures ... ~4 hours for a 1 line fix :-/

My fault ... oops ;)

-- 
Cheers,

Peter Donald
Sufficiently advanced science is 
 indistinguishable from magic" 
               -- Arthur C. Clarke


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Problem with Logger Categories using the ECM

Posted by Peter Donald <pe...@apache.org>.
On Fri, 13 Sep 2002 07:27, Peter Donald wrote:
> On Fri, 13 Sep 2002 01:47, Leif Mortenson wrote:
> > These changes seem to all be working, so I'll go ahead and commit them.
> > Let me know
> > if you see any problems.
>
> Looks goos to me.

Alternatively you could parse the names passed into getLogger() and strip the 
first element of name. ie

"" --> ""
"foo" --> ""
"foo.bar" --> "bar"
"foo.bar.baz" --> "bar.baz"

-- 
Cheers,

Peter Donald
*------------------------------------------------*
| You can't wake a person who is pretending      |
|       to be asleep. -Navajo Proverb.           |
*------------------------------------------------* 


--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Problem with Logger Categories using the ECM

Posted by Peter Donald <pe...@apache.org>.
On Fri, 13 Sep 2002 01:47, Leif Mortenson wrote:
> These changes seem to all be working, so I'll go ahead and commit them.
> Let me know
> if you see any problems.

Looks goos to me. 

-- 
Cheers,

Peter Donald
-----------------------------------------------------------
 Don't take life too seriously -- 
                          you'll never get out of it alive.
-----------------------------------------------------------



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>


Re: Problem with Logger Categories using the ECM

Posted by Leif Mortenson <le...@tanukisoftware.com>.
 (sigh)
This actually isn't working because I am unable to control log levels
from the
logkit.xml file. The log message displays to the screen as the "something"
category, but the LogKit manager is controlling the category as if it was
the "" category.

I tried switching things around like this:
---
public org.apache.log.Logger getLogger( final String categoryName )
{
final Logger logger =
m_loggerManager.getLoggerForCategory( categoryName );
final org.apache.log.Logger logkitLogger =
getHierarchy().getLoggerFor( "" );
final LogKit2LoggerTarget target =
new LogKit2LoggerTarget( logger );
logkitLogger.setLogTargets( new LogTarget[ ] { target } );
return logkitLogger;
}
---
But this does not work because the Logger assigned to logkitLogger is always
the same instance. This means that the log target is always set to the last
category called.

After playing with this some more, I ended up backing out the change in the
original message and then changing LogKit2LoggerTarget as follows:
---
RCS file:
/home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/LogKit2LoggerTarget.java,v
retrieving revision 1.2
diff -u -r1.2 LogKit2LoggerTarget.java
--- LogKit2LoggerTarget.java 2 Jun 2002 06:03:01 -0000 1.2
+++ LogKit2LoggerTarget.java 12 Sep 2002 08:54:53 -0000
@@ -46,7 +46,7 @@

public void processEvent( LogEvent event )
{
- final Logger logger = getLoggerForEvent( event );
+ final Logger logger = m_logger;

final String message = event.getMessage();
final Throwable throwable = event.getThrowable();
@@ -71,23 +71,5 @@
{
logger.fatalError( message, throwable );
}
- }
-
- /**
- * Retrieve Logger for event. If event is from a child
- * Log
- *
- * @param event the LogEvent
- * @return the Logger
- */
- private Logger getLoggerForEvent( final LogEvent event )
...
---
In summary, the target class was just simplified down so that the
m_logger is
always used. This made it possible to get rid of the getLoggerForEvent
method
completely.

So far this method has been passing all of my tests... So far..

Looking at the LogKit2LoggerTarget.createLogger method, I question
whether it
will work. It is doing exactly what I tried above. That didn't seem to work.

Moving on, I also saw some ways to simplfy and optimize the code in
LogkitLoggerManager. The above changes do not depend on these. They would
be in addition. I was noticing that once getLogKitManager() was called,
future
calls to getLoggerForCategory were being done using the Logger2LogKitManager
wrapper even though the original LoggerManager was available. I also made
getLogKitManager always be called when a getLogKitLoggerForCategory is
called
to remove some duplicate code. This also got rid of one call to
LogKit2LoggerTarget.createLogger (It is still being used by
AbstractDualLogEnabled
however)
---
RCS file:
/home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/LogkitLoggerManager.java,v
retrieving revision 1.3
diff -u -r1.3 LogkitLoggerManager.java
--- LogkitLoggerManager.java 29 Jul 2002 09:53:40 -0000 1.3
+++ LogkitLoggerManager.java 12 Sep 2002 15:37:22 -0000
@@ -31,32 +31,20 @@
m_logKitManager = logKitManager;
}

- public org.apache.log.Logger
- getLogKitLoggerForCategory( final String categoryName )
+ public org.apache.log.Logger getLogKitLoggerForCategory( final String
categoryName )
{
- if( null != m_logKitManager )
- {
- return m_logKitManager.getLogger( categoryName );
- }
- else
- {
- final Logger logger =
- m_loggerManager.getLoggerForCategory( categoryName );
- return LogKit2LoggerTarget.createLogger( logger );
- }
+ return getLogKitManager().getLogger( categoryName );
}

public Logger getLoggerForCategory( String categoryName )
{
- if( null != m_logKitManager )
+ if( null != m_loggerManager )
{
- final org.apache.log.Logger logger =
- m_logKitManager.getLogger( categoryName );
- return new LogKitLogger( logger );
+ return m_loggerManager.getLoggerForCategory( categoryName );
}
else
{
- return m_loggerManager.getLoggerForCategory( categoryName );
+ return new LogKitLogger( getLogKitLoggerForCategory( categoryName ) );
}
---

These changes seem to all be working, so I'll go ahead and commit them.
Let me know
if you see any problems.

Cheers,
Leif

Leif Mortenson wrote:

>This led me to make the following extremely simple fix:
>---
>RCS file:
>/home/cvs/jakarta-avalon-excalibur/component/src/java/org/apache/avalon/excalibur/component/Logger2LogKitManager.java,v
>retrieving revision 1.3
>diff -u -r1.3 Logger2LogKitManager.java
>--- Logger2LogKitManager.java 29 Jul 2002 09:53:40 -0000 1.3
>+++ Logger2LogKitManager.java 12 Sep 2002 06:31:29 -0000
>@@ -36,7 +36,7 @@
>public org.apache.log.Logger getLogger( final String categoryName )
>{
>final Logger logger =
>- m_loggerManager.getLoggerForCategory( categoryName );
>+ m_loggerManager.getLoggerForCategory( "" );
>final org.apache.log.Logger logkitLogger =
>getHierarchy().getLoggerFor( categoryName );
>final LogKit2LoggerTarget target =
>---
>
>This seems to work great for me, but I wanted to check with the author
>(Peter?)
>to make sure that this will also work with other Logger implementations.
>I am
>using the LogKitLoggerManager.
>It this looks Ok, then I will go ahead and commit it.
>
>Figures ... ~4 hours for a 1 line fix :-/
>
>Cheers,
>Leif
>
>
>--
>To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
>For additional commands, e-mail: <ma...@jakarta.apache.org>
>
>
>  
>



--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>