You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@velocity.apache.org by Markku Saarela <ma...@entra.fi> on 2003/12/15 09:08:10 UTC

SimpleLog4JLogSystem appender log4j:ERROR fix

Hi,

There are situations where Velocity prints following error because 
SimpleLog4JLogSystem close those appenders in its finalize method:
log4j:ERROR Attempted to append to closed appender named [null]

So I modified internalInit method and get rid of these finalize and 
shutdown methods and now there are no errors.

Here is code snipped from internalInit method:

        java.util.Properties props = new java.util.Properties();
        String ap = "log4j.appender.R";
       
        /*
         * Priority is set for DEBUG becouse this implementation checks
         * log level.
         */
        props.put("log4j.logger."+this.getClass().getName(),
          Priority.DEBUG.toString()+ " ,R");
        props.put(ap, RollingFileAppender.class.getName());
        props.put(ap+".File", logfile);
        props.put(ap+".MaxFileSize", "100KB");
        props.put(ap+".MaxBackupIndex", "1");
       
        props.put(ap+".layout", PatternLayout.class.getName());
        props.put(ap+".layout.ConversionPattern", "%d - %m%n");
        org.apache.log4j.PropertyConfigurator.configure(props);
       
        logger = Category.getInstance(this.getClass().getName());
//        logger.setAdditivity(false);

        /*
         * Priority is set for DEBUG becouse this implementation checks
         * log level.
         */
//        logger.setPriority(Priority.DEBUG);
//
//        RollingFileAppender appender = new RollingFileAppender( new 
PatternLayout( "%d - %m%n"), logfile, true);
//       
//        appender.setMaxBackupIndex( 1 );
//       
//        appender.setMaximumFileSize( 100000 );
//      
//        logger.addAppender(appender);

-- 
Markku Saarela
Senior Software Engineer (OO Technology, Java, J2EE, Smalltalk)
Entra e-Solutions Oy
mobile: +358-9-590 6505
markku.saarela@entra.fi
www.entra.fi


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


Re: SimpleLog4JLogSystem appender log4j:ERROR fix

Posted by Markku Saarela <ma...@entra.fi>.
I tried that one too(closing only this added appender). No help. I think 
reason is when you run several tasks (in Ant or web/j2ee container) on 
same VM, Log4J is keeping  list of appenders and when you ran 1. task 
and starts second task then LOG4J do not add new appender if it allready 
exists and then when VM GC this old instance of SimleLog4JLogSystem it 
closes this only one and then there is no appender open any more.

Therefore its safer lets Log4J do adding and closing appenders then 
these appenders is open when they are needed.

You can replace "log4j.appender.R" for example 
"log4j.appender.velocityRollingFile" so there is no collision.

Markku

Geir Magnusson Jr wrote:

