[WordPress] 外掛分享: RHJ4 Diagnostics

前言介紹

  • 這款 WordPress 外掛「RHJ4 Diagnostics」是 2014-09-19 上架。
  • 目前尚無安裝啟用數,是個很新的外掛。如有要安裝使用,建議多測試確保功能沒問題!
  • 上一次更新是 2014-10-09,距離現在已有 3860 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.0.1 以上版本才可以安裝。
  • 尚未有人給過這款外掛評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

bob-jones |

外掛標籤

debugging | diagnostics |

內容簡介

RHJ4 Diagnostics 可以捕獲 PHP 中撰寫的診斷訊息,並有條件地將它們導向不同的日誌函數。該外掛可以在頁面執行期間啟用或禁用,並且它可以攔截和處理各種類型的 PHP 錯誤,例如 E_ERROR 和 ~E_NOTICE。文件包含多種用法示例。

診斷可以與 RHJ4 通知緊密結合。每個外掛都使用了另一個外掛中的功能。

使用:

問題:

當我不確定哪個地方出現了錯誤時,追蹤語句是我偏好的調試方法。實現追蹤的常見方法是使用 PHP 的 error_log 函数,該函數將一行文本寫入在 PHP.ini 配置文件中指定的特定文件中。

這種方法很簡單有效,但是也會帶來幾個問題:

1. 之後必須返回代碼並刪除 error_log 呼叫,否則它們將減慢程序速度並填充錯誤日誌,成為現在無用的訊息。
2. 日誌還可能包含形式為 PHP 訊息和堆棧轉儲的不需要儲存的“噪音”。您可以使用 error_reporting(級別)函數控制這些之中的訊息,但有些訊息根本不會消失。
3. 根據您的配置,您可能無法訪問 PHP.ini,因此可能無法查看生成的日誌,因為您無權訪問日誌寫入的位置。

RHJ4Diagnostics:

JavaScript 代碼在瀏覽器中執行,PHP 代碼在服務器中執行,這兩種情況下,您都希望能夠輕鬆捕獲和報告異常情況。

RHJ4Diagnostics 可以在幾個位置上報告其訊息:在瀏覽器的除錯控制台中,在 PHP error_log 中,或者在您指定的函數中。

診斷可以在整個網站或代碼部分中啟用或禁用。由於診斷非常輕量級,因此無需將其從代碼中刪除即可禁用。

PHP API:

PHP 代碼實現為一個類,具有以下功能:

instance():返回外掛實例(不初始化)。

init($options = NULL):初始化外掛。

kill():殺死當前外掛實例。

options ($options = null):解析並設置當前加載頁面的選項。

set($options = NULL):在 wp_options 表中解析和設置選項。注意:支持多站點。選項將在 wp_$blog_id_options 表格中設置。

reset():將所有選項重置為其默認值。

diagnostic($message, $options = NULL):捕獲診斷訊息。注意:如果 $options 是一個字符串,它將被視為是:array('source' - > 'whatever:')。

set_error_reporting($level = null):設置 PHP error_reporting_level。

render_error_level($level):將整數 error_level 值轉換為字符串。

OPTIONS:

以下選項可用於所有接受 $options 的函數:

.enabled 啟用外掛(預設值:false)。如果未啟用,將不會顯示診斷訊息。

.threshold 設置閾值以確定哪些訊息將被日誌記錄。具有高於當前閾值的訊息將被記錄。

原文外掛簡介

RHJ4 Diagnostics captures diagnostic messages written in PHP and conditionally directs them to various different logging functions. The plugin can be enabled or disabled during page execution, and it can intercept and handle various classes of PHP errors, e.g. E_ERROR & ~E_NOTICE. The documentation includes numerous usage examples.
Diagnostics works closely with RHJ4 Notifications. Each uses functionality in the other plugin.
Usage
The Problem
Using trace statements is my preferred method of debugging when I am not
exactly sure where the bug is coming from. A common method for implementing
tracing is to use the PHP error_log function which writes a line of text
into a specific file that is identified in the PHP.ini configuration file.
This method is simple and effective, but there are several problems that come
along with it:

You have to go back through your code later and remove the error_log calls
or they will slow the program down and fill the error log with what are now
useless messages.
The log may also contain undesired “noise” in the form of PHP messages and
stack dumps. Many of these you can control using the error_reporting (level)
function, but some of these messages simply won’t go away.
Depending on your configuration, you may not have access to PHP.ini and you
may not be able to see the generated log because you don’t have access
rights to the location the log was written to.

RHJ4Diagnostics
Diagnostic problems can arise in the Javascript code executing in the browser
or in PHP code executing in the server. In both cases, you want to be able to
easily capture and report the condition.
RHJ4Diagnostics reports its messages in several places: on the Browser’s
debugging console, in the PHP error_log, or to a function you specify.
Diagnostics can be turned on or off for an entire site or for a single section
of code. Since the diagnostics are very lightweight, they don’t have to be
removed from the code in order to be disabled.
PHP API
The PHP code is implemented as a class with the following functions:

instance(): Return instance of plugin (does not initialize).

init($options = NULL): Initialize plugin

