Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/239.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP日志记录-赢得';不要添加新行_Php_Function_Logging_Newline_Fwrite - Fatal编程技术网

PHP日志记录-赢得';不要添加新行

PHP日志记录-赢得';不要添加新行,php,function,logging,newline,fwrite,Php,Function,Logging,Newline,Fwrite,我有一个名为logToFile的函数,当我调用它时,它会记录到文件中,但不会添加新行 这是我的代码: 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); }

我有一个名为
logToFile
的函数,当我调用它时,它会记录到文件中,但不会添加新行

这是我的代码:

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);
}
我试过:

$msg . "\n"
$msg . "\r\n"
它们都输出以下内容:

[2013/11/03 06:32:06]Test[2013/11/03 06:34:58]Test2[2013/11/03 06:37:10]Test3
尝试:


这将为运行PHP的平台编写正确类型的行尾字符串。在Unix上它将写入
\n
,在Windows上它应该写入
\r\n
这些
\n
\r
只能通过浏览器看到。所以,如果您想看到它,请停止使用记事本打开它,并在浏览器中打开log.txt文件。 为此,请尝试以下代码:

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);
}
但是,另一种方法是使用html文件而不是txt文件。您可以在那里使用
。因此:

  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);
    }
函数日志文件($msg){
$filename=“log.html”;
$fd=fopen($filename,“a”);
$str=“[”.date(“Y/m/d h:i:s”)。“].”$msg.
“; fwrite($fd,$str.“\n”); 现金流量表(美元); }
您还可以设置它的样式:

$str = "<span style='background-color: red;'>[" . date("Y/m/d h:i:s") . "] " . $msg . "</span><br>";
$str=“[”.date(“Y/m/d h:i:s”)。“]$味精。“
”;
除了缺少新行(很可能是记事本的“功能”),您还可以在PHP中使用
error\u log
函数。使用它们,您不必担心打开和关闭文件句柄的开销,因为这一切都由您来完成:

/**
 * 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);
    }
}
上面的函数是我为自定义记录到文件(或者数据库表)而编写的函数


希望这对您有所帮助

您使用什么来查看文件?我想它应该是
$msg.=“\n”记事本不显示
\n
,请改用
\r\n
。或者使用一个“好”的文本编辑器。这在我的机器上很好用。您使用的编辑器一定有问题,请尝试使用简单的命令行cat来显示log.txt,好吗?一个建议是使用带有file\u APPEND标志的file\u put\u内容,这将非常方便
/**
 * 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);
    }
}