Correct way to log from Entities and Repositories in Symfony2

纵然是瞬间 提交于 2019-12-23 09:31:45

问题


What is a way to log messages or errors from an Entity or Repository class in the symfony2 architecture? In symfony1 you could use the singleton to kill puppies by doing something like this to get the logger from anywhere:

sfContext::getInstance()->getLogger()

Symfony2's container model is stricter, which is great, but how should one go about logging from non-container-aware classes? For repos, I guess you can define them (all) as services, with a dependency on the logger, and go from there. But what about when you just have an instance of an Entity class?

Historically I'd want to put the log message inside class methods, but now? Should I pass the logger (as a parameter) into every class method that wants to write a log message? This seems like a bit of overkill but perhaps it's best practice?

Or am I looking at this wrong and Entities or Repos shouldn't be writing log messages but passing them back to the controller to handle?


回答1:


You should probably avoid putting business logic (even logging) inside entity model.

As for the repositories, the way you described is the right one.



来源:https://stackoverflow.com/questions/19211577/correct-way-to-log-from-entities-and-repositories-in-symfony2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!