We had this same issue and we have solved this by implementing a
LogFactory.
I would not inject the dispatcher in my model,
For one, because you want to log, not dispatch
And for two, because it is very cumbersome to provide the logger to
all your classes (changing all constructors and constructor calls)
And finally for three, because a reference to the logger (dispatcher)
makes it impossible to serialize your classes, which makes it
impossible to cache them...
I would recommend a LogFactory that can return the logger by:
LogFactory::getLogger() which returns an sfLogger instance.
This LogFactory needs to be initialized early when running your
webApps or tasks by parsing the factory.yml
I can provide you some examples and the logfactory implementation
tomorrow if you like.
Please note that I would not use this solution in Symfony2. In that
case I would use the DI containers to configure the injection of the
logger into the constructors of my classes.
regards
Leon
Damon Jones schreef:
> Using sfContext has its problems, as you have seen. It couples your
> models and where you can create your own context in your own tasks,
> that doesn't solve the problem with standard tasks.
>
> How about using the event dispatcher?
>
> In your model:
>
> class Something
> {
> protected $dispatcher;
>
> public function __construct(sfDispatcher $dispatcher = null)
> {
> if ($dispatcher) {
> $this->dispatcher = $dispatcher;
> } else {
> $this->dispatcher = ProjectConfiguration::getActive()-
> >getEventDispatcher();
> }
> }
>
> public function log($message, $priority = sfLogger::ERR)
> {
> $this->dispatcher->notify(new sfEvent($this, 'application.log',
> array('message' => $message, 'priority' => $priority)));
> }
> }
>
> You'll need to set the logger for the prod environment to
> sfAggregateLogger instead of sfNoLogger and set your level, etc.
>
> Hope this helps.
>
> On Feb 17, 4:09 am, davidsonrob <[email protected]> wrote:
> > Hi,
> >
> > As the complexity of my models has grown I have felt the need to log
> > errors in the model to help me thoubleshoot
> >
> > The way I have been doing it is something like
> >
> > sfContext::getInstance()->getLogger()>err('{artistImage}
> > getImageThumbPath unexpected thumbnailType input param');
> >
> > Which all works fine, it gives me a nice little error in my
> > debugtoolbar.
> >
> > However when I use the CLI for tasks such as
> > php symfony doctrine:build --all
> >
> > rebuilding models etc I get an sfContext error.
> >
> > ... So what would be the corrrect way for me to log errors in my model?
--
If you want to report a vulnerability issue on symfony, please send it to
security at symfony-project.com
You received this message because you are subscribed to the Google
Groups "symfony users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/symfony-users?hl=en