summaryrefslogtreecommitdiff
blob: 91d1b294a4a0c0af7f0284f6e397856cc5b2c394 (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
67
68
69
70
71
72
73
74
75
<?php
/**
 * Contains a generic job class
 *
 * @file
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 */

namespace MediaWiki\Extension\Translate\Jobs;

use Job;
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(): string {
		$prefix = $this->getType();
		if ( isset( $this->title ) ) {
			$prefix .= ' [' . $this->title->getPrefixedText() . ']';
		}

		// Add a separator at the end.
		$prefix .= ': ';
		return $prefix;
	}

	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 );
	}
}

class_alias( GenericTranslateJob::class, '\MediaWiki\Extensions\Translate\GenericTranslateJob' );