Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
EventBusMonologHandler | |
0.00% |
0 / 11 |
|
0.00% |
0 / 2 |
6 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
write | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\EventBus\Adapters\Monolog; |
4 | |
5 | use MediaWiki\Deferred\DeferredUpdates; |
6 | use MediaWiki\Extension\EventBus\EventBus; |
7 | use Monolog\Handler\AbstractProcessingHandler; |
8 | use Psr\Log\LogLevel; |
9 | |
10 | /** |
11 | * Log handler that supports sending messages to Kafka over |
12 | * EventBus and EventGate service. |
13 | * |
14 | * @file |
15 | * @since 1.33 |
16 | * @copyright © 2019 Wikimedia Foundation and contributors |
17 | * @author Petr Pchelko <ppchelko@wikimedia.org> |
18 | */ |
19 | class EventBusMonologHandler extends AbstractProcessingHandler { |
20 | |
21 | /** |
22 | * The instance of EventBus to use for logging |
23 | * @var EventBus |
24 | */ |
25 | private $eventBus; |
26 | |
27 | /** |
28 | * EventBusHandler constructor. |
29 | * |
30 | * @param string $eventServiceName the name of the event service to use |
31 | * @param int|string $level The minimum logging level at which this handler will be triggered |
32 | * @param bool $bubble Whether the messages that are handled can bubble up the stack or not |
33 | */ |
34 | public function __construct( $eventServiceName, $level = LogLevel::DEBUG, $bubble = true ) { |
35 | parent::__construct( $level, $bubble ); |
36 | |
37 | $this->eventBus = EventBus::getInstance( $eventServiceName ); |
38 | } |
39 | |
40 | /** |
41 | * Assumes that $record['context'] contains the event to send via EventBus. |
42 | * |
43 | * @param array $record |
44 | * @return void |
45 | */ |
46 | protected function write( array $record ): void { |
47 | // Use the log record context as formatted as the event data. |
48 | $event = $record['context']; |
49 | |
50 | // wfDebugLog() adds a field called 'private' to the context |
51 | // that does not belong in the event. Delete the 'private' field here and |
52 | // then let EventBus serialize the log context to JSON string and send it. |
53 | // NOTE: we could create a custom formatter for EventBus, but all |
54 | // it would do is exactly this. |
55 | unset( $event['private'] ); |
56 | |
57 | DeferredUpdates::addCallableUpdate( |
58 | function () use ( $event ) { |
59 | // Events via Monolog might have binary strings in them. |
60 | // We need to be sure that any binary data is first encoded. |
61 | // |
62 | EventBus::replaceBinaryValuesRecursive( $event ); |
63 | $this->eventBus->send( [ $event ] ); |
64 | } |
65 | ); |
66 | } |
67 | } |