0

I have a function called logToFile and when I call it, it logs to the file but doesn't add a new line.

This is my code:

function logToFile($msg) {
    $filename = "log.txt";
    $fd = fopen($filename, "a");
    $str = "[" . date("Y/m/d h:i:s", mktime()) . "] " . $msg;
    fwrite($fd, $str . "\n");
    fclose($fd);
}

I've tried:

$msg . "\n"
$msg . "\r\n"

They all output this:

[2013/11/03 06:32:06]Test[2013/11/03 06:34:58]Test2[2013/11/03 06:37:10]Test3
  • 2
    What are you using to view the file? – alexn Nov 03 '13 at 17:52
  • I think it needs to be `$msg .= "\n";` – putvande Nov 03 '13 at 17:53
  • 1
    Notepad does not show `\n`, use `\r\n` instead. Or use a "good" text editor. – Tobias Nov 03 '13 at 17:54
  • This works fine in my machine. It must be the problem with the editor you are using , could you please try with simple command line cat to display the log.txt ? One suggestion is to use file_put_contents with FILE_APPEND flag , which will be very handy – Althaf M Dec 11 '13 at 17:48

3 Answers3

0

Try:

fwrite($fd, $str . PHP_EOL);

This will write the correct type of end-of-line string for the platform on which PHP is running. On Unix it will write \n, on Windows it should write \r\n.

Amal Murali
  • 75,622
  • 18
  • 128
  • 150
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

These \n and \r can be seen only by the browser. So, if you want to see it ok, stop oppening it with NotePad, and open that log.txt file in your browser. For this, try this code:

function logToFile($msg) {
    $filename = "log.txt";
    $fd = fopen($filename, "a");
    $str = "[" . date("Y/m/d h:i:s") . "] " . $msg . "\n";
    fwrite($fd, $str . "\n");
    fclose($fd);
}

But, another way is to use a html file instead of a txt file. And you can use
there. So:

  function logToFile($msg) {
        $filename = "log.html";
        $fd = fopen($filename, "a");
        $str = "[" . date("Y/m/d h:i:s") . "] " . $msg . "<br>";
        fwrite($fd, $str . "\n");
        fclose($fd);
    }

And you can also style it:

$str = "<span style='background-color: red;'>[" . date("Y/m/d h:i:s") . "] " . $msg . "</span><br>";
artur99
  • 818
  • 6
  • 18
0

Aside from the missing new-lines (which is most likely down to Notepad's "features"), you could use the error_log function in PHP. Using them you don't have to worry about the overhead of opening and closing file handles as it's all taken care of for you:

/**
 * logMessage
 * @param string $message
 * @param string $filename
 * @param resource $logHandle
 */
function logMessage($message=null, $filename=null, $logHandle=null)
{
    if (!is_null($filename))
    {
        $logMsg=date('Y/m/d H:i:s').": {$message}\n";
        error_log($logMsg, 3, $filename);
    }

    if (is_object($logHandle))
    {
        try
        {
            $errorPS=$logHandle->prepare("insert into ".LOG_TABLE." (insertDateTime,logText) values (now(),:message)");

            $errorPS->bindParam(':message', $message, PDO::PARAM_STR);

            $errorPS->execute();
        } catch (PDOException $e)
        {
            logError($e->getMessage(), ERROR_LOG);
        }
    }
}

/**
 * logError
 * @param string $message
 * @param string $filename
 * @param resource $logHandle
 */
function logError($message=null, $filename=null, $logHandle=null)
{
    if (!is_null($message))
    {
        logMessage("***ERROR*** {$message}", $filename, $logHandle);
    }
}

The above functions are ones that I wrote for custom logging to a file (and, alternatively, a database table)

Hope this helps

DaveyBoy
  • 2,928
  • 2
  • 17
  • 27