Manual:How to debug/zh

该页面对调试MediaWiki软件进行了基本介绍.

您会注意到的第一件事是，“echo”通常不起作用. 这是常规设计的一部分.



PHP错误
要查看PHP错误，请在 中从前面开始的第二行（就在下面）添加这段代码：

或在设置：

或在.htaccess中设置：

这将使PHP错误显示在页面上. 这可能会使攻击者更容易找到进入你的服务器的方法，因此在找到问题后请禁用它.

Note that fatal PHP errors may happen before the lines above are ever executed, or may prevent them from being shown. Fatal PHP errors are usually logged to Apache's error log – check the  setting in   (or use  ).

有几个配置选项可以帮助调试. 以下的选项都是默认 的. 通过在 中将它们设置为   来启用它们：


 * 启用更多细节（例如“栈追踪”）以在“致命的错误”页面上显示.
 * 在页面上显示带有"分析"、“日志消息”等的工具栏
 * 将 wgDebugToolbar 的“日志消息”部分作为原始列表添加到页面.
 * MediaWiki 将针对一些可能的错误情况和已弃用的功能发出通知.



开启 display_startup_errors
Some providers turn  off, which hides the errors even if you raise the   level. 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:

在其它环境中：



SQL错误
要记录所有 SQL 查询，而不仅仅是引发异常的查询，请在  中设置 ：

对于 MediaWiki 1.32 之前的版本，您需要设置 和  以在 HTML 输出中查看数据库异常的详细信息：



调试器
对于最常见的设置（使用 MediaWiki-Vagrant 和 PhpStorm），请参阅.

Zend
如果您使用 Zend PHP 解释器，您可以使用 XDebug 调试您的代码. MediaWiki-Vagrant 为此提供了 built in settings. 如果您没有使用 MediaWiki-Vagrant，但您的设置类似，您可以重用这些值. 在某些情况下（例如由于防火墙），您可能必须在与 Web 服务器相同的机器上使用 IDE. 在这种情况下，您可以简单地设置：

有关详细信息，请参阅 XDebug 文档.

要在 MediaWiki-Vagrant 上调试命令行脚本（例如 PHPUnit 或维护脚本），请使用：

根据需要调整脚本、参数和远程主机（它应该是您的 IP 所在计算机的 IP，10.0.2.2 应该适用于 MediaWiki-Vagrant）.

记录
有关更多详细信息，您需要分析和记录错误.



设置调试日志文件
要将错误和调试信息保存到日志中，请将  添加到   文件中. 将值更改为要保存调试跟踪输出的文本文件.

MediaWiki 软件必须从您的操作系统获得创建和写入该文件的权限，例如在默认的 Ubuntu 安装中，它以用户和组 的身份运行代码:. 这是一个设置示例：

该文件将包含来自 MediaWiki 核心和扩展的大量调试信息. 一些子系统写入自定义日志，请参阅 #Creating a custom log file 以捕获它们的输出.

数据库事务生命周期 可以使用 为某些数据库启用调试.



创建自定义日志文件
在 MediaWiki 1.32 之前，要创建仅包含特定调试语句的自定义日志文件，请使用  函数. 这个函数有两个参数，要记录的文本字符串和日志文件的路径：



创建自定义日志组
如果您正在调试几个不同的组件，将某些日志组写入单独的文件可能会很有用. 参见以获取更多信息.

要设置自定义日志组，请在 LocalSettings.php 中使用以下内容：

To log to one of these groups, call  like this:



结构化日志
结构化日志允许您在日志记录中包括字段. 参见以获取更多信息.

统计
Advanced client-side logging can be performed with, which requires a complex setup and careful inspection of privacy issues.

Simple counting of certain kind of events is possible (since MediaWiki 1.25) using StatsD. StatsD offers meters, gauges, counters, and timing metrics.

用例：

The metrics can be sent to a StatsD server, which may be specified via the  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,  is a shortcut for the   method on the main   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.



Working live with MediaWiki objects
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 portable virtual machine integrates the interactive PHP shell   (when using Zend).

Callable updates
Code embedded in the  function, such as   in , 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.

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.

工具：

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.
 * ResourceLoader offers a means to ensure JavaScript is easily viewable by client-side tools.
 * Open your browser's console.
 * 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  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:  (dies with the given message and prints the callstack)
 * wikitech:Debugging - debugging on Wikimedia's production cluster
 * wikitech:Debugging - debugging on Wikimedia's production cluster
 * wikitech:Debugging - debugging on Wikimedia's production cluster