To make your logger available to every action in your controller, I
suggest using an action helper, after registered the resource, like
the following one:

class My_Helper_Logger extends Zend_Controller_Action_Helper_Abstract
{

  function init()
  {
    $this->getActionController()->logger =
$this->getActionController()->getInvokeArg('bootstrap')->getResource('logger');
  }
}

to apply the helper all you have to do is to put in the init()
function of Michael's example this line:

Zend_Controller_Action_HelperBroker::addHelper(new My_Helper_Logger());

and in every action of your controllers now you can access the logger
as $this->logger

Hope it helps,
Jules Piccotti


2009/10/16 Jurian Sluiman <[email protected]>:
> Why don't you place the log in the registry? You're able to get it
> everywhere you want.
>
> Inside bootstrap resource:
> Zend_Registry::set('log', $log);
>
> Where you want to log something:
> Zend_Registry::get('log')->warn('Your log message here');
>
> Regards, Jurian
>
> --
> Jurian Sluiman
> Soflomo.com
>
> Op Monday 12 October 2009 08:51:21 schreef Michael Depetrillo:
>> <?php
>> class Zend_Application_Bootstrap_Resource_Logger extends
>> Zend_Application_Resource_ResourceAbstract
>> {
>> /**
>> * @var Zend_Layout
>> */
>> protected $_logger;
>>
>> /**
>> * Defined by Zend_Application_Resource_Resource
>> *
>> * @return Zend_Logger
>> */
>> public function init()
>> {
>> $this->getBootstrap()->bootstrap('FrontController');
>> return $this->getLogger();
>> }
>>
>> /**
>> * Retrieve logger object using resources.logger.applog configuration
>> value.
>> * Will also set php error_log if resources.logger.errorlog
>> configuration value provided.
>> * @return Zend_Logger
>> */
>> public function getLogger($name = 'applog')
>> {
>> if (null === $this->_logger) {
>> $options = $this->getOptions();
>>
>> if (!isset($options[$name])) {
>> throw new Exception("Log path undefined in
>> application.ini");
>> }
>>
>> if (isset($options['errorlog'])) {
>> ini_set('error_log', $options['errorlog']); // send php
>> errors here
>> ini_set('log_errors', 1);
>> }
>>
>> $writer = new Zend_Log_Writer_Stream($options[$name]);
>> $this->_logger = new Zend_Log($writer);
>> }
>> return $this->_logger;
>> }
>>
>> }
>>
>> Michael DePetrillo
>> [email protected]
>> Mobile: (858) 761-1605
>> www.michaeldepetrillo.com
>>
>> On Sun, Oct 11, 2009 at 12:53 AM, Peter Smit <[email protected]> wrote:
>> > I would like to make a global Zend_Log object that I can reach from my
>> > Controllers and my Models.
>> >
>> > What should I add to my Bootstrap? (My bootstrap extends
>> > Zend_Application_Bootstrap)
>> >
>> > How then can I reach the logger object from my controller actions and
>> > from my model?
>> > Regards,
>> >
>> > Peter Smit

Reply via email to