2009-12-14 153 views
0

代碼工作正常,除了事實,這裏有一個問題:我想你不追加到文件PHP的日誌記錄問題

//Log Events 
function logEvent($newinput) { 
    if ($newinput !== NULL) { 
     // Add a timestamp to the start of the $message 
     $newinput = date("Y/m/d H:i:s").': '.$newinput; 
     $fp = fopen('log.txt', 'w'); 
     fwrite($fp, $newinput."\n"); 
     fclose($fp); 
    } 
} 
//Problem writing these two lines to log.txt? 
//The bad, the two lines below are not on the log.txt 
logEvent('Selection'.$selections[$selection]); 
logEvent('Change' . $change. 'cents.'); 

//This line is written to a text file (log.txt), okay that's good. 
logEvent('Input' . $newinput); 
+0

對不起Newb,我應該注意到,當我第一次寫這個函數。 – 2009-12-14 13:48:14

回答

1

,你重寫它。試着用'a'而不是'w'打開。

+0

甜蜜,謝謝,只是想知道周圍的人有沒有睡過... – Newb 2009-12-14 13:42:26

+0

它可能是一天中的任何時間,你會得到比閃電更快的反應,那真棒。 – Newb 2009-12-14 13:43:20

+0

再次感謝ufk。 – Newb 2009-12-14 13:44:19

0

您需要使用追加修改打開文件時,你已經走了

的fopen( 'log.txt的', 'W');

這意味着每次調用該函數,日誌文件是越來越吹出來並重新創建,如果用來代替

的fopen(「log.txt的」,「A」);

然後您的新日誌條目將追加到該文件。

您也可以考慮讓文件保持打開狀態以便以後插入,但其他請求中可能存在多個更新的問題。