简体   繁体   English

从Symfony2中的实体和存储库登录的正确方法

[英]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? 在symfony2体系结构中,从Entity或Repository类记录消息或错误的方法是什么? In symfony1 you could use the singleton to kill puppies by doing something like this to get the logger from anywhere: 在symfony1中,您可以使用单例来杀死幼犬,方法是从任何地方获取记录器:

sfContext::getInstance()->getLogger()

Symfony2's container model is stricter, which is great, but how should one go about logging from non-container-aware classes? Symfony2的容器模型更严格,这很不错,但是如何从非容器感知类进行日志记录呢? 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? 但是,当您只有一个Entity类的实例时该怎么办?

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? 这似乎有些矫kill过正,但这也许是最佳做法?

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? 还是我看错了,实体或回购协议不应该编写日志消息,而是将它们传递回控制器进行处理?

You should probably avoid putting business logic (even logging) inside entity model. 您可能应该避免将业务逻辑(甚至记录)放入实体模型中。

As for the repositories, the way you described is the right one. 至于存储库,您描述的方式是正确的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM