Handbuch:Wie man debuggt
Diese Seite gibt eine grundlegende Einführung in das Debugging von MediaWiki-Software.
Eines der ersten Dinge, die du bemerken wirst, ist, dass "Echo" generell nicht funktioniert; das ist Teil des allgemeinen Designs.
Es gibt verschiedene Konfigurationsoptionen, die das Debugging erleichtern. Die folgenden sind alle standardmäßig false
. Aktiviere sie, indem du sie in deinen LocalSettings.php auf true
festlegst:
$wgShowExceptionDetails
Aktiviere, dass auf der Seite "Schwerwiegender Fehler" mehr Details (z. B. ein Stack-Trace) angezeigt werden.$wgDebugToolbar
Zeigt eine Symbolleiste auf der Seite mit Profilerstellung, Protokollmeldungen und mehr.$wgShowDebug
Fügt den "Logmeldungen"-Teil der wgDebugToolbar als rohe Liste zur Seite hinzu.$wgDevelopmentWarnings
MediaWiki wird Benachrichtigungen für einige mögliche Fehlerbedingungen und für veraltete Funktionen ausgeben.
PHP-Fehler
Um PHP-Fehler zu sehen, füge dies in die zweite Zeile von oben (direkt unter der <?php
) von LocalSettings.php ein:
error_reporting( -1 );
ini_set( 'display_errors', 1 );
Oder setze es in php.ini :
error_reporting = E_ALL
display_errors = On
Oder setze es in .htaccess:
php_value error_reporting -1
php_flag display_errors On
Dies führt dazu, dass PHP-Fehler auf der Seite angezeigt werden. Dies könnte es Angreifern erleichtern, einen Weg in deinen Server zu finden. Deaktiviere es daher wieder, wenn du das Problem gefunden hast.
Beachte, dass fatale PHP-Fehler auftreten können, bevor die obigen Zeilen überhaupt ausgeführt werden, oder dass sie nicht angezeigt werden können. Fatale PHP-Fehler werden normalerweise im Apache-Fehlerprotokoll protokolliert - überprüfe die Einstellung error_log
in der php.ini
(oder verwende phpinfo()
).
display_startup_errors einschalten
Einige Anbieter schalten display_startup_errors
ab, wodurch die Fehler verborgen bleiben, auch wenn du die error_reporting
Stufe erhöhst. Wenn du sie im Programm einschaltest, ist es zu spät! Stattdessen musst du eine Wrapper-Datei um deine Datei herum erstellen. Im Fall von MediaWiki kannst du diese einfach über mediawiki/index.php einfügen:
--- index.php
error_reporting( -1 );
ini_set( 'display_startup_errors', 1 );
ini_set( 'display_errors', 1 );
In mother embrion mers:
--- myTestFile.php
error_reporting( -1 );
ini_set( 'display_startup_errors', 1 );
ini_set( 'display_errors', 1 );
require 'your_file.php';
SQL-Fehler
Um alle SQL-Abfragen zu protokollieren und nicht nur diejenige, die die Ausnahme ausgelöst hat, legst du $wgDebugDumpSql in LocalSettings.php
fest:
$wgDebugDumpSql = true;
MediaWiki Versions: | 1.16 – 1.31 |
Vor MediaWiki 1.32 musstest du $wgShowSQLErrors und $wgShowDBErrorBacktrace festlegen, um Details von Datenbankausnahmen in der HTML-Ausgabe zu sehen:
$wgShowSQLErrors = true;
$wgShowDBErrorBacktrace = true;
Eingehendes Debugging
Debugger
Für die gebräuchlichste Einrichtung (mit MediaWiki-Vagrant und PhpStorm) siehe Manual:How to debug/with MediaWiki-Vagrant and PHPStorm .
Zend
Wenn du den Zend PHP-Interpreter verwendest, kannst du deinen Code mit XDebug debuggen. MediaWiki-Vagrant hat dafür in den Einstellungen festgelegt. Wenn du nicht MediaWiki-Vagrant verwendest, dein Setup jedoch ähnlich ist, kannst du diese Werte wiederverwenden. In manchen Fällen (z. B. wegen einer Firewall) musst du die IDE auf demselben Rechner verwenden wie den Webserver. In diesem Fall kannst du einfach festlegen:
xdebug.remote_enable = 1
xdebug.remote_host = 'localhost'
See the XDebug documentation for more information.
To debug a command-line script (e.g. PHPUnit, or a maintenance script) on MediaWiki-Vagrant, use:
xdebug_on; php5 /vagrant/mediawiki/tests/phpunit/phpunit.php --wiki=wiki /vagrant/mediawiki/extensions/Extension/tests/phpunit/SomeTest.php; 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.
Protokollierung
Für viel mehr Details musst du Fehler profilieren und protokollieren.
$wgMWLoggerDefaultSpi
änderst, um zum Beispiel die Rolle psr3
auf einer vagrant -Box zu aktivieren, werden diese Einstellungen wahrscheinlich ignoriert. Siehe in diesen Fall die Dokumentation deines Loggers nach, zum Beispiel Manual:MonologSpi .
Wie man eine Debug-Protokolldatei einrichtet
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
.
Hier ist eine Beispieleinstellung:
/**
* 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";
Diese Datei wird viele Debuginformationen vom MediaWiki-Kern und Erweiterungen enthalten. Some subsystems write to custom logs, see #Creating a custom log file to capture their output.
Database transaction lifecycle debugging can be enabled for some databases with $wgDebugDBTransactions .
Creating a custom log file
MediaWiki Version: | ≤ 1.31 |
Prior to MediaWiki 1.32, to create a custom log file that only holds your specific debug statements, use the wfErrorLog()
function.
This function takes two arguments, the text string to log and the path to the log file:
wfErrorLog( "An error occurred.\n", '/var/log/mediawiki/my-custom-debug.log' );
Creating custom log groups
If you're debugging several different components, it may be useful to direct certain log groups to write to a separate file. Siehe $wgDebugLogGroups für weitere Informationen.
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 = 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',
);
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
Structured logging
MediaWiki Version: | ≥ 1.25 |
Structured logging allows you to include fields in your log records. Siehe Structured logging für weitere Informationen.
JavaScript error logging
MediaWiki Version: | ≥ 1.36 |
See the documentation of the mediawiki.errorLogger ResourceLoader module.
Statistiken
Advanced client-side logging can be performed with Erweiterung: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.
Anwendungsbeispiel:
$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.
Tools:
- ResourceLoader 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
Siehe auch
- 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
- Kategorie:Debug-Variablen
- wikitech:Debugging in production - debugging on Wikimedia's production cluster
- Hilfe:Auffinden defekter Skripte