Yii Framework Forum: Logging in Long running Console app - Yii Framework Forum

Jump to content

  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

Logging in Long running Console app Rate Topic: -----

#1 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 12 July 2010 - 08:05 PM

Hi, all

I've searched and I can't seem to find the solution in this forum.
I have a console command running forever for some reason.
And the problem is I want to log something every loop but the log doesn't show immediately

class EmailCommand extends CConsoleCommand {
    
    public function run($args) {
	for(;;) {
		Yii::log('Something');
	}
    }
}


Is there any solution?
0

#2 User is offline   Y!! 

  • Advanced Member
  • Yii
  • Group: Yii Dev Team
  • Posts: 978
  • Joined: 18-June 09

Posted 12 July 2010 - 08:45 PM

You can access the logger object this way:

$log = Yii::getLogger();


Check out the logger API, there you'll find properties/methods on how to flush the log manually or after x messages.
0

#3 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 12 July 2010 - 08:53 PM

View PostY!!, on 12 July 2010 - 08:45 PM, said:

You can access the logger object this way:

$log = Yii::getLogger();


Check out the logger API, there you'll find properties/methods on how to flush the log manually or after x messages.


I have tried both

    public function run($args) {
        Yii::getLogger()->autoFlush = 1;
        for (;;){
            Yii::log('Something');
        }
    }


and

    public function run($args) {
        for (;;){
            Yii::log('Something');
            Yii::getLogger()->flush();
        }
    }


Still nothing happened.
0

#4 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 July 2010 - 01:45 AM

Alter the autoFlash property or call flush() manually after each log entry. This will trigger the onFlush event of your logger and send the collected log entries to your configured log routes.

EDIT: Sorry, didn't read that Y!! suggested the same. Sure you've configured some log routes in your console config?

This post has been edited by Mike: 13 July 2010 - 01:46 AM

0

#5 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 13 July 2010 - 02:17 AM

View PostMike, on 13 July 2010 - 01:45 AM, said:

Alter the autoFlash property or call flush() manually after each log entry. This will trigger the onFlush event of your logger and send the collected log entries to your configured log routes.

EDIT: Sorry, didn't read that Y!! suggested the same. Sure you've configured some log routes in your console config?

Of course I'm sure

    public function run($args) {
        Yii::log('Something');
    }


works perfectly,

have you tried yourself?
0

#6 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 July 2010 - 03:28 AM

Aah, the problem seem to be in CLogRouter::collectLogs(). It calls collectLogs($logger,false) for all log routes. The false parameter tells the route not to process the log entries immediately. So the routes will collect the logs first and only log onEndRequest again. I think, there's no simple way to circumvent this.

But you could try to write your custom LogRouter (extends CLogRouter) and override the collectLogs() method there. You'll maybe have to flush the log route's $log property, too, each time you process the logs.
1

#7 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 13 July 2010 - 04:28 AM

View PostMike, on 13 July 2010 - 03:28 AM, said:

Aah, the problem seem to be in CLogRouter::collectLogs(). It calls collectLogs($logger,false) for all log routes. The false parameter tells the route not to process the log entries immediately. So the routes will collect the logs first and only log onEndRequest again. I think, there's no simple way to circumvent this.

But you could try to write your custom LogRouter (extends CLogRouter) and override the collectLogs() method there. You'll maybe have to flush the log route's $log property, too, each time you process the logs.


I just tried alter the CLogRouter::collectLogs(). You mean like this, right?

	public function collectLogs($event)
	{
		$logger=Yii::getLogger();
		foreach($this->_routes as $route)
		{
			if($route->enabled) {
				$route->collectLogs($logger,true);
                                $route->logs = array();
                        }
		}
	}


It's working quite fine, though.
0

#8 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 13 July 2010 - 04:32 AM

Yes, looks good to me.
0

#9 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 13 July 2010 - 04:46 AM

Okay, the problem is solved until some issues are found :lol: :lol:
0

#10 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 13 July 2010 - 08:27 PM

Just found a problem, the logger didn't work well if I use more than one logroute (CFileLogRoute and CDbLogRoute). And it didn't work at all when I use only CDbLogRoute.
0

