<commit_before><?php
namespace js\tools\commons\logging;

use js\tools\commons\exceptions\LogException;

abstract class Logger
{
	/**
	 * @param int $level One of the {@link LogLevel} constants.
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see debug()
	 * @see notice()
	 * @see info()
	 * @see warning()
	 * @see error()
	 * @see critical()
	 * @see fatal()
	 */
	public final function log(int $level, string $message, ...$parameters): void
	{
		$message = $this->prepareMessage($message, ...$parameters);
		$message = $this->formatMessage($message, $level);
		
		$this->write($message, $level);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function debug(string $message, ...$parameters): void
	{
		$this->log(LogLevel::DEBUG, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function notice(string $message, ...$parameters): void
	{
		$this->log(LogLevel::NOTICE, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function info(string $message, ...$parameters): void
	{
		$this->log(LogLevel::INFO, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function warning(string $message, ...$parameters): void
	{
		$this->log(LogLevel::WARNING, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function error(string $message, ...$parameters): void
	{
		$this->log(LogLevel::ERROR, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function critical(string $message, ...$parameters): void
	{
		$this->log(LogLevel::CRITICAL, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function fatal(string $message, ...$parameters): void
	{
		$this->log(LogLevel::FATAL, $message, ...$parameters);
	}
	
	protected function prepareMessage(string $message, ...$parameters): string
	{
		return sprintf(trim($message), ...$parameters);
	}
	
	/**
	 * @param string $message The source message received from the logging methods.
	 * @param int $level One of the {@link LogLevel} constants.
	 * @return string The formatted message.
	 * @throws LogException If the log level is invalid.
	 * @noinspection PhpDocRedundantThrowsInspection
	 * @noinspection PhpUnusedParameterInspection
	 */
	protected function formatMessage(string $message, int $level): string
	{
		return $message;
	}
	
	/**
	 * @param string $message The formatted message received from {@link formatMessage()}.
	 * @param int $level One of the {@link LogLevel} constants.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 */
	protected abstract function write(string $message, int $level): void;
}<commit_msg>Moving the "Invalid log level" exception from `format()`/`write()` to `log()`. Not a very graceful solution, but that's what we got to work with. `LogLevel::getName()` already throws the exception on an invalid parameter, so there is no additional code necessary here.<commit_after><?php
namespace js\tools\commons\logging;

use js\tools\commons\exceptions\LogException;

abstract class Logger
{
	/**
	 * @param int $level One of the {@link LogLevel} constants.
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see debug()
	 * @see notice()
	 * @see info()
	 * @see warning()
	 * @see error()
	 * @see critical()
	 * @see fatal()
	 */
	public final function log(int $level, string $message, ...$parameters): void
	{
		LogLevel::getName($level); // Fail-fast in case of invalid $level, ensuring it won't happen later.
		$message = $this->prepareMessage($message, ...$parameters);
		$message = $this->formatMessage($message, $level);
		
		$this->write($message, $level);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function debug(string $message, ...$parameters): void
	{
		$this->log(LogLevel::DEBUG, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function notice(string $message, ...$parameters): void
	{
		$this->log(LogLevel::NOTICE, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function info(string $message, ...$parameters): void
	{
		$this->log(LogLevel::INFO, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function warning(string $message, ...$parameters): void
	{
		$this->log(LogLevel::WARNING, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function error(string $message, ...$parameters): void
	{
		$this->log(LogLevel::ERROR, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function critical(string $message, ...$parameters): void
	{
		$this->log(LogLevel::CRITICAL, $message, ...$parameters);
	}
	
	/**
	 * @param string $message The message to log; may contain parameter placeholders in sprintf()-accepted format.
	 * @param string[] $parameters The message parameters.
	 * @throws LogException If the log level is invalid or message failed to be written.
	 * @see log
	 */
	public final function fatal(string $message, ...$parameters): void
	{
		$this->log(LogLevel::FATAL, $message, ...$parameters);
	}
	
	protected function prepareMessage(string $message, ...$parameters): string
	{
		return sprintf(trim($message), ...$parameters);
	}
	
	/**
	 * @param string $message The source message received from the logging methods.
	 * @param int $level One of the {@link LogLevel} constants.
	 * @return string The formatted message.
	 * @noinspection PhpUnusedParameterInspection
	 */
	protected function formatMessage(string $message, int $level): string
	{
		return $message;
	}
	
	/**
	 * @param string $message The formatted message received from {@link formatMessage()}.
	 * @param int $level One of the {@link LogLevel} constants.
	 * @throws LogException If the message failed to be written.
	 */
	protected abstract function write(string $message, int $level): void;
}
{'fraction_non_alphanumeric': 0.12224342663273961, 'fraction_numerical': 0.0, 'mean_word_length': 3.9725882973115447, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2189440', 'n_tokens_mistral': 2684, 'n_tokens_neox': 2507, 'n_words': 1036}