[email protected]
[Top] [All Lists]

Re: Add appender programmatically

Subject: Re: Add appender programmatically
From: Christian Grobmeier
Date: Thu, 15 Apr 2010 13:38:30 +0200
aaaaah yes, I didn't see this. Maybe its worth thinking to
autmatically activateoptions once this appender is called

Thanks for giving feedback



On Thu, Apr 15, 2010 at 1:32 PM, Miquel Canes <[email protected]> wrote:
> Hi,
> Finally, after using the php debuger, i found what's my problem.
>
> $logger = Logger::getRootLogger();
>
> $appender = new LoggerAppenderFile("programmatically");
> $layout = new LoggerLayoutPattern("[%p] %t %c - %m%n");
>
> $appender->setLayout($layout);
> $appender->setFileName("programmatically.log");
> $appender->setAppend(true);
> $appender->activateOptions();
> $logger->addAppender($appender);
>
> $allappenders = $logger->getAllAppenders();
> foreach ($allappenders as $nappender){
> Â Â Â Âecho $nappender->getName()."\n";
> }
>
> $logger->info("info text");
>
> This function: $appender->activateOptions() creates the file descriptor.
> Must be called before sending any log message.
>
> Thanks,
> Miquel
>
> On Wednesday 14 April 2010 17:52:58 Miquel Canes wrote:
>> Hi,
>> If i call the configure() function after add the appender, none of them
>> works. Well, all the loggers and appenders inside the property file are
>> ignored.
>>
>> If i try put the logger line of the property file on the bottom, happens
>> the same that if that line are on the top of the property file.
>>
>> And finally if I don't call the configure function, only stdout log works
>> (default appender) but the file appender added inside the program is still
>> not working.
>>
>> Maybe we need to put some more sets to the $appender or something like that
>> to add an appender inside the program.
>>
>> Thanks,
>> Miquel
>>
>> On Wednesday 14 April 2010 17:08:03 Christian Grobmeier wrote:
>> > Hm
>> >
>> > I think I know the problem. Its because you first need to configure
>> > the appenders and then the loggers. Otherwise the references are not
>> > set. This is also true for property files in general in log4php. First
>> > appenders, then loggers.
>> >
>> > In your case - I am not sure if there is a workaround for this because
>> > you configuration is mixed. You probably can try it out with putting
>> > the rootLogger defintion on the bottom of your property file or
>> > assigning the appender before calling configure(). i am not sure if it
>> > helps.
>> >
>> > There is already an issue in Jira which addresses this
>> >
>> > Christian
>> >
>> > 2010/4/14 Miquel Canes <[email protected]>:
>> > > Hello,
>> > > It doesn't work.
>> > >
>> > > Logger::configure(dirname(__FILE__) . '/../log4php.properties');
>> > >
>> > > $logger = Logger::getRootLogger();
>> > >
>> > > $appender = new LoggerAppenderFile("programmatically");
>> > > $layout = new LoggerLayoutPattern("[%p] %t %c - %m%n");
>> > >
>> > > $appender->setLayout($layout);
>> > > $appender->setFileName("programmatically.log");
>> > > $appender->setAppend(true);
>> > > $appender->setThreshold("ALL");
>> > > $logger->addAppender($appender);
>> > >
>> > > echo $logger->isInfoEnabled() . "\n";
>> > >
>> > > $allappenders = $logger->getAllAppenders();
>> > > foreach ($allappenders as $nappender){
>> > >
>> > > Â Â Â Âecho $nappender->getName()."\n";
>> > >
>> > > }
>> > >
>> > > $logger->info("info text");
>> > >
>> > > With this code it write:
>> > > 1
>> > > stdout
>> > > R
>> > > programmatically
>> > >
>> > >
>> > > I'm using a config file too and the file appender defined on it works.
>> > >
>> > > log4php.properties content:
>> > >
>> > > #R - Default Log file appender.
>> > > log4php.rootLogger=all, stdout, R
>> > >
>> > > log4php.appender.stdout=LoggerAppenderConsole
>> > > log4php.appender.stdout.layout=LoggerLayoutPattern
>> > >
>> > > # Pattern to output the caller's file name and line number.
>> > > #log4php.appender.stdout.layout.ConversionPattern=[%p] %t %c - %m%n
>> > >
>> > > log4php.appender.R=LoggerAppenderRollingFile
>> > > log4php.appender.R.File=testrollingappender.log
>> > >
>> > > log4php.appender.R.MaxFileSize=100KB
>> > > # Keep one backup file
>> > > log4php.appender.R.MaxBackupIndex=1
>> > >
>> > > log4php.appender.R.layout=LoggerLayoutPattern
>> > > log4php.appender.R.layout.ConversionPattern=[%p] %t %c - %m%n
>> > >
>> > >
>> > > Maybe, a programmatically appender cannot be added if a configure file
>> > > is used? But it does not make any sense.
>> > >
>> > > Thanks,
>> > > Miquel
>> > >
>> > > On Wednesday 14 April 2010 16:23:59 Christian Grobmeier wrote:
>> > >> Hello,
>> > >>
>> > >> whats returned if you do:
>> > >>
>> > >> echo $logger->isInfoEnabled();
>> > >>
>> > >> My guess is that info level is not enabled for the root logger
>> > >>
>> > >> Also you might need to set a threshhold for your appender:
>> > >> $appender->setThreshold($threshhold);
>> > >>
>> > >> which is in fact a LoggerLevel. If you use ALL, you outputing all
>> > >>
>> > >> Pls let me know if that worked for you
>> > >>
>> > >> Christian
>> > >>
>> > >> 2010/4/13 Miquel Canes <[email protected]>:
>> > >> > Hello,
>> > >> >
>> > >> > I'm trying to add a file appender programmatically.
>> > >> >
>> > >> > I'm using this piece of code
>> > >> >
>> > >> > $logger = Logger::getRootLogger();
>> > >> >
>> > >> > $appender = new LoggerAppenderFile("programmatically");
>> > >> > $layout = new LoggerLayoutPattern("[%p] %t %c - %m%n");
>> > >> >
>> > >> > $appender->setLayout($layout);
>> > >> > $appender->setFileName("programmatically.log");
>> > >> > $appender->setAppend(true);
>> > >> > $logger->addAppender($appender);
>> > >> >
>> > >> > $allappenders = $logger->getAllAppenders();
>> > >> > foreach ($allappenders as $nappender){
>> > >> >
>> > >> > Â Â Â Âecho $nappender->getName()."\n";
>> > >> >
>> > >> > }
>> > >> >
>> > >> > $logger->info("info text");
>> > >> >
>> > >> >
>> > >> >
>> > >> > It show the new appender when he print all the appenders but it
>> > >> > doesn't create the new log file.
>> > >> >
>> > >> > How can I add a new file appender to the logger?
>> > >> >
>> > >> > Thanks,
>> > >> > Miquel
>> > >> >
>> > >> > *Advertencia legal: en virtud de lo establecido en la Ley OrgÃnica
>> > >> > 15/1999 de ProtecciÃn de Datos de CarÃcter Personal, le informamos
>> > >> > de que los datos personales que pueda facilitarnos se incorporaran
>> > >> > a un fichero automatizado titularidad de CAST INFO, S.A. con la
>> > >> > finalidad de gestionar la relaciÃn negocial que nos vincula. PodrÃ
>> > >> > revocar su consentimiento al tratamiento de los datos, asà como
>> > >> > ejercer sus derechos de acceso, rectificaciÃn, cancelaciÃn u
>> > >> > oposiciÃn
>> > >> > dirigiÃndose por escrito a CAST INFO domiciliada en C/ Tuset 23, 1Â
>> > >> > -- 08006 Barcelona, o a la direcciÃn de correo electrÃnico
>> > >> > [email protected]
>> > >> >
>> > >> > Este mensaje y los ficheros anexos que pueda contener son
>> > >> > confidenciales, pueden contener informaciÃn sometida a secreto
>> > >> > profesional y se dirige exclusivamente a su destinatario. Si ha
>> > >> > recibido este mensaje por error o tiene conocimiento del mismo por
>> > >> > cualquier motivo, le rogamos que nos lo comunique inmediatamente por
>> > >> > este mismo medio y se abstenga de utilizarlo, reproducirlo,
>> > >> > alterarlo, archivarlo o comunicarlo a terceros. El emisor no se
>> > >> > responsabiliza de posibles perjuicios derivados de la captura,
>> > >> > incorporaciones de virus o cualesquiera otras manipulaciones
>> > >> > efectuadas por terceros.
>> > >> >
>> > >> > Antes de imprimir este e-mail piense bien si es necesario hacerlo.
>> > >>
>> > >> *Advertencia legal: en virtud de lo establecido en la Ley Orgïnica
>> > >> 15/1999 de Protecciïn de Datos de Carïcter Personal, le informamos de
>> > >> que los datos personales que pueda facilitarnos se incorporaran a un
>> > >> fichero automatizado titularidad de CAST INFO, S.A. con la finalidad
>> > >> de gestionar la relaciïn negocial que nos vincula. Podrï revocar su
>> > >> consentimiento al tratamiento de los datos, asï como ejercer sus
>> > >> derechos de acceso, rectificaciïn, cancelaciïn u oposiciïn
>> > >> dirigiïndose por escrito a CAST INFO domiciliada en C/ Tuset 23, 1ï
>> > >> -- 08006 Barcelona, o a la direcciïn de correo electrïnico
>> > >> [email protected]
>> > >>
>> > >> Este mensaje y los ficheros anexos que pueda contener son
>> > >> confidenciales, pueden contener informaciïn sometida a secreto
>> > >> profesional y se dirige exclusivamente a su destinatario. Si ha
>> > >> recibido este mensaje por error o tiene conocimiento del mismo por
>> > >> cualquier motivo, le rogamos que nos lo comunique inmediatamente por
>> > >> este mismo medio y se abstenga de utilizarlo, reproducirlo, alterarlo,
>> > >> archivarlo o comunicarlo a terceros. El emisor no se responsabiliza de
>> > >> posibles perjuicios derivados de la captura, incorporaciones de virus
>> > >> o cualesquiera otras manipulaciones efectuadas por terceros.
>> > >>
>> > >> Antes de imprimir este e-mail piense bien si es necesario hacerlo.
>

<Prev in Thread] Current Thread [Next in Thread>