Simple Logging in PHP (File based & one-liner)簡單的測井在PHP (文件為基礎的&一班輪)
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.這將創建一個文件在目錄中的腳本是由引用和附加您的日誌消息。
Filed under提起下 Headline News頭條新聞 , , How To如何 , , PHP PHP的 , , Tech Note技術說明 , , Web網頁 , , Web Services Web服務 , , Windows在Windows | |
| |
RSS 2.0 2.0 | |
Trackback Trackback跟踪 this Article |此文章|
Email this Article電子郵件此文章
You may also like to read您也可以想讀 |





August 23rd, 2006 at 1:24 pm 2006年8月23日在下午1時24分
[...] articles on Open Source Software, PHP, Web, WebLog, How To, Headline News, Tech Note | | RSS 2.0 | Trackback this Article | Email thisArticle [...] [ … … ]文章對開放源碼軟件的, Web網站,博客,如何,頭條新聞,技術說明| | 2.0 | Trackback跟踪此文章|通過電子郵件發送thisarticle [ … … ]
September 11th, 2007 at 11:44 pm 2007年9月11日在下午11時44分
Thanks a zillion for this hint!感謝zillion這暗示!
It\’s EXACTLY what I needed.它\奇摩正是我所需要的。 I hope there isn\’t some kind of exploit as it almost seems too good to be true.我希望有isn \ '噸某種形式的剝削,因為它幾乎似乎過於真正的好。
[it also makes for a nice alternative to logging services that add cookies and such. [它也使得一個很好的替代測井服務,添加Cookie和等。 I mean, I use \'em - I like to know what keywords are used, how many pages, entry and exit pages and all of that --- but sometimes...我的意思是,我使用\ '教統-我想知道什麼樣的關鍵字的使用,多少網頁,進入和退出頁面和所有的- --但有時. .. sometimes you just want a stinkin\' IP address!!有時候您只是想要一stinkin \ '的IP地址!
I\’ma microsoft excel NUT, so I like to be able to have massive amounts of data to sort through and make my own graphs, charts, filters, ideas, implementations. i \ '馬的Microsoft Excel螺母,所以我希望能夠有大量的數據進行排序,使我自己的圖形,圖表,過濾器,思想,實現。
Your one little line of code is just what the doctor ordered.您的一個小的代碼行,只是什麼醫生命令。
Ken of Naples FL USA - grateful.縣佛羅里達州那不勒斯的美國-表示感謝。
September 12th, 2007 at 8:41 pm 2007年9月12日在下午8時41分
This isn’t an exploit這不是一個利用