Manual:How to debug/zh
该页面对调试MediaWiki软件进行了基本介绍。
您会注意到的第一件事是,“echo”通常不起作用。这是常规设计的一部分。
有几个配置选项可以帮助调试。 以下的选项都是默认false
的。 通过在 $localsettings 中将它们设置为 true
来启用它们:
Enable them by setting them to true
in your LocalSettings.php :
$wgShowExceptionDetails
启用更多细节(例如“栈追踪”)以在“致命的错误”页面上显示。$wgDebugToolbar
在页面上显示带有"分析"、“日志消息”等的工具栏$wgShowDebug
将 wgDebugToolbar 的“日志消息”部分作为原始列表添加到页面。$wgDevelopmentWarnings
MediaWiki 将针对一些可能的错误情况和已弃用的功能发出通知。
Example line to be added in your LocalSettings.php :
$wgShowExceptionDetails = true;
PHP错误
要查看PHP错误,请在LocalSettings.php 中从前面开始的第二行(就在<?php
下面)添加这段代码:
error_reporting( -1 );
ini_set( 'display_errors', 1 );
或在php.ini 设置:
error_reporting = E_ALL
display_errors = On
或在.htaccess中设置:
php_value error_reporting -1
php_flag display_errors On
这将使PHP错误显示在页面上。这可能会使攻击者更容易找到进入你的服务器的方法,因此在找到问题后请禁用它。
请注意,在执行上述行之前有可能发生“致命”PHP 错误,或者可能会阻止信息显示。 致命的 PHP 错误通常记录到 Apache 的错误日志中——检查 php.ini
中的 [$errorlog error_log]
设置(或使用 [$phpinfo phpinfo()]
)。
Fatal PHP errors are usually logged to Apache's error log – check the error_log
setting in php.ini
(or use phpinfo()
).
开启 display_startup_errors
一些提供程序会关闭 display_startup_errors
,即使您提高 error_reporting
级别也会隐藏错误。 在程序中打开它为时已晚! 相反,您必须围绕您的文件创建一个包装文件。 对于 MediaWiki,您只需将其添加到 mediawiki/index.php 之上:
Turning it on within the program is too late! Instead you'll have to create a wrapper file around your file.
In the case of MediaWiki you can just add this on top of mediawiki/index.php:
--- index.php
error_reporting( -1 );
ini_set( 'display_startup_errors', 1 );
ini_set( 'display_errors', 1 );
在其它环境中:
--- myTestFile.php
error_reporting( -1 );
ini_set( 'display_startup_errors', 1 );
ini_set( 'display_errors', 1 );
require 'your_file.php';
SQL错误
要记录所有 SQL 查询,而不仅仅是引发异常的查询,请在 LocalSettings.php
中设置 $dumpsql:
$wgDebugDumpSql = true;
<translate> MediaWiki versions:</translate> |
对于 MediaWiki 1.32 之前的版本,您需要设置 $wgShowSQLErrors 和 $wgShowDBErrorBacktrace 以在 HTML 输出中查看数据库异常的详细信息:
$wgShowSQLErrors = true;
$wgShowDBErrorBacktrace = true;
深度调试
调试器
You can debug your code step-by-step with XDebug . For some common setups, see:
MediaWiki-Vagrant has built in settings for this. If you're not using MediaWiki-Vagrant, but your setup is similar, you can reuse those values. In some cases (e.g. due to a firewall), you may have to use the IDE on the same machine as the web server. In this case, simply set:
xdebug.remote_enable = 1
xdebug.remote_host = 'localhost'
有关详细信息,请参阅 XDebug 文档。
要在 MediaWiki-Vagrant 上调试命令行脚本(例如 PHPUnit 或维护脚本),请使用:
xdebug_on; php /vagrant/mediawiki/path/to/script.php --wiki=wiki ; xdebug_off
根据需要调整脚本、参数和远程主机(它应该是您的 IP 所在计算机的 IP,10.0.2.2 应该适用于 MediaWiki-Vagrant)。
记录
有关更多详细信息,您需要分析和记录错误。
$wgMWLoggerDefaultSpi
,例如在vagrant 框上启用psr3
>角色,则这些设置可能会被忽略。 在这种情况下,请参阅记录器的文档,例如Manual:MonologSpi 。
设置调试日志文件
要将错误和调试信息保存到日志中,请将 $wgDebugLogFile
添加到 LocalSettings.php
文件中。 将值更改为要保存调试跟踪输出的文本文件。
MediaWiki 软件必须从您的操作系统获得创建和写入该文件的权限,例如在默认的 Ubuntu 安装中,它以用户和组 www-data
的身份运行代码:www-data</ code>。
这是一个设置示例:
/**
* The debug log file must never be publicly accessible because it contains private data.
* But ensure that the directory is writeable by the PHP script running within your Web server.
* The filename is with the database name of the wiki.
*/
$wgDebugLogFile = "/var/log/mediawiki/debug-{$wgDBname}.log";
该文件将包含来自 MediaWiki 核心和扩展的大量调试信息。
一些子系统写入自定义日志,请参阅 #Creating a custom log file 以捕获它们的输出。
创建自定义日志文件
<td class="mw-version-versionbox" title="<translate nowrap> MediaWiki <tvar name=1>1.31</tvar> is unsupported version</translate>"><translate> ≤</translate> 1.31
<translate> MediaWiki version:</translate>
在 MediaWiki 1.32 之前,要创建仅包含特定调试语句的自定义日志文件,请使用 wfErrorLog()
函数。
这个函数有两个参数,要记录的文本字符串和日志文件的路径:
wfErrorLog( "An error occurred.\n", '/var/log/mediawiki/my-custom-debug.log' );
创建自定义日志组
如果您正在调试几个不同的组件,将某些日志组写入单独的文件可能会很有用。
参见$wgDebugLogGroups 以获取更多信息。
要设置自定义日志组,请在 LocalSettings.php 中使用以下内容:
/**
* The debug log file should not be publicly accessible if it is used, as it
* may contain private data. However, it must be in a directory to which PHP run
* within your web server can write.
*
* Contrary to wgDebugLogFile, it is not necessary to include a wiki-id in these log file names
* if you have multiple wikis. These log entries are prefixed with sufficient information to
* identify the relevant wiki (web server hostname and wiki-id).
*/
// Groups from MediaWiki core
$wgDBerrorLog = '/var/log/mediawiki/dberror.log';
$wgDebugLogGroups = array(
'exception' => '/var/log/mediawiki/exception.log',
'resourceloader' => '/var/log/mediawiki/resourceloader.log',
'ratelimit' => '/var/log/mediawiki/ratelimit.log',
// Extra log groups from your extension
#'myextension' => '/var/log/mediawiki/myextension.log',
#'somegroup' => '/var/log/mediawiki/somegroup.log',
);
要登录到这些组之一,请像这样调用 wfDebugLog
:
if ( $module->hasFailed ) {
wfDebugLog( 'myextension', "Something is not right, module {$module->name} failed." );
}
如果您已仔细遵循上述说明,但没有任何内容写入日志文件,并且您的系统正在使用 SELinux,请查看 SELinux 页面上的日志部分 以解决此SELinux 上下文相关的问题。
将日志文件写入 /tmp
目录可能根本不会生成任何日志文件,即使 /tmp 目录应该可以被任何人写入。 这种情况可能发生在如果您的系统正在使用为该进程创建虚拟 /tmp 目录的 systemd 功能之一时。 如果是这种情况,请将您的日志文件配置为写入不同的目录,例如 /var/log/mediawiki
结构化日志
MediaWiki版本:
<translate> ≥</translate> 1.25
结构化日志允许您在日志记录中包括字段。
参见Structured logging 以获取更多信息。
您将需要配置一个更好的记录器,例如 Monolog,来收集额外的字段。
JavaScript error logging
<td class="mw-version-versionbox" title="<translate nowrap> The latest stable version is <tvar name=1>1.41</tvar></translate>"><translate> ≥</translate> 1.36
<translate> MediaWiki version:</translate>
See the documentation of the mediawiki.errorLogger ResourceLoader module.
统计
可以使用 Extension:EventLogging 执行高级客户端日志记录,这需要复杂的设置和仔细检查隐私问题。
Simple counting of certain kind of events is possible (since MediaWiki 1.25) using StatsD. StatsD offers meters, gauges, counters, and timing metrics.
用例:
$stats = $context->getStats();
$stats->increment( 'resourceloader.cache.hits' );
$stats->timing( 'resourceloader.cache.rtt', $rtt );
The metrics can be sent to a StatsD server, which may be specified via the wgStatsdServer
configuration variable.
(If not set, the metrics are discarded.)
You can work with StatsD locally (without needing a Graphite server) by starting a StatsD server and configuring it with the "backends/console" backend, which will output metrics to the console.
As of MediaWiki 1.25, wfIncrStats()
is a shortcut for the increment()
method on the main RequestContext::getStats()
instance.
Send debug data to an HTML comment in the output
This may occasionally be useful when supporting a non-technical end-user. It's more secure than exposing the debug log file to the web, since the output only contains private data for the current user. But it's not ideal for development use since data is lost on fatal errors and redirects. Use on production sites is not recommended. Debug comments reveal information in page views which could potentially expose security risks.
$wgDebugComments = true;
Working live with MediaWiki objects
eval.php is an interactive script to evaluate and interact with MediaWiki objects and functions in a fully initialized environment.
$ php maintenance/eval.php
> print wfMessage("Recentchanges")->plain();
Recent changes
The MediaWiki-Vagrant portable virtual machine integrates the interactive PHP shell phpsh
(when using Zend).
Callable updates
Code embedded in the DeferredUpdates::addCallableUpdate()
function, such as $rc->save()
in RecentChange.php
, is not executed during the web request, so no error message will be displayed if it fails.
For debugging, it may be helpful to temporarily remove the code from within the function so that it is executed live.
Interactive shell
Client side debugging (JavaScript)
Wikipedia offers a rich set of tools for debugging client side JavaScript.
In addition to the MediaWiki tools, other techniques are available to assist with diagnosing client interactions.
工具:
- 资源加载器 offers a means to ensure JavaScript is easily viewable by client-side tools.
- Open your browser's console.
Many client side mediawiki scripts log error messages to the console using ResourceLoader, which provides a safety oriented way to log to the client console. Beyond the native JavaScript logging function, it provides a check to ensure that a console is available and that logging does not produce its own error. ResourceLoader/Architecture#Debug_mode also describes this feature.
- Browser tools may provide native functionality to debug client side script.
- Network tracers, like Wireshark can provide insight into the script that is being provided by a page.
- You can add
?debug=true
to your URL as in https://www.mediawiki.org/wiki/MediaWiki?debug=true to get more detailed information for debugging via your browser's console
参见
- ResourceLoader: ResourceLoader/Developing with ResourceLoader#Debugging
- All configuration variables related to debugging/logging: Manual:Configuration settings#Debug/logging
- Useful debugging tip:
throw new MWException( 'foo' );
(dies with the given message and prints the callstack)
- Manual:Errors and symptoms
- 分类:调试变量
- wikitech:Debugging in production - Debugging on Wikimedia's production cluster
- Help:定位损坏脚本