> Why is this safe to create this way?
>
> I understand the need for not closing all appenders (so if we just 
> closed our appender....), but why is configuring "log4j.appender.R' 
> like this not going to collide w/ some other config?
>
> On Dec 15, 2003, at 3:08 AM, Markku Saarela wrote:
>
>> Hi,
>>
>> There are situations where Velocity prints following error because 
>> SimpleLog4JLogSystem close those appenders in its finalize method:
>> log4j:ERROR Attempted to append to closed appender named [null]
>>
>> So I modified internalInit method and get rid of these finalize and 
>> shutdown methods and now there are no errors.
>>
>> Here is code snipped from internalInit method:
>>
>>        java.util.Properties props = new java.util.Properties();
>>        String ap = "log4j.appender.R";
>>              /*
>>         * Priority is set for DEBUG becouse this implementation checks
>>         * log level.
>>         */
>>        props.put("log4j.logger."+this.getClass().getName(),
>>          Priority.DEBUG.toString()+ " ,R");
>>        props.put(ap, RollingFileAppender.class.getName());
>>        props.put(ap+".File", logfile);
>>        props.put(ap+".MaxFileSize", "100KB");
>>        props.put(ap+".MaxBackupIndex", "1");
>>              props.put(ap+".layout", PatternLayout.class.getName());
>>        props.put(ap+".layout.ConversionPattern", "%d - %m%n");
>>        org.apache.log4j.PropertyConfigurator.configure(props);
>>              logger = Category.getInstance(this.getClass().getName());
>> //        logger.setAdditivity(false);
>>
>>        /*
>>         * Priority is set for DEBUG becouse this implementation checks
>>         * log level.
>>         */
>> //        logger.setPriority(Priority.DEBUG);
>> //
>> //        RollingFileAppender appender = new RollingFileAppender( new 
>> PatternLayout( "%d - %m%n"), logfile, true);
>> //       //        appender.setMaxBackupIndex( 1 );
>> //       //        appender.setMaximumFileSize( 100000 );
>> //      //        logger.addAppender(appender);
>>
>> -- 
>> Markku Saarela
>> Senior Software Engineer (OO Technology, Java, J2EE, Smalltalk)
>> Entra e-Solutions Oy
>> mobile: +358-9-590 6505
>> markku.saarela@entra.fi
>> www.entra.fi
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
>> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>>
>>


-- 
Markku Saarela
Senior Software Engineer (OO Technology, Java, J2EE, Smalltalk)
Entra e-Solutions Oy
mobile: +358-9-590 6505
markku.saarela@entra.fi
www.entra.fi


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


Re: SimpleLog4JLogSystem appender log4j:ERROR fix

Posted by Geir Magnusson Jr <ge...@4quarters.com>.
Why is this safe to create this way?

I understand the need for not closing all appenders (so if we just 
closed our appender....), but why is configuring "log4j.appender.R' 
like this not going to collide w/ some other config?

On Dec 15, 2003, at 3:08 AM, Markku Saarela wrote:

> Hi,
>
> There are situations where Velocity prints following error because 
> SimpleLog4JLogSystem close those appenders in its finalize method:
> log4j:ERROR Attempted to append to closed appender named [null]
>
> So I modified internalInit method and get rid of these finalize and 
> shutdown methods and now there are no errors.
>
> Here is code snipped from internalInit method:
>
>        java.util.Properties props = new java.util.Properties();
>        String ap = "log4j.appender.R";
>              /*
>         * Priority is set for DEBUG becouse this implementation checks
>         * log level.
>         */
>        props.put("log4j.logger."+this.getClass().getName(),
>          Priority.DEBUG.toString()+ " ,R");
>        props.put(ap, RollingFileAppender.class.getName());
>        props.put(ap+".File", logfile);
>        props.put(ap+".MaxFileSize", "100KB");
>        props.put(ap+".MaxBackupIndex", "1");
>              props.put(ap+".layout", PatternLayout.class.getName());
>        props.put(ap+".layout.ConversionPattern", "%d - %m%n");
>        org.apache.log4j.PropertyConfigurator.configure(props);
>              logger = Category.getInstance(this.getClass().getName());
> //        logger.setAdditivity(false);
>
>        /*
>         * Priority is set for DEBUG becouse this implementation checks
>         * log level.
>         */
> //        logger.setPriority(Priority.DEBUG);
> //
> //        RollingFileAppender appender = new RollingFileAppender( new 
> PatternLayout( "%d - %m%n"), logfile, true);
> //       //        appender.setMaxBackupIndex( 1 );
> //       //        appender.setMaximumFileSize( 100000 );
> //      //        logger.addAppender(appender);
>
> -- 
> Markku Saarela
> Senior Software Engineer (OO Technology, Java, J2EE, Smalltalk)
> Entra e-Solutions Oy
> mobile: +358-9-590 6505
> markku.saarela@entra.fi
> www.entra.fi
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: velocity-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: velocity-user-help@jakarta.apache.org
>
>
-- 
Geir Magnusson Jr                                   203-247-1713(m)
geir@4quarters.com


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