从Symfony2中的实体和存储库登录的正确方法 [英] Correct way to log from Entities and Repositories in Symfony2

查看:81
本文介绍了从Symfony2中的实体和存储库登录的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从symfony2体系结构中的Entity或Repository类记录消息或错误的方法是什么?在symfony1中,您可以使用单例来杀死幼犬,方法是从任何地方获取记录器:

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的容器模型更严格,这很不错,但是如何从非容器感知类进行日志记录呢?对于回购协议,我想您可以将它们(全部)定义为服务,并依赖于记录器,然后从那里开始.但是,当您只有一个Entity类的实例时该怎么办?

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?

从历史上看,我想将日志消息放在类方法中,但是现在呢?我是否应该将记录器(作为参数)传递到每个要编写日志消息的类方法中?这似乎有些矫kill过正,但这也许是最佳做法?

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?

还是我看错了,Entities或Repos不应编写日志消息,而是将它们传递回控制器以进行处理?

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.

这篇关于从Symfony2中的实体和存储库登录的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