redrom
redrom

Reputation: 11642

CakePHP how to log API requests globally?

I have an API based on CakePHP, controller with name AccessLogController is responsible for saving access log into the database.

Question is:

What is the best practice for global logging in CakePHP?

I thought that I will call AccessLogController method from inherited AppController in before filter callback like this:

 public function beforeFilter() {
        $accessLogCtrl = new AccessLogsController();
        $accessLogCtrl->add($param1, $param2);
    }

But i not sure that is it a good way how to do it..

Many Thanks for any advice..

Upvotes: 0

Views: 584

Answers (1)

ndm
ndm

Reputation: 60463

That's not how you should use controllers, never ever, except maybe in your test suite!

That being said, using AppController::beforeFilter() to globally log controller action requests is generally fine, just make sure that you always invoke parent::beforeFilter() in case you are overriding the filter in an extending controller.

However, you should definitely refactor your logging functionality into either a utility class, a component, a model, or even directly into AppController, depending on how it's actually logging things, and if you need to use it in places other than in AppController.

Upvotes: 1

Related Questions