Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Correct way to log from Entities and Repositories in Symfony2

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?

like image 649
caponica Avatar asked Oct 06 '13 17:10

caponica


1 Answers

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

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

like image 113
Jovan Perovic Avatar answered Nov 15 '22 00:11

Jovan Perovic