#11 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 14 July 2010 - 02:13 AM

Just to make sure: And CDbLogRoute works in the console app if you don't use the custom code and without the loop?
0

#12 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 14 July 2010 - 02:57 AM

View PostMike, on 14 July 2010 - 02:13 AM, said:

Just to make sure: And CDbLogRoute works in the console app if you don't use the custom code and without the loop?


The original CLogRouter works fine only with
        public function run($args) {
            Yii::log('Something');            
        }

either with CFileLogRoute or CDbLogRoute or both,

but doesn't work within the infinite loop.

If I use the CFileLogRoute with the custom CLogRouter, the logger works quite fine. But the CDbLogRoute alone doesn't work not to mention when they're both used.
0

#13 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 21 July 2010 - 05:58 AM

Anyone has solution?
0

#14 User is offline   I.N.Sobolev 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 32
  • Joined: 03-December 10
  • Location:Moscow

Posted 08 December 2010 - 04:13 AM

You can try and add new method and event to CLogger like

	/**
	 * Custom method
	 */
	public function flushSave()
	{
		$this->onFlushAndWrite(new CEvent($this));
		$this->_logs=array();
		$this->_logCount=0;
	}

	/**
	 * Custom method
	 */
	public function onFlushAndWrite($event)
	{
		$this->raiseEvent('onFlushAndWrite', $event);
	}


Then you can add eventhandler to this new event( in CLogRouter )
like this

Yii::getLogger()->attachEventHandler('onFlushAndWrite',array($this,'processLogs'));


This worked out for me.
0

#15 User is offline   Vitaliy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-January 11
  • Location:Odessa, Ukraine

Posted 13 January 2011 - 04:59 PM

Also, you can try it

class TestCommand extends CConsoleCommand
{
	public function run($args)
	{
		Yii::getLogger()->autoFlush = 1;
		Yii::getLogger()->attachEventHandler('onFlush',array(Yii::app()->log,'processLogs'));
		parent::run($args);
	}

}

0

#16 User is offline   Vitaliy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-January 11
  • Location:Odessa, Ukraine

Posted 17 January 2011 - 04:10 PM

Unfortunately, my previous solution does not work well.
See updated version:

class TestCommand extends CConsoleCommand
{

	public function run($args)
	{
		Yii::getLogger()->autoFlush = 10;
		Yii::getLogger()->detachEventHandler('onFlush',array(Yii::app()->log,'collectLogs'));
		Yii::getLogger()->attachEventHandler('onFlush',array($this,'processLogs'));
		parent::run($args);
	}

	public function processLogs($event)
	{
		static $routes;
		$logger = Yii::getLogger();
		$routes = isset($routes) ? $routes : Yii::app()->log->getRoutes();
		foreach($routes as $route)
		{
			if($route->enabled)
			{
				$route->collectLogs($logger,true);
				$route->logs = array();
			}
		}
	}
}

0

#17 User is offline   petra 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 218
  • Joined: 20-June 10

Posted 17 January 2011 - 05:44 PM

does it work on multiple log route?
0

#18 User is offline   Vitaliy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-January 11
  • Location:Odessa, Ukraine

Posted 18 January 2011 - 03:39 PM

View Postpetra, on 17 January 2011 - 05:44 PM, said:

does it work on multiple log route?


Yes, the solution works perfectly.
I checked with CFileLogRoute and CDbLogRoute
Note! If using CDbLogRoute `YII_DEBUG` must be defined to FALSE and `YII_TRACE_LEVEL` set to 0
0

#19 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 3,345
  • Joined: 17-January 09
  • Location:Russia

Posted 18 January 2011 - 04:01 PM

Vitaliy
Would be great if you'll share your code via wiki or extension.
Yii 1.1 Application Development Cookbook

Enjoying Yii? Star us at github: 1.1 and 2.0.
0

#20 User is offline   Vitaliy 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 4
  • Joined: 13-January 11
  • Location:Odessa, Ukraine

Posted 18 January 2011 - 04:46 PM

samdark
No problem, I'll write a short article on wiki.
0

Share this topic:


  • (2 Pages)
  • +
  • 1
  • 2
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users