2014-06-23 22:25:55 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
2015-03-23 00:53:24 +00:00
|
|
|
|
|
|
|
|
namespace MediaWiki\Logger;
|
|
|
|
|
|
|
|
|
|
use MediaWikiTestCase;
|
2014-12-30 01:42:48 +00:00
|
|
|
use Psr\Log\LogLevel;
|
2014-06-23 22:25:55 +00:00
|
|
|
|
2015-03-23 00:53:24 +00:00
|
|
|
class LegacyLoggerTest extends MediaWikiTestCase {
|
2014-06-23 22:25:55 +00:00
|
|
|
|
|
|
|
|
/**
|
2016-02-23 03:18:31 +00:00
|
|
|
* @covers MediaWiki\Logger\LegacyLogger::interpolate
|
2014-06-23 22:25:55 +00:00
|
|
|
* @dataProvider provideInterpolate
|
|
|
|
|
*/
|
|
|
|
|
public function testInterpolate( $message, $context, $expect ) {
|
|
|
|
|
$this->assertEquals(
|
2015-03-23 00:53:24 +00:00
|
|
|
$expect, LegacyLogger::interpolate( $message, $context ) );
|
2014-06-23 22:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function provideInterpolate() {
|
2015-05-24 20:02:13 +00:00
|
|
|
$e = new \Exception( 'boom!' );
|
|
|
|
|
$d = new \DateTime();
|
2016-02-17 09:09:32 +00:00
|
|
|
return [
|
|
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'no-op',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2014-06-23 22:25:55 +00:00
|
|
|
'no-op',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'Hello {world}!',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'world' => 'World',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-06-23 22:25:55 +00:00
|
|
|
'Hello World!',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'{greeting} {user}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'greeting' => 'Goodnight',
|
|
|
|
|
'user' => 'Moon',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-06-23 22:25:55 +00:00
|
|
|
'Goodnight Moon',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'Oops {key_not_set}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[],
|
2014-06-23 22:25:55 +00:00
|
|
|
'Oops {key_not_set}',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'{ not interpolated }',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2014-06-23 22:25:55 +00:00
|
|
|
'not interpolated' => 'This should NOT show up in the message',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2014-06-23 22:25:55 +00:00
|
|
|
'{ not interpolated }',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{null}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'null' => null,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
'[Null]',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{bool}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'bool' => true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
'true',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-07-30 01:26:52 +00:00
|
|
|
'{float}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-07-30 01:26:52 +00:00
|
|
|
'float' => 1.23,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-07-30 01:26:52 +00:00
|
|
|
'1.23',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{array}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
|
|
|
|
'array' => [ 1, 2, 3 ],
|
|
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
'[Array(3)]',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{exception}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'exception' => $e,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
'[Exception ' . get_class( $e ) . '( ' .
|
|
|
|
|
$e->getFile() . ':' . $e->getLine() . ') ' .
|
|
|
|
|
$e->getMessage() . ']',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{datetime}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'datetime' => $d,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
$d->format( 'c' ),
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'{object}',
|
2016-02-17 09:09:32 +00:00
|
|
|
[
|
2015-05-24 20:02:13 +00:00
|
|
|
'object' => new \stdClass,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
2015-05-24 20:02:13 +00:00
|
|
|
'[Object stdClass]',
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-06-23 22:25:55 +00:00
|
|
|
}
|
|
|
|
|
|
2014-12-30 01:42:48 +00:00
|
|
|
/**
|
2016-02-23 03:18:31 +00:00
|
|
|
* @covers MediaWiki\Logger\LegacyLogger::shouldEmit
|
2014-12-30 01:42:48 +00:00
|
|
|
* @dataProvider provideShouldEmit
|
|
|
|
|
*/
|
|
|
|
|
public function testShouldEmit( $level, $config, $expected ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->setMwGlobals( 'wgDebugLogGroups', [ 'fakechannel' => $config ] );
|
2014-12-30 01:42:48 +00:00
|
|
|
$this->assertEquals(
|
|
|
|
|
$expected,
|
2016-02-17 09:09:32 +00:00
|
|
|
LegacyLogger::shouldEmit( 'fakechannel', 'some message', $level, [] )
|
2014-12-30 01:42:48 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static function provideShouldEmit() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$dest = [ 'destination' => 'foobar' ];
|
|
|
|
|
$tests = [
|
|
|
|
|
[
|
2014-12-30 01:42:48 +00:00
|
|
|
LogLevel::DEBUG,
|
|
|
|
|
$dest,
|
|
|
|
|
true
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-12-30 01:42:48 +00:00
|
|
|
LogLevel::WARNING,
|
2016-02-17 09:09:32 +00:00
|
|
|
$dest + [ 'level' => LogLevel::INFO ],
|
2014-12-30 01:42:48 +00:00
|
|
|
true,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
[
|
2014-12-30 01:42:48 +00:00
|
|
|
LogLevel::INFO,
|
2016-02-17 09:09:32 +00:00
|
|
|
$dest + [ 'level' => LogLevel::CRITICAL ],
|
2014-12-30 01:42:48 +00:00
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
],
|
|
|
|
|
];
|
2014-12-30 01:42:48 +00:00
|
|
|
|
|
|
|
|
if ( class_exists( '\Monolog\Logger' ) ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$tests[] = [
|
2014-12-30 01:42:48 +00:00
|
|
|
\Monolog\Logger::INFO,
|
2016-02-17 09:09:32 +00:00
|
|
|
$dest + [ 'level' => LogLevel::INFO ],
|
2014-12-30 01:42:48 +00:00
|
|
|
true,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
|
|
|
|
$tests[] = [
|
2014-12-30 01:42:48 +00:00
|
|
|
\Monolog\Logger::WARNING,
|
2016-02-17 09:09:32 +00:00
|
|
|
$dest + [ 'level' => LogLevel::EMERGENCY ],
|
2014-12-30 01:42:48 +00:00
|
|
|
false,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2014-12-30 01:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $tests;
|
|
|
|
|
}
|
|
|
|
|
|
2014-06-23 22:25:55 +00:00
|
|
|
}
|