Skip to content

Commit

Permalink
Merge pull request #65 from jenkoian/qm-handler
Browse files Browse the repository at this point in the history
Add Query Monitor handler.
  • Loading branch information
gmazzap authored Mar 1, 2023
2 parents d9ee8c2 + 4538455 commit 0616eda
Show file tree
Hide file tree
Showing 2 changed files with 181 additions and 0 deletions.
68 changes: 68 additions & 0 deletions src/Handler/QueryMonitorHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the Wonolog package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Inpsyde\Wonolog\Handler;

use Monolog\Handler\AbstractProcessingHandler;
use Psr\Log\LogLevel;

/**
* Log to Query Monitor using the QM logging functionality.
*
* @package wonolog
* @license http://opensource.org/licenses/MIT MIT
*/
class QueryMonitorHandler extends AbstractProcessingHandler {
const LEVEL_MAP = [
LogLevel::DEBUG => 'qm/debug',
LogLevel::INFO => 'qm/info',
LogLevel::NOTICE => 'qm/notice',
LogLevel::WARNING => 'qm/warning',
LogLevel::ERROR => 'qm/error',
LogLevel::CRITICAL => 'qm/critical',
LogLevel::ALERT => 'qm/alert',
LogLevel::EMERGENCY => 'qm/emergency',
];

/**
* Write the log to the Query Monitor log.
*
* @param array $record The record to write.
*
* @return void
*/
protected function write( array $record ) {
if ( empty( $record['message'] ) || ! is_string( $record['message'] ) ) {
return;
}

if ( ! isset( $record['level_name'] ) || ! is_string( $record['level_name'] ) || ! array_key_exists( strtolower( $record['level_name'] ), self::LEVEL_MAP ) ) {
$record['level_name'] = LogLevel::DEBUG;
}

if ( ! isset( $record['context'] ) || ! is_array( $record['context'] ) ) {
$record['context'] = [];
}

$qm_level = $this->map_level( $record['level_name'] );
do_action( $qm_level, $record['message'], $record['context'] );
}

/**
* Map the log level to the Query Monitor log level.
*
* @param string $level_name Level name to map.
*
* @return string
*/
private function map_level( $level_name ) {
return array_key_exists( strtolower( $level_name ), self::LEVEL_MAP ) ? self::LEVEL_MAP[ strtolower( $level_name ) ] : 'qm/debug';
}
}
113 changes: 113 additions & 0 deletions tests/src/Unit/Handler/QueryMonitorHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php # -*- coding: utf-8 -*-
/*
* This file is part of the Wonolog package.
*
* (c) Inpsyde GmbH
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Inpsyde\Wonolog\Tests\Unit\Handler;

use Inpsyde\Wonolog\Handler\QueryMonitorHandler;
use Inpsyde\Wonolog\Tests\TestCase;
use Monolog\Logger;

use function Brain\Monkey\Actions\expectDone;

/**
* @package wonolog\tests
* @license http://opensource.org/licenses/MIT MIT
*/
class QueryMonitorHandlerTest extends TestCase {
/**
* @dataProvider get_levels
*/
public function test_the_handler_raises_the_appropriate_action( $log_level, $expected_qm_action ) {
$handler = new QueryMonitorHandler();
$handler->handle( $this->get_record( $log_level ) );

self::assertTrue( (bool) did_action( $expected_qm_action ) );
}

public function test_the_handler_adds_the_message_to_the_log() {
expectDone( 'qm/debug' )
->once()
->with( 'This is a test', [ 'foo' => 'bar' ] );

$handler = new QueryMonitorHandler();
$handler->handle( $this->get_record( Logger::DEBUG, 'This is a test', [ 'foo' => 'bar' ] ) );
}

public function test_the_handler_does_not_log_if_message_empty() {
$handler = new QueryMonitorHandler();
$handler->handle( $this->get_record( Logger::DEBUG, '', [ 'foo' => 'bar' ] ) );

self::assertFalse( (bool) did_action( 'qm/debug' ) );
}

public function test_the_handler_defaults_to_debug_if_no_level_name_supplied_in_record() {
$record = $this->get_record( Logger::DEBUG, 'Test', [ 'foo' => 'bar' ] );
$handler = new QueryMonitorHandler();

// Remove level details.
unset( $record['level_name'] );
$handler->handle( $record );

self::assertTrue( (bool) did_action( 'qm/debug' ) );
}

public function test_the_handler_defaults_to_debug_if_level_name_is_null() {
$record = $this->get_record( Logger::DEBUG, 'Test', [ 'foo' => 'bar' ] );
$handler = new QueryMonitorHandler();

// Set level to null.
$record['level_name'] = null;
$handler->handle( $record );

self::assertTrue( (bool) did_action( 'qm/debug' ) );
}

public function test_the_handler_defaults_to_debug_if_level_name_is_invalid() {
$record = $this->get_record( Logger::DEBUG, 'Test', [ 'foo' => 'bar' ] );
$handler = new QueryMonitorHandler();

// Set level to invalid value.
$record['level_name'] = 'invalid';
$handler->handle( $record );

self::assertTrue( (bool) did_action( 'qm/debug' ) );
}

public function get_levels() {
return [
'Debug level' => [ Logger::DEBUG, 'qm/debug' ],
'Info level' => [ Logger::INFO, 'qm/info' ],
'Notice level' => [ Logger::NOTICE, 'qm/notice' ],
'Warning level' => [ Logger::WARNING, 'qm/warning' ],
'Error level' => [ Logger::ERROR, 'qm/error' ],
'Critical level' => [ Logger::CRITICAL, 'qm/critical' ],
'Alert level' => [ Logger::ALERT, 'qm/alert' ],
'Emergency level' => [ Logger::EMERGENCY, 'qm/emergency' ],
];
}

/**
* @param int $level Level to log.
* @param string $message Message to log.
* @param array $context Context to log.
*
* @return array
*/
protected function get_record( $level = Logger::WARNING, $message = 'test', $context = [] ) {
return [
'message' => (string) $message,
'context' => $context,
'level' => $level,
'level_name' => Logger::getLevelName( $level ),
'channel' => 'test',
'extra' => [],
];
}
}

0 comments on commit 0616eda

Please sign in to comment.