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