kill(): Kill current instance of plugin

options ($options = null): parse and set options for currently loaded page

set($options = NULL): parse and set options in wp_options table. NOTE: Multisite is supported. Options will be set in wp_$blog_id_options table.

reset(): reset all options to their defaults

diagnostic($message, $options = NULL): Capture a diagnostic message. NOTE: $if $options is a string, it will be treated as if it were: array(‘source’ -> ‘whatever:’).

set_error_reporting($level = null): Set PHP error_reporting_level

render_error_level($level): Translate integer error_level value to string

OPTIONS
The following options may be used for all functions that accept $options:

enabled enables plugin (default: false). If not enabled, no diagnostic messages will be displayed.
threshold sets a threshold that determines which messages will be logged. Messages with a threshold above the current threshold will not be written to the log file.
output identifies output handler function (default: ‘rhj4_log’). Output may be a single function name or an array of function names. Function must exist to be set.
source identifies the body of code that diagnostics will be coming from. This string will be prepended to each message.
logfile identifies where output diagnostics will be saved (default: ‘rhj4_diags.log’).
level PHP error reporting level for PHP diagnostics (default: 71 = 64 + 4 + 2 + 1 = ERROR, WARNING, PARSE, COMPILE ERROR). PHP errors will be reported if WP_DEBUG is TRUE. RHJ4Diagnostics will be reported if plugin is enabled.

Option values are stored in the wp_options table using the key ‘rhj4_diagnostic_options’.
SHORTCODES
The shortcode format is:
[rhj4_diagnostics = [...]]

