summaryrefslogtreecommitdiff
blob: a74677a7f28d7e3a9c87a8f5093c8a873fc21463 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
 * Contains a generic job class
 *
 * @file
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 */

namespace MediaWiki\Extensions\Translate\Jobs;

use MediaWiki\Logger\LoggerFactory;
use Psr\Log\LoggerInterface;

/**
 * Generic Job class extended by other jobs. Provides logging functionality.
 * @since 2019.08
 */
abstract class GenericTranslateJob extends \Job {
	/**
	 * A logger instance
	 * @var LoggerInterface
	 */
	protected $logger;

	/**
	 * Channel name to be used during logging
	 * @var string
	 */
	private const CHANNEL_NAME = 'Translate.Jobs';

	/**
	 * Returns a logger instance with the channel name. Can have only a single
	 * channel per job, so once instantiated, the same instance is returned.
	 * @return LoggerInterface
	 */
	protected function getLogger() {
		if ( $this->logger ) {
			return $this->logger;
		}

		$this->logger = LoggerFactory::getInstance( self::CHANNEL_NAME );
		return $this->logger;
	}

	protected function getLogPrefix() {
		return '[Job: ' . $this->getType() . '][Request ID: ' . $this->getRequestId() .
			'][Title: ' . $this->title->getPrefixedText() . '] ';
	}

	protected function logInfo( $msg, $context = [] ) {
		$this->getLogger()->info( $this->getLogPrefix() . $msg, $context );
	}

	protected function logDebug( $msg, $context = [] ) {
		$this->getLogger()->debug( $this->getLogPrefix() . $msg, $context );
	}

	protected function logError( $msg, $context = [] ) {
		$this->getLogger()->error( $this->getLogPrefix() . $msg, $context );
	}

	protected function logWarning( $msg, $context = [] ) {
		$this->getLogger()->warning( $this->getLogPrefix() . $msg, $context );
	}
}