I found some more messages on this after digging and going through
source.  I can see why the symfony develolpers wanted to simplify
tasks so they don't require contexts.  Unfortunately, it also means
that a lot of code may not be reusable in tasks.  I'm not sure if
there is a happy balance that can be struck.

I saw some hacks - people patching the sfTask itself.  I'd rather not
do that.  Here is my own "hack" attempting to be as reverse-compatible
as possible if and when I upgrade the core packages.

add an option in configure - this is a "dummy" environment's
configuration, used to create an application context, rather than
the :

    new sfCommandOption('auxEnv', null,
sfCommandOption::PARAMETER_REQUIRED, 'The environment used for
logging', 'dev')

add the following into your task (or in a superclass):

  protected function setupAuxEnv($arguments = array(), $options = array
())
  {
    // setup a context so we can log
    $this->log('Initialize auxillary context');
    $configuration = ProjectConfiguration::getApplicationConfiguration
    ($options['application'], $options['auxEnv'], true);
    sfContext::createInstance($configuration, 'default');

    // Now override the logger so that we use the same logging as
    // the task
    $logger = new sfTaskLogger($this->dispatcher, array('level' =>
'debug'));
    sfContext::getInstance('default')->set('logger', $logger);
  }

Create a simple task logger to wrap the existing logging ability of
the task:

class sfTaskLogger extends sfLogger
{
  protected $dispatcher = null;

  public function __construct(sfEventDispatcher $dispatcher, $options
= array())
  {
    $this->initialize($dispatcher, $options);
    if (!isset($options['auto_shutdown']) || $options
['auto_shutdown'])
    {
      register_shutdown_function(array($this, 'shutdown'));
    }
  }

  public function initialize(sfEventDispatcher $dispatcher, $options =
array())
  {
    if (isset($options['level']))
    {
      $this->setLogLevel($options['level']);
    }
    $this->dispatcher = $dispatcher;
  }

  protected function doLog($message, $priority)
  {
    $this->dispatcher->notify(new sfEvent($this, 'command.log', array
($message)));
  }
}




On Jun 11, 2:14 pm, Steve Sanyal <steve.san...@gmail.com> wrote:
> Hi,
>
> Do you know how to enable logging in code called by a task?
>
> In the task itself you can use the sfTask::log function, but my task
> invokes other classes, some of which also contain logging.
>
> My log statements are not printing out.  It seems the context doesn't
> get setup by the task to provide logging perhaps?
>
> Steve
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"symfony users" group.
To post to this group, send email to symfony-users@googlegroups.com
To unsubscribe from this group, send email to 
symfony-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/symfony-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to