Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7067411
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:11:56+00:00 2026-05-28T05:11:56+00:00

I would like to extend the cakephp logging facility. Using $this->log($msg, $level) you can

  • 0

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:

  1. 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.
  2. Automatically put the name of the class in front of a message, e.g. $this->debug($msg) will lead to “MyClass: $msg” if $this is 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T05:11:57+00:00Added an answer on May 28, 2026 at 5:11 am

    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:

    1. Create a function (eg. some custom functions such as error() that call CakeLog::write() in the way you like.)
    2. Load a class (eg. load your very own logging class called something like.. Log, so you can call Log::error() in places)
    3. See below:

    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):

    $this->log($msg, $level) // any class extending `Object` inherits this
    // or
    CakeLog::write($level, $message); // this is actually what is called by the above
    

    The arbitrary $level parameter that you are trying to eliminate is actually quite a powerful feature:

    $this->log('Cannot connect to SMTP server', 'email'); // logs to app/logs/email.log
    // vs
    $this->email('Cannot connect to SMTP server'); // ambiguous - does this send emails?
    

    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:

    function log($msg, $type = LOG_ERROR) {
        if (!class_exists('CakeLog')) { // winning
            require LIBS . 'cake_log.php';
        }
        // ...
    

    As you can see, the core CakeLog class 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):

    // app/config/bootstrap.php
    App::import('Lib', 'CakeLog'); // copy cake/libs/cake_log.php to app/lib/cake_log.php
    

    The above would give you full control over the implementation of the CakeLog class 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:

    CakeLog::config('file', array(
        'engine' => 'FileLog', // copy cake/libs/log/file_log.php to app/libs/log/file_log.php
    ));
    

    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. 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I extend a builtin class in python? I would like to add
I would like to use Google OAuth in my cakePHP application to let the
I would like to extend Entity\Base classes, how to do this in Doctrine 2.1?
I'm using ExtJS 3.0 and I would like to extend FormPanel or any solution
So i'm using kohana user module, and i would like to extend my register
I would like to extend a kd-tree (2D) class to be able to remove
I would like to create extend a Java Swing application to have a look
I'm trying to extend my ActiveRecord class with some dynamic methods. I would like
I would like to use Visual Studio 2008 to the greatest extent possible while
Would like to get a list of advantages and disadvantages of using Stored Procedures.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.