Nick
Nick

Reputation: 8329

CakePHP 2.x: Custom Logging

I've got a CakePHP application that receives instant payment notifications from PayPal. I'd like to log the data that gets posted by PayPal. I could easily do that using something like this:

file_put_contents(LOGS . 'ipns.log', date('Y-m-d H:i:s ') . print_r($_POST, true) . "\n", FILE_APPEND|LOCK_EX);

But I prefer to do things "the CakePHP way™" whenever possible. I've already looked through the "Core Libraries > Logging" section of CakePHP's cookbook and am having trouble understanding it. I know it's not correct to do this:

CakeLog::write('ipns', print_r($_POST, true));

Although the above does seem to work, it can also cause problems, as shown here.

So what is the CakePHP way to do this? Or should I just use the raw PHP shown at the top of this question?

Upvotes: 3

Views: 5218

Answers (2)

Serge S.
Serge S.

Reputation: 4915

According to Writing to log paragraph of Logging section of the 2.x cookbook:

CakeLog does not auto-configure itself anymore. As a result log files will not be auto-created anymore if no stream is listening. Make sure you got at least one default stream set up, if you want to listen to all types and levels. Usually, you can just set the core FileLog class to output into app/tmp/logs/:

CakeLog::config('default', array(
    'engine' => 'File'
));

So, in order to make CakeLog::write('ipns', print_r($_POST, true)); to write to custom file app/tmp/logs/ipns.log you need in app/Config/bootstrap.php instead of:

/**
 * Configures default file logging options
 */
App::uses('CakeLog', 'Log');
CakeLog::config('debug', array(
    'engine' => 'File',
    'types' => array('notice', 'info', 'debug'),
    'file' => 'debug',
));
CakeLog::config('error', array(
    'engine' => 'File',
    'types' => array('warning', 'error', 'critical', 'alert', 'emergency'),
    'file' => 'error',
));

write:

/**
 * Configures default file logging options
 */
App::uses('CakeLog', 'Log');
CakeLog::config('default', array(
    'engine' => 'File'
));

Upvotes: 3

floriank
floriank

Reputation: 25698

What you want is explained here http://book.cakephp.org/2.0/en/core-libraries/logging.html#creating-and-configuring-log-streams

But I would suggest you to read the whole page and not just this section.

I would write the ipn to a database table field by field and not into a file log. I can tell you this based on my experience with the paypal API. The advantages are obviously, you can for example lookup the ipns for an order, search for errors and so on.

Upvotes: 4

Related Questions