Correct way to log from Entities and Repositories

2019-06-20 09:29发布

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条回答
可以哭但决不认输i
2楼-- · 2019-06-20 10:31

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

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

查看更多
登录 后发表回答