简体   繁体   English

登录MVC(Zend Framework)

[英]Logging in MVC (Zend Framework)

Is there a best-practice when it comes to where to put the logging functionality in an MVC application, for example a Zend Framework application (Zend_Log)? 在将日志记录功能放在MVC应用程序中的位置是否存在最佳实践,例如Zend Framework应用程序(Zend_Log)? Should I put the logging in the controller or in the model? 我应该将记录放在控制器中还是在模型中? Or in both? 或两者兼而有之?

If in both, should they have the same logger or a separate one? 如果在两者中,它们应该具有相同的记录器还是单独的记录器?

Follow the Information Expert principle in the GRASP guidelines for object-oriented design: 遵循GRASP指南中的信息专家原则,以实现面向对象的设计:

...place a responsibility in classes with the most information required to fulfill it. ......将责任放在具有实现它所需的最多信息的课程中。

So you would write to the log from the class that contains the data you need to log. 因此,您将从包含您需要记录的数据的类写入日志。 If the event you want to log pertains to the work of the model, then write to the log in the model. 如果要记录的事件与模型的工作有关,则写入模型中的日志。 If the event want to log pertains to the work of the controller, then write to the log in the controller. 如果事件想要记录与控制器工作有关的事件,则写入控制器中的日志。

Do create one log output for an app. 为应用程序创建一个日志输出。 Otherwise you'll have to hunt through many log files to find any diagnostic information! 否则,您将不得不寻找许多日志文件以查找任何诊断信息! You can store a log object in the Zend_Registry so you can call up the log from any class in your app. 您可以在Zend_Registry存储日志对象,以便可以从应用程序中的任何类调用日志。


Re your comments: 你的意见:

Better to just fail gracefully if the logger is not found under the expected registry key. 如果在预期的注册表项下找不到记录器,最好只是优雅地失败。 By fail gracefully I mean either output an error to stdout (to the web page) or stderr (to the httpd server log), or throw an exception and let the app handle it. 优雅地失败我的意思是输出错误到stdout(到网页)或stderr(到httpd服务器日志),或抛出异常并让应用程序处理它。

As for dependencies, this is not a problem. 至于依赖关系,这不是问题。 Any time a class uses another class you have a similar kind of dependency. 每当一个类使用另一个类时,你就会有类似的依赖。 See the Registry design pattern. 请参阅注册表设计模式。

Agreeing with Bill Karwin's comment , I would also use a single log output but also take advantage of the ability to filter errors based on priority (for example, there's also a firebug logger which could be setup with ease). 同意Bill Karwin的评论 ,我也会使用单个日志输出,但也可以利用基于优先级过滤错误的功能 (例如,还有一个可以轻松设置的firebug记录器)。

In our main app, we got a db-writer (which is turned into an RSS-feed at a simple page backend) which is also used as the main-log, and emails for critical errors. 在我们的主应用程序中,我们得到了一个db-writer(在一个简单的页面后端变成了一个RSS-feed),它也被用作主日志,并通过电子邮件发送关键错误。 Very handy for sortable data and getting statistics out of it. 非常方便可排序的数据并从中获取统计数据。

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

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