Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 10 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
GenericTranslationNotificationsJob | |
0.00% |
0 / 10 |
|
0.00% |
0 / 6 |
56 | |
0.00% |
0 / 1 |
run | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
getLogger | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
getLogPrefix | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
logInfo | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
logDebug | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
logError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
logWarn | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Contains a generic job class |
4 | * |
5 | * @file |
6 | * @author Abijeet Patro |
7 | * @license GPL-2.0-or-later |
8 | */ |
9 | |
10 | namespace MediaWiki\Extension\TranslationNotifications\Jobs; |
11 | |
12 | use Job; |
13 | use MediaWiki\Logger\LoggerFactory; |
14 | use Psr\Log\LoggerInterface; |
15 | |
16 | /** |
17 | * Generic Job class extended by other jobs. Provides logging functionality. |
18 | * @since 2019.09 |
19 | */ |
20 | abstract class GenericTranslationNotificationsJob extends Job { |
21 | /** |
22 | * A logger instance |
23 | * @var LoggerInterface |
24 | */ |
25 | protected $logger; |
26 | |
27 | /** |
28 | * Channel name to be used during logging |
29 | * @var string |
30 | */ |
31 | private const CHANNEL_NAME = 'TranslationNotifications.Jobs'; |
32 | |
33 | abstract public function run(); |
34 | |
35 | /** |
36 | * Returns a logger instance with the channel name. Can have only a single |
37 | * channel per job, so once instantiated, the same instance is returned. |
38 | * @return LoggerInterface |
39 | */ |
40 | protected function getLogger() { |
41 | if ( $this->logger ) { |
42 | return $this->logger; |
43 | } |
44 | |
45 | $this->logger = LoggerFactory::getInstance( self::CHANNEL_NAME ); |
46 | return $this->logger; |
47 | } |
48 | |
49 | protected function getLogPrefix(): string { |
50 | return '[Job: ' . $this->getType() . '][Request ID: ' . $this->getRequestId() . |
51 | '][Title: ' . $this->title->getPrefixedText() . '] '; |
52 | } |
53 | |
54 | protected function logInfo( string $msg, array $context = [] ): void { |
55 | $this->getLogger()->info( $this->getLogPrefix() . $msg, $context ); |
56 | } |
57 | |
58 | protected function logDebug( string $msg, array $context = [] ): void { |
59 | $this->getLogger()->debug( $this->getLogPrefix() . $msg, $context ); |
60 | } |
61 | |
62 | protected function logError( string $msg, array $context = [] ): void { |
63 | $this->getLogger()->error( $this->getLogPrefix() . $msg, $context ); |
64 | } |
65 | |
66 | protected function logWarn( string $msg, array $context = [] ): void { |
67 | $this->getLogger()->warning( $this->getLogPrefix() . $msg, $context ); |
68 | } |
69 | } |