The following code values are supported:
enable [true|false int 1
'threshold' => int 10
'output' => string 'rhj4_log' (length=8)
'source' => string 'DIAG: ' (length=6)
'logfile' => string 'rhj4_diags.log' (length=14)
'level' => int 71
Logfile: D:\ApacheHtdocs\bhamwpdev/wp-content/rhj4_diags.log

09/14/14 04:56:09->DIAG: Page Name [] User [bob_jones]
09/14/14 04:56:55->DIAG: Page Name [diags] User [bob_jones]

rhj4_diags.log deleted

Example:
[rhj4_diagnostics notify=true message="this is a notification" verbose=true]

Will generate a popup window containing the notification message.
NOTE: Use of the notify function requires the installation of the RHJ4 Notifications plugin.
DEMONSTRATION Page: Visit http://bellinghamwordpressdevelopers.com/diags for a demonstration of these shortcodes in action.
EXAMPLES
There are two basic ways to invoke this plugin:

Call rhj4_diagnostic and pass in the message and (optionally) any options

Acquire an instance of the plugin and work with that:
$diags = RHJ4Diagnostics::instance();
$diags holds an instance of the diagnostics object and can be used to invoke methods in the object.

These two lines are equivalent:
$diags->diagnostic('message','SOURCE: ');
$diags->diagnostic('message', array('source' => 'SOURCE: '));

Enable plugin for this page and print a message:
$diags->diagnostic('message',
array('source' => 'SOURCE: ', 'enabled' => true));

This code:
rhj4_diagnostic('this is a message');

Will produce this output in the current log file:
08/13/14 23:36:44->DIAGNOSTICS:this is a message

This example:
$diags = RHJ4Diagnostics::instance();
$diags = RHJ4Diagnostics::instance()->init(array (
'enabled' => true,
'threshold' => 10,
'output' => 'rhj4_log',
'source' => 'DIAGNOSTICS:',
'logfile' => 'rhj4_diags.log',
'level' => E_ALL & ~E_DEPRECATED & ~E_NOTICE & ~E_STRICT
));

$diags->diagnostic($diags->plugin_slug.' enabled');

Will:

Enable the plugin.
Set the message threshold to 10.
Set the output logging function (‘rhj4_log).
Identify the logging file (‘rhj4_diags.log’).
Tell the plugin to report all PHP errors except E_DEPRECATED, E_NOTICE and E_STRICT.

Write a line in the output file like this:
08/13/14 23:36:42->DIAGNOSTICS:rhj4_diagnostics enabled

Demo Code
Add this line of code (or uncomment the line in the plugin source):
add_action('init','rhj4_diagnostics_demo');

and the plugin will execute code in the function rhj4_diagnostics_demo:
Get an instance of this plugin:
$diags = RHJ4Diagnostics::instance();

Reset option settings to their defaults. The reset values are remembered in wp_options:
$diags->clear_log();
$diags->reset();
$options = $diags->options();
var_dump($options);

After reset, plugin will not be enabled, so this next message won’t show:
$message = rhj4_diagnostic('This message will NOT appear in the log.');
var_dump($message);

Enable plugin and change the source message:
$message = $diags->diagnostic('This message WILL appear in log', array(
'enabled' => true,
'source' => 'DEMO: '
));

$options = $diags->options();
var_dump($message);
var_dump($options);

$message = $diags->diagnostic('Turn on reporting of PHP Errors', array(
'level' => E_ALL ));

$options = $diags->options();
var_dump($message);
var_dump($options);

rhj4_diagnostic('End of demo','THE END: ');

Custom Output Handlers
You can write your own output handlers that will direct diagnostics to any file (or database table) that you want. Here is an example:
function my_diagnostic_test_output($message, $logfile = 'rhj4_diags_test.log') {
$timestamp = date('m/d/y H:i:s',time());
$file = fopen($logfile,"a+");
fwrite($file, $timestamp.'->'.$message."\n");
fclose($file);
}

Multiple Output Handlers
//
// This example writes the output to two log files
//
rhj4_diagnostic('more complex message', array(
'source' =>'MULTIPLE FUNCTIONS: ',
'output' => array(
array( 'function' => 'my_diagnostic_test_output',
'logfile' => 'rhj4_diags_test.log'),
array( 'function' => 'rhj4_log',
'logfile' => 'rhj4_diags.log')
)));

SUPPORT
I will attempt to answer your email as quickly as I can, but cannot promise immediate response.
I will entertain ideas for enhancements, especially if I hear the same request from multiple people.
Donations will encourage my support… and my thanks.
CONSULTING
I make my living by helping WordPress developers. If I can help you, please contact me.
Bob Jones
[email protected]
http://bellinghamwordpressdevelopers.com/

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「RHJ4 Diagnostics」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


最新版本

延伸相關外掛(你可能也想知道)

  • P3 (Plugin Performance Profiler) 》這個外掛程式可以創建您 WordPress 網站的外掛程式效能概況,並且藉由測量這些外掛對網站載入時間的影響。通常WordPress網站由於過多或配置不佳的外掛程式而...。
  • Freesoul Deactivate Plugins – Disable plugins on individual WordPress pages 》FDP 可讓您在特定頁面上停用外掛程式,以進行速度優化、偵錯和解決問題。它可適用於所有頁面、文章、定製文章(可公開查詢)、歸檔和後端頁面。, , 使用 FDP...。
  • Fatal Error Notify 》該外掛在偵測到網站發生致命錯誤(或其他錯誤層級,可配置)時,會發送電子郵件通知給您。, 與傳統的運行時間監控服務不同,這個外掛可以在偵測到任何頁面或...。
  • WP Safe Mode 》WP Safe Mode 可讓您暫時停用/啟用特定外掛,並切換到其他佈景主題以檢視您的網站。, 如果您遇到特定外掛或佈景主題的問題並需要進行疑難排解,但又不想影響...。
  • Debug Bar Console 》在除錯列中新增了一個PHP/MySQL控制台。需要 Debug Bar 插件 (v0.5或更新版本)。。
  • Black Bar 》Black Bar 是一個專為 WordPress 開發人員設計的低調的偵錯列(Debug Bar)。它收集並顯示錯誤、執行的 SQL 查詢、緩慢的動作和掛鈎、主題模板、全域變數,並...。
  • WP PHP Console 》, PHP Console 讓你可以透過 Google Chrome 擴充元件 PHP Console 和 PHP Console 伺服器程式庫 處理 PHP 錯誤和例外、輸出變數、遠端執行 PHP 程式等等多種...。
  • Ray 》Ray 是一款美觀、輕量的桌面應用程式,可幫助您進行應用程式的偵錯操作。可使用免費試用版,也可透過許可證進行解鎖。, 安裝此外掛後,您可以使用 ray() 函數...。
  • Editor Cleanup For Elementor: clean up and solve plugin conflicts with the Elementor editor 》Editor Cleanup For Elementor 是一個外掛,屬於 Freesoul Deactivate Plugins 的補充功能,用於整理 Elementor 的編輯器。, 此外掛不僅整理其他外掛的資源,...。
  • Debug Log Viewer 》**總結:**, , Debug Log Viewer 簡化了檢視和管理 WordPress debug.log 檔案的過程。這是開發人員或網站管理員所需的完美工具,他們可以通過追蹤錯誤、警告和...。
  • WP Output Log File 》這個外掛可以控制輸出日誌檔案,無論是否開啟 WP_DEBUG 模式。, 使用此外掛,您可以指定輸出目錄和檔案名稱,並可下載和刪除檔案。, 除此之外,您還可以指定...。
  • Premmerce Dev Tools 》這個外掛旨在為 WordPress 平台上的開發、測試和除錯代碼提供更方便的工具。, 我們根據我們的研究創建了這個外掛:「WordPress 開發環境和開發人員最佳實踐評...。
  • Phpinfo 》此外掛會列印出您的網頁伺服器 PHP 設定以及有關您的 WordPress 安裝的其他資訊。這對於在各種 WordPress 支援論壇上發表文章是很重要的。。
  • Editor Cleanup For Oxygen: FDP add-on to cleanup the Oxygen editor 》Editor Cleanup For Oxygen 是 增加的 Freesoul Deactivate Plugins 的 WordPress 外掛,可清理 Oxygen 的編輯器。, 它不僅可以清理其他外掛添加的資源,它們...。
  • DebugPress: Debugger in Popup 》DebugPress 是一個易於使用的外掛程式,實現了用於調試和分析當前加載的 WordPress 網站頁面的彈出窗口,支持攔截 AJAX 請求。主調試窗口顯示為彈出窗口,通...。

文章
Filter
Mastodon