0

我正在根据下面的 PHP 代码捕获网页查看器日志...

$viewerlog = "../SiteLogFiles/live-smoothstreaming-" . date('Ymd') . ".txt";
$fp = fopen($viewerlog,"a");
if ($fp) {
    fputs($fp,"-----------------------------------------------------------------------\r\n");
    fputs($fp,"Server Date:           $now\r\n");
    fputs($fp,"IP Address:            $ip\r\n");
    fputs($fp,"City:                  $city\r\n");
    fputs($fp,"State:                 $state\r\n");
    fputs($fp,"Country Name:          $countryname\r\n");
    fputs($fp,"Continent:             $continent\r\n");
    fputs($fp,"-----------------------------------------------------------------------\r\n");
    fclose($fp);
}

它会产生这样的日志......

-----------------------------------------------------------------------
Server Date:           2013-01-06T05:32:06-06:00
IP Address:            157.55.32.61
City:                  Unknown
State:                 Unknown
Country Name:          United States
Continent:             North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date:           2013-01-06T06:02:40-06:00
IP Address:            157.55.32.187
City:                  Unknown
State:                 Unknown
Country Name:          United States
Continent:             North America
-----------------------------------------------------------------------

但时不时地,日志会被损坏,看起来像这样......

-----------------------------------------------------------------------
Server Date:           2012-12-21T18:27:27-06:00
IP Address:            99.54.103.128
City:                  Columbus
State:                 Ohio
Country Name:          United States
Continent:             North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date:           2012-12-21T18:27:53-06:00
IP Address:            75.97.98.115
City:                  Palmerton
State:                 Pennsylvania
--------------------------------------Continent:             North America
Server Date:           2012-12-21T18:27:54-06:00
---------------------
IP Address:            70.76.36.232
City:                  Saskatoon
State:                 Saskatchewan
Country Name:          Canada
Continent:             North America
-----------------------------------------------------------------------
-----------------------------------------------------------------------
Server Date:           2012-12-21T18:28:04-06:00
IP Address:            173.17.240.190
City:                  Springfield
State:                 Missouri
Country Name:          United States
Continent:             North America
-----------------------------------------------------------------------

值得注意的是,我使用http://www.easyjquery.com从 IP 地址获取位置信息。

因此,许多人建议使用 SQL 数据库进行日志记录,而不是纯文本日志。有了这些建议,我就能够建立上述 SQL 数据库。我能够在 1 秒内写入 7 条记录(尽可能快地按 F5),没有问题。

使用中的PHP代码如下...

try {
    $dbh = new PDO("sqlsrv:Server=localhost;Database=ViewerLogs", "", "");
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $stmt = $dbh->query("INSERT INTO dbo.LiveSmoothStreaming (DateTime, Ip, City, State, Country, Continent) VALUES('$now', '$ip', '$city', '$state', '$countryname', '$continent')");
    $stmt->setFetchMode(PDO::FETCH_ASSOC);
    $dbh = null;
}
catch(PDOException $e) {
    echo $e->getMessage();
}

不过,有一个问题。我的纯文本日志似乎比 SQL 日志捕获了更多的记录。

纯文本日志正在生成...

Tue 5:58pm  68.55.34.32 Laurel      Maryland    United States   North America
Tue 5:58pm  70.88.57.62 Port Charlotte  Florida United States   North America
Tue 5:59pm  98.19.198.54    Eastview        Kentucky    United States   North America
Tue 5:59pm  71.3.202.110    Cape Coral      Florida United States   North America
Tue 5:59pm  173.218.32.154  Branson     Missouri    United States   North America
Tue 5:59pm  184.38.238.64   Unknown     Unknown United States   North America
Tue 5:59pm  67.239.18.161   Naples      Florida United States   North America

并且 SQL 日志正在生成...

2013-02-05T17:58:45-06:00   68.55.34.32 Laurel      Maryland    United States   North America
2013-02-05T17:58:52-06:00   70.88.57.62 Port Charlotte  Florida     United States   North America
2013-02-05T17:59:06-06:00   71.3.202.110    Cape Coral  Florida     United States   North America
2013-02-05T17:59:23-06:00   67.239.18.161   Naples      Florida     United States   North America

您可以看到,在短短几分钟内,就有了差异。在几个小时的过程中,差异可能达到数百个。

在这个特定的纯文本日志中没有损坏,所以我看不出它怎么可能是错误的。这将表明 SQL 日志存在问题。我查看了我的 PHP 错误日志中可能存在的问题,但没有发现任何明显的问题(我只是搜索了有问题的页面并查看了这些错误)。

关于为什么 SQL 不会捕获纯文本日志的记录,有什么想法吗?在我的代码中,SQL 日志记录直接出现在纯文本日志记录之后(也许将 SQL 放在纯文本之前会产生相反的结果?)。

4

1 回答 1

1

据我所知,您可以使用文件锁定或系统日志。由于您似乎想让您的应用程序编写不间断的文本块,我认为锁定是可取的。

$lockwait = 2;       // seconds to wait for lock
$waittime = 250000;  // microseconds to wait between lock attempts
// 2s / 250000us = 8 attempts.
$myfile = '/path/to/file.log';

if( $fh = fopen($myfile, 'a') ) {
  $waitsum = 0;
  // attempt to get exclusive, non-blocking lock
  $locked = flock($fh, LOCK_EX | LOCK_NB); 
  while( !$locked && ($waitsum <= $lockwait) ) {
    $waitsum += $waittime/1000000; // microseconds to seconds
    usleep($waittime);
    $locked = flock($fh, LOCK_EX | LOCK_NB);
  }
  if( !$locked ) {
    echo "Could not lock $myfile for write within $lockwait seconds.";
  } else {
    // write out your data here
    flock($fh, LOCK_UN);  // ALWAYS unlock
  }
  fclose($fh);            // ALWAYS close your file handle
} else {
  echo "Could not open $myfile";
  exit 1;
}

您还可以使用这种稍微复杂一些的算法来进行随机“退避”等待,类似于 TCP 和 CSMA/CD 用于避免拥塞的方法。

$max_wait   = 1000000; //  1 s

$min_interval = 10000; // 10 ms
$max_interval = 50000; // 50 ms

$cur_wait = $min_interval;
$tot_wait = 0;
$locked = flock($fh, LOCK_EX | LOCK_NB); 

while( !$locked && ($tot_wait <= $max_wait) ) {
  tot_wait += $cur_wait;
  usleep($cur_wait);
  cur_wait += rand($min_interval, $max_interval);
  $locked = flock($fh, LOCK_EX | LOCK_NB);
}
if( !$locked ) {
  echo "Could not lock $myfile for write within $lockwait seconds.";
} else {
  // write out your data here
  flock($fh, LOCK_UN);
}
fclose($fh);

如果无法获得锁定,脚本会逐渐在锁定尝试之间等待越来越长的时间,直到$max_wait超过定义的时间。

在写完所有这些之后,我想你可能只想考虑将所有这些插入数据库,如果你需要一个日志文件,那么每天运行一次 cron 作业以一次性写出文件,并且可以选择为空桌子。

于 2013-01-10T20:50:59.213 回答