I would like to extend the cakephp logging facility.
Using
$this->log($msg, $level)
you can log a $msg with $level to tmp/logs/$level.log.
How I would like to use logging is:
- Separate functions for different levels, e.g.
$this->debug($msg)for$this->log($msg, 'debug')and$this->error($msg)for$this->log($msg, 'error')etc. for logging. - Automatically put the name of the class in front of a message, e.g.
$this->debug($msg)will lead to “MyClass: $msg” if$thisis of type “MyClass”.
I know I can extend the functionality by extending AppModel, AppController etc., but as I need the functionality everywhere in my application, I would rather need to extend cakephp’s Object – but didn’t find a stable mechanism for that (don’t want to change it in the cake/ folder).
I though about implementing a new class for that functionality, but I’m not sure how to make that available in cakephp.
Could you please give me some hints where/how I can implement these extensions neatly?
Global convenience methods
Well, you can’t really do monkey patching in PHP (5.2 at least), and that is probably a good thing for the the developers that have to maintain your code after you are gone. 🙂
CakePHP – being an MVC framework with strict conventions – makes it hard for you to break the MVC paradigm by only allowing you the extend the parts you need in isolation (ie.
AppModel,AppController, etc.) and keeping the object-orientated foundation untouched in the core (making it hard to add code that “can be used everywhere” for potential misuse).As for adding functionality that transcends all the MVC separation, the place for this is
app/config/bootstrap.php. When you place code here it seems clear that it is not part of the framework (quite rightly so), but allows you to add these sort of essentials before CakePHP even loads. A few options of what to do here might be:error()that callCakeLog::write()in the way you like.)Log, so you can callLog::error()in places)The logger API
Cake does allow for many customisations to be made to things like the logger, but unfortunately the API exposed to us is already defined in the core in this case. The API for logging in CakePHP is as follows, and you can use either approach anywhere you like (well, the former only in classes):
The arbitrary
$levelparameter that you are trying to eliminate is actually quite a powerful feature:We just created a brand new log type without writing an extra line of code and it’s quite clear what the intention of our code is.
Customising the logger
The core developers had the foresight to add a condition allowing us to completely replace the logger class should we wish to:
As you can see, the core
CakeLogclass only gets instantiated if no such class exists, giving you the opportunity to insert something of your own creation (or an exact copy with a few tweaks – though you would want to sync changes with core – manually – when upgrading):The above would give you full control over the implementation of the
CakeLogclass in your application, so you could do something like dynamically adding the calling class name to your log messages. However, a more direct way of doing that (and other types of logging – such as to a database) would be to create a custom log stream:TL;DR – Although you can load your own code before CakePHP bootstraps or for use in isolation in each of the MVC layers provided, you shouldn’t tamper with the object hierarchy provided by the core. This makes it hard to add class methods that are inherited globally.
My advice: use the API given to you and concentrate on adding more features instead of syntactical subtleties. 🙂