Often we (php developers) need to log certain messages in order to debug our script.我們常常( PHP開發)需要先登錄,在特定的信息,以便調試我們的腳本。 Here I will describe the default syslog method and a much simpler (and better) way to log your messages quickly and painlessly.在這裡,我將描述默認的系統記錄方法和簡單得多, (和更好的)的方式登錄您的郵件迅速和painlessly 。

PHP manual suggests using PHP手冊建議採用 syslog系統記錄 for logging.為記錄。

Problems with 問題 syslog 系統記錄
The problem with syslog is that its implementation is system dependent.這個問題與系統記錄的是其執行系統的依賴。 For example openlog() is not supported on windows.例如openlog ( )是不支持在Windows上使用。

The location of the logged data is system dependent, which you may not have access to (if you are on a shared web hosting environment) or know about.位置登錄的數據是系統的依賴,您可能無法訪問(如果您是在一個共用的Web託管環境) ,或知道。

The procedure is more cumbersome than you may like to undertake for simple quick and dirty log messages.程序較為繁複的比你可能想進行一些簡單的快速和骯髒的日誌消息。

Using syslog 使用系統日誌
Here is a sample code using syslog:這裡是一個範例程式碼,使用系統記錄:

< ?php < ? PHP的
define_syslog_variables(); define_syslog_variables ( ) ;
// open syslog, include the process ID and also send / /開放系統記錄,包括進程ID ,並同時發送
// the log to standard error, and use a user defined / /日誌,以標準誤差,並使用用戶定義
// logging mechanism / /測井機制
openlog(”myScriptLog”, LOG_PID | LOG_PERROR, LOG_LOCAL0); openlog ( “ myscriptlog ” , log_pid | log_perror , log_local0 ) ;

// some code / /一些代碼

if (authorized_client()) {如果( authorized_client ( ) ) (
// do something / /做點事
} else { )否則(
// unauthorized client! / /未經授權的客戶端!
// log the attempt / /記錄的企圖
$access = date(”Y/m/d H:i:s”);美元獲得=日期(的“ Y /米/小時:一:的” ) ;
syslog(LOG_WARNING, “Unauthorized client: $access $_SERVER[REMOTE_ADDR] ($_SERVER[HTTP_USER_AGENT])”);系統日誌( log_warning , “未經授權的客戶:元元進入_server [ remote_addr ] (元_server [ http_user_agent ])");
}

closelog(); closelog ( ) ;
?> ? >

Simpler alternative 簡單的替代
Now lets look into a quicker and simpler alternative for your simple logging needs.現在讓看成為一個更快和更簡單的替代為您簡單的測井需要。
file_put_contents file_put_contents (’log_file_name’, $data, FILE_APPEND); ( ' log_file_name ' ,元數據, file_append ) ;

$data is the data you want logged.元數據是數據,你想記錄下來。 You can also pass in an array.您也可以通過在一個數組。 This creates a file in the directory the script is invoked from and appends your log messages.這將創建一個文件在目錄中的腳本是由引用和附加您的日誌消息。