-2

My code:

function log_this($to_log, $prepend = null){

    ob_start();
    $fn = '../info.log';        
    $fp = fopen($fn, 'a');

    fputs($fp, "\r\rnew log -------- \r\r");

    if(isset($prepend)) fputs($fp, $prepend . ":\r\r");

    var_dump($to_log);
    $content = ob_get_contents();

    fputs($fp, $content);
    fclose($fp);
    ob_end_clean();
}

It's a function I always use in my local environment (MAMP) to log things from wordpress. it always worked. Now it doesn't work anymore. I tried to understand why for a couple of days but can't find any solution. I'm not a really advanced php programmer, so maybe there's something I don't know and should.. Can anyone help me please?

By the way, function_exists and also file_exists, from where I call it.

lanzz
  • 42,060
  • 10
  • 89
  • 98
Luca Reghellin
  • 7,426
  • 12
  • 73
  • 118
  • 2
    You're saying "it doesn't work anymore", which means exactly nothing. Are you getting an error? Are you getting no `var_dump()` written to the file, or are you getting _nothing_ written (i.e. not even `new log --------`)? – lanzz Sep 02 '12 at 09:46
  • are you sure `log_this` function is called? Can you log something like `error_log(__METHOD__);` in your function and check whether you got value in apache error log? – Kalpesh Sep 02 '12 at 09:46
  • @lanzz: as I wrote, the purpose of that function is to log things. So I mean it doesn't log anymore. Even if, as I wrote, function_exists and also file_exists. info.log remains blank. – Luca Reghellin Sep 02 '12 at 15:20

1 Answers1

1

I'm not sure why fputs isn't working, it is probably to do with your servers folder permissions (usually 0755 or 0775 is safe), could also add a condition to check is_writable to eliminate that possibility. Have you tried using file_get_contents and file_put_contents.

define('FILE_PATH', 'path/to/file/file.log'); 
    function log_this($command, $array = null) {
    //check if we can get to the file
    if(file_exists(FILE_PATH)){
    $current = file_get_contents(FILE_PATH);
    $current .= $command;
    if(!is_null($array)){
    ob_start();print_r($array);$output = ob_get_clean();
    $current .= $output;
    }
    file_put_contents(FILE_PATH, $current);
    }
 }
classicjonesynz
  • 4,012
  • 5
  • 38
  • 78
  • Thank you, I'll try and eventually post results! – Luca Reghellin Sep 02 '12 at 15:21
  • Ok man... I forgot I had more than one info.log file around, and the function didn't throw any error because it in fact was writing a real file, not the one I was looking into. Mmmm... Flushing here.. :P – Luca Reghellin Sep 02 '12 at 15:43