Yii Framework Forum: How to log in a ConsoleApplication - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

How to log in a ConsoleApplication Rate Topic: -----

#1 User is offline   ChrisK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-January 10

Posted 27 January 2010 - 01:07 PM

Hi all,

I am currently implementing a import console application for which it would be nice to log error messages, warnings, etc. to a log file. I tried to use Yii::log(), but it won't write the log entries to the application.log.
I have already been digging through the Yii sources and as it seems to me console applications do not support logging at all. Am I right, or did I just forget any special config for that purpose?

Thanks,
Chris
0

#2 User is offline   Y!! 

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

Posted 27 January 2010 - 01:16 PM

Welcome to forum.

Logging for console applications works just like the logging for web applications. Could you show us your config file and the usage of the Yii::log() command?
0

#3 User is offline   ChrisK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-January 10

Posted 27 January 2010 - 01:30 PM

Hi,

The log configuration looks like this (I've split the config to 3 files, so I will only post the relevant parts):
'components' => array(
    'log' => array(
        'class' => 'CLogRouter',
        'routes' => array(
	array(
          'class' => 'CFileLogRoute',
          'levels' => 'error, warning',
	),
        array(
          'class' => 'CWebLogRoute',
          'showInFireBug' => true
        ),
        array(
          'class' => 'XWebDebugRouter',
          'config'=>'alignLeft, opaque, runInDebug, fixedPos, collapsed',
          'levels'=>'error, warning, trace, profile, info',
        ),
      )
    )
  )


I'm using the log command like this:
Yii::log('Product save error.', 'error', 'application.commands.shell.WProductImport.WProductImportCommand');


Thanks,
Chris
0

#4 User is offline   talby101 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 36
  • Joined: 26-August 10
  • Location:Wales

Posted 21 July 2011 - 11:23 AM

Hi,
I did it like this :

public function run($args)
	{
		  
		$identity=new UserIdentity('admin','admin');
		 if(! $identity->authenticate())
		 {
		 	echo "Error: Authentication is required to run this command"; 
			return;
		 } 
		 	Yii::app()->user->login($identity);
}

0

#5 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 26 July 2011 - 08:34 PM

Has this issue been resolved? I have the same configuration as Chris; logging works fine for web apps but not console apps. Thanks!
:mellow:
0

#6 User is offline   ChrisK 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 3
  • Joined: 27-January 10

Posted 27 July 2011 - 07:10 AM

I think the problem was ending the console app with "die()" instead of Yii::app()->end as logs are written when the app is shut down.
But for saving a lot memory I now would recommend completely deactivating the logging within console apps or using the new 1.1.8 release which enables logging at runtime.
0

#7 User is offline   Emily Dickinson 

  • Standard Member
  • PipPip
  • Yii
  • Group: Members
  • Posts: 201
  • Joined: 17-September 10
  • Location:Albuquerque, NM

Posted 27 July 2011 - 02:04 PM

Updating to the stable 1.1.8 version, and eliminating calls to PHP exit() was indeed the fix. Thanks!
:mellow:
0

#8 User is offline   matteosistisette 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-June 12

Posted 15 November 2012 - 02:32 PM

I have a similar problem.

I have exactly the same configuration for logroutes in config/console.php as in config/main.php (except a different filename for logFile)

However, logging from web application works, while logging from console commands doesn't work. Nothing is logged at all. And i don't have any die() or exit().

Also, I'm interested in how to enable this "runtime logging". I guess that means logging immediately rather than holding log messages in memory until the end, right? How do I enable that? (obviously I wouldn't do it for the web app but for the console app).
0

#9 User is offline   1cichy 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 24
  • Joined: 12-June 12
  • Location:Poland

Posted 15 November 2012 - 06:00 PM

Hi,

Make sure
'preload'=>array('log')
is in your console config file (console.php)
0

#10 User is offline   matteosistisette 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 08-June 12

Posted 15 November 2012 - 06:23 PM

That was it!! thanks!
0

Share this topic:


Page 1 of 1
  • 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