You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Tal Dayan <ta...@zapta.com> on 2001/07/26 20:47:24 UTC

How to register my own logger ?

The developer documentation shows an example where a logger is registered,
just
before initializing Velocity, using the method

Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, LogSystem );

I had hard time to perform such registration.
The only way that worked for me was to set a property named
"runtime.log.logsystem.class"
with the class name of my logger. The problem is that this way I cannot pass
parameters to
the logger's constructor.

Am I missing something ?

Thanks,

Tal


RE: How to register my own logger ?

Posted by Tal Dayan <ta...@zapta.com>.
Ok, now I get it ;-)

Thanks,

Tal


> -----Original Message-----
> From: gmj@mta2.srv.hcvlny.cv.net [mailto:gmj@mta2.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Monday, July 30, 2001 4:35 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: How to register my own logger ?
> 
> 
> Tal Dayan wrote:
> > 
> > I tried again with:
> > 
> >     final Properties velocityProps = new Properties();
> > 
> >     ....
> > 
> >     final Logger logger = new Logger();
> >     velocityProps.put("runtime.log.logsystem", logger);   // my logger
> > 
> >     Velocity.init(velocityProps);
> > 
> > And got the exception:
> > 
> > java.lang.NullPointerException
> >         at java.util.Hashtable.put(Hashtable.java:380)
> >         at
> > 
> org.apache.commons.collections.ExtendedProperties.addPropertyDirec
> t(Extended
> > Properties.java:683)
> >         at
> > 
> org.apache.commons.collections.ExtendedProperties.addProperty(Exte
> ndedProper
> > ties.java:657)
> >         at
> > 
> org.apache.commons.collections.ExtendedProperties.setProperty(Exte
> ndedProper
> > ties.java:746)
> >         at
> > 
> org.apache.commons.collections.ExtendedProperties.convertPropertie
> s(Extended
> > Properties.java:1872)
> >         at org.apache.velocity.runtime.Runtime.init(Runtime.java:410)
> >         at org.apache.velocity.app.Velocity.init(Velocity.java:155)
> >         at cg.facility.config.Config.initVelocity(Config.java:199)
> >         at cg.facility.config.Config.configure(Config.java:219)
> >         at cg.facility.config.Config.main(Config.java:330)
> > 
> > Can the properties passed to Velocity.init() contains values other than
> > String (a logger
> > object in this case) ?
> 
> This is the point I was trying to make in the 'vm library thread', that
> it may not be currently possible to do both setProperty() followed by an
> init(Properties).  (Which I will agree should probably work...)
> 
> For now, try to iterate over your properties and use setProperty(), and
> then do a setProperty() with the logger :
> 
> /*
>  *  from the Properties
>  */
> 
> for( Enumeration e = p.propertyNames(); e.hasMoreElements(); )
> {
>   String el = (String) e.nextElement();
>   Velocity.setProperty( el, p.getProperty( el ) );
> }
> 
> /*
>  *  add some individual properties if you wish
>  */
> 
> Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, logger );
>  
> /*
>  *  and now call init
>  */
> 
> Velocity.init();
> 
> And that indeed will work (I tested it :)
> 
> geir
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.

Re: How to register my own logger ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> I tried again with:
> 
>     final Properties velocityProps = new Properties();
> 
>     ....
> 
>     final Logger logger = new Logger();
>     velocityProps.put("runtime.log.logsystem", logger);   // my logger
> 
>     Velocity.init(velocityProps);
> 
> And got the exception:
> 
> java.lang.NullPointerException
>         at java.util.Hashtable.put(Hashtable.java:380)
>         at
> org.apache.commons.collections.ExtendedProperties.addPropertyDirect(Extended
> Properties.java:683)
>         at
> org.apache.commons.collections.ExtendedProperties.addProperty(ExtendedProper
> ties.java:657)
>         at
> org.apache.commons.collections.ExtendedProperties.setProperty(ExtendedProper
> ties.java:746)
>         at
> org.apache.commons.collections.ExtendedProperties.convertProperties(Extended
> Properties.java:1872)
>         at org.apache.velocity.runtime.Runtime.init(Runtime.java:410)
>         at org.apache.velocity.app.Velocity.init(Velocity.java:155)
>         at cg.facility.config.Config.initVelocity(Config.java:199)
>         at cg.facility.config.Config.configure(Config.java:219)
>         at cg.facility.config.Config.main(Config.java:330)
> 
> Can the properties passed to Velocity.init() contains values other than
> String (a logger
> object in this case) ?

This is the point I was trying to make in the 'vm library thread', that
it may not be currently possible to do both setProperty() followed by an
init(Properties).  (Which I will agree should probably work...)

For now, try to iterate over your properties and use setProperty(), and
then do a setProperty() with the logger :

/*
 *  from the Properties
 */

for( Enumeration e = p.propertyNames(); e.hasMoreElements(); )
{
  String el = (String) e.nextElement();
  Velocity.setProperty( el, p.getProperty( el ) );
}

/*
 *  add some individual properties if you wish
 */

Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, logger );
 
