How to log in a ConsoleApplication

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

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?

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

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);

}

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

:mellow:

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.

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

:mellow:

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).

Hi,

Make sure


'preload'=>array('log')

is in your console config file (console.php)

That was it!! thanks!