Manual:How to debug/zh

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

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

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


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

Example line to be added in your :



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

或在设置：

或在.htaccess中设置：

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

请注意，在执行上述行之前有可能发生“致命”PHP 错误，或者可能会阻止信息显示. 致命的 PHP 错误通常记录到 Apache 的错误日志中——检查  中的   设置（或使用  ）.



开启 display_startup_errors
一些提供程序会关闭 ，即使您提高   级别也会隐藏错误. 在程序中打开它为时已晚！ 相反，您必须围绕您的文件创建一个包装文件. 对于 MediaWiki，您只需将其添加到 mediawiki/index.php 之上：

在其它环境中：



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

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



调试器
You can debug your code step-by-step with XDebug. For some common setups, see:


 * MediaWiki-Docker
 * mwcli
 * Vagrant with PHPStorm
 * Vagrant with other IDEs
 * MacOS

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, you can simply set:

有关详细信息，请参阅 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 中使用以下内容：

要登录到这些组之一，请像这样调用 ：



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

JavaScript error logging
See the documentation of the ResourceLoader module.

统计
可以使用 执行高级客户端日志记录，这需要复杂的设置和仔细检查隐私问题.

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.

Interactive shell
You can use as a PHP REPL with full access to MediaWiki internals.

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 in production - debugging on Wikimedia's production cluster
 * wikitech:Debugging in production - debugging on Wikimedia's production cluster
 * wikitech:Debugging in production - debugging on Wikimedia's production cluster