Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
GenericTranslateJob | |
0.00% |
0 / 17 |
|
0.00% |
0 / 7 |
72 | |
0.00% |
0 / 1 |
getLogger | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
formatLogEntry | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
logDebug | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
logInfo | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
logNotice | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
logWarning | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
logError | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\Jobs; |
5 | |
6 | use Job; |
7 | use MediaWiki\Extension\Translate\LogNames; |
8 | use MediaWiki\Logger\LoggerFactory; |
9 | use Psr\Log\LoggerInterface; |
10 | |
11 | /** |
12 | * Generic Job class extended by other jobs. Provides logging functionality. |
13 | * @author Abijeet Patro |
14 | * @license GPL-2.0-or-later |
15 | * @since 2019.08 |
16 | */ |
17 | abstract class GenericTranslateJob extends Job { |
18 | private LoggerInterface $logger; |
19 | |
20 | /** |
21 | * Returns a logger instance with the channel name. Can have only a single |
22 | * channel per job, so once instantiated, the same instance is returned. |
23 | */ |
24 | private function getLogger(): LoggerInterface { |
25 | $this->logger ??= LoggerFactory::getInstance( LogNames::JOBS ); |
26 | return $this->logger; |
27 | } |
28 | |
29 | /** @phan-return array{0:string,1:array} */ |
30 | private function formatLogEntry( string $msg, array $context = [] ): array { |
31 | $prefix = $this->getType(); |
32 | if ( isset( $this->title ) ) { |
33 | $prefix .= ' [{job_title}]'; |
34 | $context['job_title'] = $this->title->getPrefixedText(); |
35 | } |
36 | |
37 | return [ "$prefix: $msg", $context ]; |
38 | } |
39 | |
40 | protected function logDebug( string $msg, array $context = [] ): void { |
41 | [ $msg, $context ] = $this->formatLogEntry( $msg, $context ); |
42 | $this->getLogger()->debug( $msg, $context ); |
43 | } |
44 | |
45 | protected function logInfo( string $msg, array $context = [] ): void { |
46 | [ $msg, $context ] = $this->formatLogEntry( $msg, $context ); |
47 | $this->getLogger()->info( $msg, $context ); |
48 | } |
49 | |
50 | protected function logNotice( string $msg, array $context = [] ): void { |
51 | [ $msg, $context ] = $this->formatLogEntry( $msg, $context ); |
52 | $this->getLogger()->notice( $msg, $context ); |
53 | } |
54 | |
55 | protected function logWarning( string $msg, array $context = [] ): void { |
56 | [ $msg, $context ] = $this->formatLogEntry( $msg, $context ); |
57 | $this->getLogger()->warning( $msg, $context ); |
58 | } |
59 | |
60 | protected function logError( string $msg, array $context = [] ): void { |
61 | [ $msg, $context ] = $this->formatLogEntry( $msg, $context ); |
62 | $this->getLogger()->error( $msg, $context ); |
63 | } |
64 | } |