/*
 *  and now call init
 */

Velocity.init();

And that indeed will work (I tested it :)

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: How to register my own logger ?

Posted by Tal Dayan <ta...@zapta.com>.
I tried again with:

    final Properties velocityProps = new Properties();

    ....

    final Logger logger = new Logger();
    velocityProps.put("runtime.log.logsystem", logger);   // my logger

    Velocity.init(velocityProps);

And got the exception:

java.lang.NullPointerException
        at java.util.Hashtable.put(Hashtable.java:380)
        at
org.apache.commons.collections.ExtendedProperties.addPropertyDirect(Extended
Properties.java:683)
        at
org.apache.commons.collections.ExtendedProperties.addProperty(ExtendedProper
ties.java:657)
        at
org.apache.commons.collections.ExtendedProperties.setProperty(ExtendedProper
ties.java:746)
        at
org.apache.commons.collections.ExtendedProperties.convertProperties(Extended
Properties.java:1872)
        at org.apache.velocity.runtime.Runtime.init(Runtime.java:410)
        at org.apache.velocity.app.Velocity.init(Velocity.java:155)
        at cg.facility.config.Config.initVelocity(Config.java:199)
        at cg.facility.config.Config.configure(Config.java:219)
        at cg.facility.config.Config.main(Config.java:330)

Can the properties passed to Velocity.init() contains values other than
String (a logger
object in this case) ?

Tal



> -----Original Message-----
> From: gmj@mta6.srv.hcvlny.cv.net [mailto:gmj@mta6.srv.hcvlny.cv.net]On
> Behalf Of Geir Magnusson Jr.
> Sent: Friday, July 27, 2001 3:53 AM
> To: velocity-user@jakarta.apache.org
> Subject: Re: How to register my own logger ?
>
>
> Tal Dayan wrote:
> >
> > Does it have to be 'this' or can it be any object that implements
> > LogSystem ?
> >
> > I tried the following and it did not work:
> >
> >   Properties myProps = new Properties();
> >
> >   ... setting various properties in myProps ...
> >
> >   LogSystem myLogger = new MyLogger();  // implements LogSystem
> >
> >   Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, myLogger );
> >
> >   Velocity.init(myProps);
> >
> > But if it should work, I will give it another try, maybe I did somthing
> > wrong after all.
>
> It can be anything, not just 'this'.
>
> It wouldn't surprise me if the above didn't work, although I am not sure
> exactly why.
>
> Either add the logger to the properties, or simply loop over the
> properties, and setProperty each value.
>
> geir
>
> --
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.
>


Re: How to register my own logger ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> Does it have to be 'this' or can it be any object that implements
> LogSystem ?
> 
> I tried the following and it did not work:
> 
>   Properties myProps = new Properties();
> 
>   ... setting various properties in myProps ...
> 
>   LogSystem myLogger = new MyLogger();  // implements LogSystem
> 
>   Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, myLogger );
> 
>   Velocity.init(myProps);
> 
> But if it should work, I will give it another try, maybe I did somthing
> wrong after all.

It can be anything, not just 'this'.

It wouldn't surprise me if the above didn't work, although I am not sure
exactly why.

Either add the logger to the properties, or simply loop over the
properties, and setProperty each value.

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.

RE: How to register my own logger ?

Posted by Tal Dayan <ta...@zapta.com>.
Does it have to be 'this' or can it be any object that implements 
LogSystem ?

I tried the following and it did not work:

  Properties myProps = new Properties();

  ... setting various properties in myProps ...

  LogSystem myLogger = new MyLogger();  // implements LogSystem

  Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, myLogger );

  Velocity.init(myProps);

But if it should work, I will give it another try, maybe I did somthing
wrong after all.

Tal

> 
> Yes.  You can also just hand velocity a reference to a living object :
> 
> Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this );
> 
> if this implements LogSystem
> 
> See the ExternalLogSystem.java test case in org.apache.velocity.test to
> see it in action.
> 
> There also is an example :   examples/logger_example
> 
> geir
> 
> -- 
> Geir Magnusson Jr.                           geirm@optonline.net
> System and Software Consulting
> Developing for the web?  See http://jakarta.apache.org/velocity/
> Be well, do good work, and keep in touch.
> 

Re: How to register my own logger ?

Posted by "Geir Magnusson Jr." <ge...@optonline.net>.
Tal Dayan wrote:
> 
> The developer documentation shows an example where a logger is registered,
> just
> before initializing Velocity, using the method
> 
> Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, LogSystem );
> 
> I had hard time to perform such registration.
> The only way that worked for me was to set a property named
> "runtime.log.logsystem.class"
> with the class name of my logger. The problem is that this way I cannot pass
> parameters to
> the logger's constructor.
> 
> Am I missing something ?

Yes.  You can also just hand velocity a reference to a living object :

Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, this );

if this implements LogSystem

See the ExternalLogSystem.java test case in org.apache.velocity.test to
see it in action.

There also is an example :   examples/logger_example

geir

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
Be well, do good work, and keep in touch.