內容簡介
啟用 WordPress 插件和主題開發的系統記錄。這可以讓您更容易知道您的代碼正在做什麼。
特點包括:
記錄到系統日誌或文件
輕鬆添加或嵌入日誌消息中的任何值
顯示不同類型的值的不同顏色,包括字符串、數字、數組、對象、空值等
從對象數組中提取值
可選地排除 AJAX、JavaScript、CSS 和任何其他 URL 模式
攔截 PHP 錯誤和登錄嚴格警告
測量兩點之間的記憶體使用量,或各部分代碼所花費的時間
易於關閉以使用於生產環境
如何使用
使用它就像在模板或插件中調用 'log' 操作一樣簡單。
<?php do_action('log', '一些日誌消息'); ?>
這將在系統日誌中產生一行:
Jun 4 11:23:08 myserver php/mysite.com[1553]: b8e mysite.com/path-to-page: 一些日誌消息
包括:
Jun 4 11:23:08 - 日誌消息的日期和時間
myserver - 此計算機的名稱
php - 產生消息的程序
mysite.com - 網站的域名
1553 - 正在運行的 PHP 過程的進程 ID
b8e - 标识每个页面请求的随机三位代码
mysite.com/path-to-page - 請求的 URL
一些日誌消息 - 您的消息
有關更詳細的說明,請參見“如何使用”標籤頁。
外掛標籤
開發者團隊
原文外掛簡介
Enable system logging for WordPress plugin and theme development. This can make it easier to know what your code is doing.
Features include:
Log to the system log or to a file
Easily append or embed any value in log messages
Coloured logs indicating strings, numbers, arrays, objects, null values etc.
Extract values from arrays of objects
Optionally exclude AJAX, Javascripts, CSS and any other pattern of URLs.
Intercept PHP errors and log strict warnings
Measure memory usage between two points, or the time taken by various parts of your code
Easy to switch off for production sites
How to use
Using it is as simple as calling the 'log' action in your templates or plugin.
This will produce a line in your system log:
Jun 4 11:23:08 myserver php/mysite.com[1553]: b8e mysite.com/path-to-page: Some log message
This includes:
Jun 4 11:23:08 – The date and time of the log message
myserver – The name of this computer
php – The program that produced the message
mysite.com – The domain name of the site
1553 – The process ID of the running PHP process
b8e – A random 3-digit code identifying each page request
mysite.com/path-to-page – The URL of the request
Some log message – Your message
For more detailed instructions, see the How to use tab.
How to use
To log a message, use the 'log' action in your templates or plugins:
This will output the log message:
Some log message
Embedding values
If you include extra parameters, they’ll be added at the end of the log message:
This will produce the log message:
A number and a string: 17, foo
The value will be formatted correctly depending on its type: integers, strings, arrays, objects, booleans etc.
You don’t need to check if values are null or empty, they’ll still be output safely.
If you put the code %s into your log message, then one of the arguments will be dropped into the message:
This will produce the log message:
I have 4 numbers: [9, 16, 307, 1]
Selecting fields
Logging a complete object – such as a WordPress post – can be very large, and sometimes it’s only one or two fields you need.
If you put a string starting with an exclamation point "!" followed by a list of field names, they will be selected from the following object.
The following will only show the ID and post_title fields of the post:
This will produce the log message:
Loaded the post: {ID: 1932, post_title: Test page}
If you do this with an array of objects, those fields will be selected from each of them. The following will output a list of post IDs:
This will produce the log message:
Loaded 3 posts: [1932, 1594, 1103]
Coloured logs
If you have coloured logging switched on, values will appear in different colours to indicate their type.
This can make for quicker scanning of log files.
To enable coloured logs, tick the Coloured logs checkbox on the settings page.
Then use the log.sh script, included with this plugin, to decode and display the coloured log files.
