How do you keep debug code out of production?

It may not be perfect but I have a macro in my editor that allows me to add debug and wraps it in appropriate flagging comments. I also have a script that I run later that rips that stuff back out. Granted, it took me a while to really trust this mechanism but over time I've become comfortable with it.

My preference is avoid ever checking in debug code. Obviously as with any other 'rule' there are exceptions to this, but because it's easy to miss things later, I don't like checking it in.


The most simple method

define("DEBUG", true);


if (DEBUG) {
    echo "Debug Method";
}

For js its similar.


One method is with an environmental variable. In your server configuration, you could set an environmental variable to say debug or not. The production servers would be configured to false, and the development to true. That way all you do in the code is check the environmental variable:

In PHP:

if (getenv('DEBUG_MODE')) {
    var_dump($foo);
}

That way, there's no way to forget, since it'll automatically turn itself off. But if you REALLY need to turn it on in production, just flip the switch...

  • Docs for Apache
  • Docs for Lighttpd
  • Docs for NginX

Human error is hard to prevent

https://meta.stackexchange.com/questions/71780/lol-debugging-are-we-so-homepage-alerts-false