Manual:How to debug/ja
このページでは、MediaWiki をデバッグするソフトウェアの基礎を紹介します。
最初に気付くことの 1 つは、"echo" が全般的に動作しないということでしょう。この挙動は全般的な設計の一部です。
デバッグを支援するための設定オプションがいくつかあります。以下は、既定ではすべて false
に設定されています。
LocalSettings.php で true
に設定することで有効にできます。
$wgShowExceptionDetails
「致命的なエラー」ページにスタック トレースなどの詳細情報を表示できるようにします。$wgDebugToolbar
ページ上にプロファイリング、ログ メッセージなどを含むツールバーを表示します。$wgShowDebug
ログ メッセージをページの下部に生のリストとして追加します。$wgDevelopmentWarnings
MediaWiki は、いくつかの潜在的なエラー状況や廃止予定の関数に対して通知を出します。
LocalSettings.php に追加する例は以下の通りです:
$wgShowExceptionDetails = true;
PHP のエラー
PHP のエラーを閲覧するには、LocalSettings.php の2行目の先頭 (<?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
内の error_log
設定を確認するか、phpinfo()
を使用してください。
display_startup_errorsをonにする
一部のプロバイダーは display_startup_errors
を無効にしており、この場合は error_reporting
レベルを引き上げてもエラーが表示されません。
プログラム内で有効にしても手遅れです。その代わりに、自分のファイルを囲むラッパー ファイルを作成する必要があります。
MediaWiki の場合は、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
内で $wgDebugDumpSql を設定してください:
$wgDebugDumpSql = true;
詳細なデバッグ
デバッガー
XDebug を使用することで、コードをステップごとにデバッグできます。一般的な設定例については以下を参照してください:
- MediaWiki-Docker
- mwcli
- Vagrant with PHPStorm
- Vagrant with other IDEs
- MacOS
- Local dev quickstart (Linux, macOS, Windows) on bare metal
MediaWiki-Vagrant has built in settings for this. MediaWiki-Vagrant を使用していない場合でも、設定が似ていればそれらの値を再利用できます。 場合によっては (例えばファイアウォールの影響などで)、Web サーバーと同じマシン上で IDE を使用しなければならないことがあります。 この場合は単純に以下のように設定します。
xdebug.remote_enable = 1
xdebug.remote_host = 'localhost'
詳細情報については XDebug の説明文書を参照してください。
To debug a command-line script (e.g. PHPUnit, or a maintenance script) on MediaWiki-Vagrant, use:
xdebug_on; php /vagrant/mediawiki/path/to/script.php --wiki=wiki ; xdebug_off
Adjust the script, parameters, and remote host (it should be the IP of the computer where your IP is, 10.0.2.2 should work for MediaWiki-Vagrant) as needed.
ログ作成
For much greater detail, you need to profile and log errors.
$wgMWLoggerDefaultSpi
, for example to enable the psr3
role on a vagrant box, these settings will probably be ignored. In this case, see the documentation of your logger, for example, Manual:MonologSpi .
デバッグ ログ ファイルのセットアップ
To save errors and debugging information to a log, add $wgDebugLogFile
to the LocalSettings.php
file. Change the value to a text file where you want to save the debug trace output.
The MediaWiki software must have permissions from your operating system to create and write to this file, for example in a default Ubuntu install it runs as user & group www-data
:www-data
.
これは設定例です。
/**
* 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";
This file will contain much debug information from MediaWiki core and extensions. Some subsystems write to custom logs, see #Creating a custom log file to capture their output.
カスタムロググループの作成
If you're debugging several different components, it may be useful to direct certain log groups to write to a separate file. 詳細情報は $wgDebugLogGroups を参照してください。
To set up custom log groups, use the following to 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 = [
'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',
];
To log to one of these groups, call wfDebugLog
like this:
if ( $module->hasFailed ) {
wfDebugLog( 'myextension', "Something is not right, module {$module->name} failed." );
}
/tmp
directory may not generate any log file at all, even if the /tmp directory is supposed to be writable by anyone. This could happen if your system is using one of the systemd features that create a virtual /tmp directory for that process. If that's the case, configure your log file to be written into a different directory, like /var/log/mediawiki
構成した記録
MediaWiki バージョン: | ≧ 1.25 |
構成した記録によりログにフィールドを追加できます。 詳細情報は Structured logging を参照してください。
JavaScriptエラーの記録
MediaWiki バージョン: | ≧ 1.36 |
See the documentation of the mediawiki.errorLogger ResourceLoader module.
統計
Advanced client-side logging can be performed with Extension:EventLogging , 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.
使用例:
$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.
出力結果のHTMLコメントにデバッグデータを含める
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/run.php eval > print wfMessage("Recentchanges")->plain(); Recent changes
MediaWiki-Vagrant ポータブルの VM は対話型 PHP シェル phpsh
(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
クライアント側のデバッグ (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.
ツール:
- ResourceLoader offers a means to ensure JavaScript is easily viewable by client-side tools.
- ブラウザのコンソールを開く 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
関連項目
- Manual:デバッグの方法/ログインの問題点
- Manual:Profiling
- ResourceLoader: ResourceLoader/Developing with ResourceLoader#Debugging
- デバッグ・ログ取得に関わるすべての構成変数: Manual:構成設定#デバッグ・ログ取得
- デバッグに使えるヒント:
throw new MWException( 'foo' );
(特定のメッセージにより解除しコールスタックを出力) - Manual:Errors and symptoms
- Category:Debug variables
- wikitech:Debugging in production - Debugging on Wikimedia's production cluster
- Help:壊れたスクリプトを特定する