简体   繁体   English

PHP日志记录-不会添加新行

[英]PHP Logging - won't add new line

I have a function called logToFile and when I call it, it logs to the file but doesn't add a new line. 我有一个名为logToFile的函数,当我调用它时,它会记录到文件中,但不添加新行。

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

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. 这将为运行PHP的平台编写正确类型的行尾字符串。 On Unix it will write \\n , on Windows it should write \\r\\n . 在Unix上它将写入\\n ,在Windows上应该写入\\r\\n

These \\n and \\r can be seen only by the browser. 这些\\n\\r只能由浏览器看到。 So, if you want to see it ok, stop oppening it with NotePad, and open that log.txt file in your browser. 因此,如果您希望它没问题,请停止使用NotePad代替它,然后在浏览器中打开该log.txt文件。 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. 但是,另一种方法是使用html文件而不是txt文件。 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>";

Aside from the missing new-lines (which is most likely down to Notepad's "features"), you could use the error_log function in PHP. 除了缺少换行符(很可能归因于记事本的“功能”)之外,还可以在PHP中使用error_log函数。 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 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM