I would like to log some variables such as $mageRunCode before it hits:

Mage::run($mageRunCode, $mageRunType);

It appears that Mage::log and even the Zend logging facility don't seem to log out to an arbitrary file output here. How would one do this properly?


Any specific reason you got to do it before mage::run is called in index.php? Are you concerned they get changed?

Attempting to log at that point would require you to adjust core file (index.php) which is not good practice.

Some flow:

In Mage::run you get:

                'scope_code' => $code,
                'scope_type' => $type,
                'options'    => $options,

The variables you are interested in is unchanged at this point.

in Mage_Core_Model_App::run you get this line of code:

Mage::register('application_params', $params);

thus magento internals nicely saves the params used for you in the registry.

To log them, you can use the event 'controller_front_init_before' which is one of the earliest (usable) events fired.

In that event you can pull the saved params, and log them.

$params = mage::registry('application_params');

To note is that this would place a drastic overhead on your sites performance, as it will log on every request. Curious why you would need to do log them?

Earliest usable event

  • Thanks, so yeah, there were some reasons I was thinking that it was being overridden, however the real problem ended up being an Apache issue wherein I was able to rebuild Apache and resolve the Env mod problem. Notwithstanding, your post was very helpful. Thanks! – ylluminate Aug 6 '13 at 7:15

I am assuming that you are trying to log stuff in developer mode/local environment, and you want to fiddle around.

If you want to log something in magento to a custom log file, you can use

Mage::log($myVariable, null, 'myVariable.log');

For this, you need to have logging enabled on the admin backend. This will log stuff to magento_root/var/log/myVariable.log

Tip: You can also print the whole backtrace (for some other deeply laid code) using

Mage::log(Varien_Debug::backtrace(true, true), null, 'backtrace.log');

The advantage of printing the whole backtrace is you can see the exact method/xml flow your call is following to reach that point in execution, anywhere within the php code. Of course, this outputs the result to magento_root/var/log/backtrace.log

  • This will not work, because the config DOM (necessary for logging) is not available prior to Mage::run(). – benmarks Aug 28 '13 at 18:51
  • @benmarks Yes, you are right. I didn't know of it earlier. – Anshul Goyal Aug 28 '13 at 19:13
  • @benmarks Anyway, please help me with a small doubt (totally unrelated to OP). Ideally there wouldn't be issues if I log it after the run statement also, right? The way that variable is defined (camelCase instead of _camelCase), it shouldn't be a global variable, and hence shouldn't have been updated deeper in the code. – Anshul Goyal Aug 28 '13 at 19:30
  • They should be available via $_SERVER superglobal which can be retrieved app-safe via Mage::app()->getRequest()->getServer(). – benmarks Aug 28 '13 at 20:17
  • 1
    @ansh0l A better (and more effective way) to debug, and get your application flow/trace is to use a real debugger with breakpoints (ie xdebug) If you are new to magento, you'll find your understanding of magento will increase in leaps once you start using a debugger, and can actually 'see' at a breakpoint what the internals are up to. Any decent IDE (I like netbeans) will have the ability to use xdebug. – ProxiBlue Aug 29 '13 at 1:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.