3

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?

2
  • I supposed that is the Cake way. It is just buggy. Use this, knowing the issues it can cause, or use pure PHP, which is probably what cake is using underneath. Commented Aug 15, 2012 at 21:35
  • 1
    It is not. The cake way would be to use a custom logger. Commented Aug 15, 2012 at 23:37

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your answer, but I'm still having trouble understanding the cookbook. Also, there's another instance that I'd like to implement logging. I have a form that was occasionally being filled out by bots, so I added some fake fields and hid them with CSS. I'd like to log whenever a bot is detected, like this: if ($this->request->data['Model']['fake_field'] != '') {CakeLog::write('bot_log', 'Bot from ' . CakeRequest::clientIp() . ': ' . print_r($this->request->data, true));}. I want it to be in "bot_log.log" and not "debug.log" or anything else. Would you mind providing a code sample?
3

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'
));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.