How to enable/disable debug messages for a certain action

Published on and tagged with cakephp  faq

An often asked question in the CakePHP IRC channel is: “How can I enable [or disable] the debug messages for a certain action?” The answer to this question is simple (works in both CakePHP 1.1 and 1.2):

function action() {
    Configure::write('debug', '2');

    // do something
}

Please notice that the key must be in lowercase, ‘DEBUG’ won’t have the desired effect.

PS: I shut down the j.42dh.com password manager service as nobody used it ;-) A slightly modified version of the application can be downloaded from http://apps.42dh.com/jumpjump.

10 comments baked

  • Dejan Ranisavljevic

    Nice snippet, i have never before used Configure class. So now i see the point of it …

    no more editing core.php all the time; :)

    Thanks.

  • kojote

    Jesus, please don’t edit core all the time, write a simple
    if($_GET[‘debug’]) or a session base debug activator with
    a cookie-extension for firefox. best way to activate debugging on live servers by the way.

    nice snippet anyway :)

  • cakebaker

    @Dejan, kojote: Thanks for your comments!

    @kojote: You should not debug on live servers ;-)

  • wluigi

    Tell me if I m wrong, but you have to write

    Configure::write(‘debug’, ‘2’);

    at the end of your action to see the sql queries

    if you end up with

    Configure::write(‘debug’, ‘0’);

    no query is displayed

  • cakebaker

    @wluigi: Yes, that’s correct. The same values as used in app/config/core.php for the DEBUG constant.

  • zheka

    does not quite work for me

    I am trying to suppress debug output on some of the actions while leaving application-wide debug level at 2.

    so i included the following line inside actions i so not want want to output debug info:

    Configure::write(‘debug’, ‘0’);

    however i still get execution time and sql info appended at the end of the page.

    I can see that the code does have some effect – “print_r” calls do not work which is behavior consistent with debug level 0

    am i missing something?

    i am running 1.1.12.4205

  • cakebaker

    @zheka: Hm, I am not sure if it works with 1.1.12.x. I would try it with the latest stable release.

  • zheka

    thanks

  • Rubens Mariuzzo

    Thanks, that save me a lot of work.

  • cakebaker

    @Rubens: You are welcome!

Bake a comment




(for code please use <code>...</code> [no escaping necessary])

© daniel hofstetter. Licensed under a Creative Commons License