I am using Zend_Log, and other required classes, without the MVC framework. I want to add Zend's logging cap开发者_开发百科abilities (and other modules in the future) to my custom classes and I am wondering what the best approach is for doing so.
Right now I have a wrapper for the Zend logger so, presumably, I can access it globally:
My_log::log('Testing', Zend_Log::INFO);
Should I be adding this code to each method in my classes that I want to log? Should I not create logs inside my classes? Is there a smarter way?
I appreciate the help, DC
Dependency injection container seems like a great solution if your app can be integrated. All static calls cause issues in testing environment.
have look around this doc http://components.symfony-project.org/dependency-injection/trunk/book/04-Builder
Worst case i would create a static getter like My_Log::get()->error("message");
the only point is that now you will be able to easily fix the test environemnt to make get return a fake instance. All your My_Log needs is a setLogger($logger)
which will replace the static instance with a mock or whatever. Any way static calls are bad :/ if possible try to decouple classes so they would depend on as few classes as possible. Even injecting logger manually into your class constructor is a better idea. If you have MVC action plugin or a base controller could provide lazy loading getLogger()
so your code could do $this->getLogger()->error('...');
art
精彩评论