Custom Logging in PHP

I've been writing more and more cron jobs lately and, although most of the time they run smoothly, there are moments where everything seems to break at once. It can be tough tracking down these problems since you're not seeing any output while they're running. So, I've started logging all sorts of information. I'm not talking about the system logs here, but instead about custom logs. I'm sure some of it is overkill, but I've been going with the store it and if you don't need it later, you can delete it approach. Here's a quick, easy example:


$logfile = '/export/logs/test-log_'.date('Ymd').'.txt' ;
$logtext = "\n".date('n/j/y g:i a')."\n" ;
if (testFunction()) { $logtext .= "success.\n" ; } else { $logtext .= "FAIL!\n" ; }
$fp = fopen($logfile,"a");
fwrite($fp, $logtext);


Leave a Comment

Name and email are required. Your email will not be published.

This post was published on April 24th, 2010 by Robert James Reese in PHP. Before using any of the code or other content in this post, you must read and agree to our Terms & Conditions.