开发者

Logging from symfony's model layer

开发者 https://www.devze.com 2022-12-25 20:43 出处:网络
I\'m currently working on a project with symfony 1.4 and Doctrine 1.2. I\'m looking for a proper way to do logging from the model layer.

I'm currently working on a project with symfony 1.4 and Doctrine 1.2. I'm looking for a proper way to do logging from the model layer.

In some model classes I use the record hook postSave() to create a ZIP file using exec() (since PHP zip doesn't provide for storage method 'Stored'). To be sure that everythings works fine I check the return code and log an error if something goes wrong. My first naive approach was to do it like this:

if ($returnCode != 0) {
  sfContext::ge开发者_JS百科tInstance()->getLogger()->debug(...);
}

As you know, this doesn't work so well because sfContext belongs to the controller layer and shouldn't be used from the model layer. My next try was to use the model's constructor to pass in an sfLogger instance, but this doesn't work due to Doctrine 1.2 reserving the constructor for internal use (Doctrine 1.2 Documentation).

I'm looking forward for your suggestions!


You can add a setLogger() method to your function, that gets a logger instance in a parameter, and sets it to an internal variable, like $this->logger. Later, when you need to log something, do it like if ($this->logger) { $this->logger->debug(...); }.

0

精彩评论

暂无评论...
验证码 换一张
取 消