<commit_before><?php
/**
 * ownCloud - galleryplus
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @copyright <NAME> 2015
 * @copyright <NAME> 2013-2015
 * @copyright <NAME> 2014-2015
 */

namespace OCA\GalleryPlus\Utility;

use OCP\ILogger;

/**
 * Lets us call the main logger without having to add the context at every
 * request
 *
 * @package OCA\GalleryPlus\Middleware
 */
class SmarterLogger implements ILogger {

	/**
	 * @type ILogger
	 */
	private $logger;

	/***
	 * Constructor
	 *
	 * @param string $appName
	 * @param ILogger $logger
	 */
	public function __construct(
		$appName,
		ILogger $logger
	) {
		$this->appName = $appName;
		$this->logger = $logger;
	}

	/**
	 * System is unusable.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function emergency($message, array $context = array()) {
		$this->log(\OCP\Util::FATAL, $message, $context);
	}

	/**
	 * Action must be taken immediately.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function alert($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Critical conditions.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function critical($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Runtime errors that do not require immediate action but should typically
	 * be logged and monitored.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function error($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Exceptional occurrences that are not errors.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function warning($message, array $context = array()) {
		$this->log(\OCP\Util::WARN, $message, $context);
	}

	/**
	 * Normal but significant events.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function notice($message, array $context = array()) {
		$this->log(\OCP\Util::INFO, $message, $context);
	}

	/**
	 * Interesting events.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function info($message, array $context = array()) {
		$this->log(\OCP\Util::INFO, $message, $context);
	}

	/**
	 * Detailed debug information.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function debug($message, array $context = array()) {
		$this->log(\OCP\Util::DEBUG, $message, $context);
	}

	/**
	 * Normalises a message and logs it with an arbitrary level.
	 *
	 * @param mixed $level
	 * @param string $message
	 * @param array $context
	 *
	 * @return mixed
	 */
	public function log($level, $message, array $context = array()) {
		// interpolate $message as defined in PSR-3
		$replace = array();
		foreach ($context as $key => $val) {
			// Allows us to dump arrays, objects and exceptions to the log
			$val = $this->normalize($val);
			$replace['{' . $key . '}'] = $val;
		}

		// interpolate replacement values into the message and return
		$message = strtr($message, $replace);

		$this->logger->log(
			$level, $message,
			array(
				'app' => $this->appName
			)
		);

	}

	/**
	 * Converts Objects, Arrays and Exceptions to String
	 *
	 * @param $data
	 *
	 * @return string
	 */
	private function normalize($data) {
		if (null === $data || is_scalar($data)) {
			return $data;
		}

		if (is_array($data) || $data instanceof \Traversable) {
			return $this->normalizeTraversable($data);
		}

		if (is_object($data)) {
			if ($data instanceof \Exception) {
				return $this->normalizeException($data);
			}
<commit_msg>Test to reduce complexity...<commit_after><?php
/**
 * ownCloud - galleryplus
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @copyright <NAME> 2015
 * @copyright <NAME> 2013-2015
 * @copyright <NAME> 2014-2015
 */

namespace OCA\GalleryPlus\Utility;

use OCP\ILogger;

/**
 * Lets us call the main logger without having to add the context at every
 * request
 *
 * @package OCA\GalleryPlus\Middleware
 */
class SmarterLogger implements ILogger {

	/**
	 * @type ILogger
	 */
	private $logger;

	/***
	 * Constructor
	 *
	 * @param string $appName
	 * @param ILogger $logger
	 */
	public function __construct(
		$appName,
		ILogger $logger
	) {
		$this->appName = $appName;
		$this->logger = $logger;
	}

	/**
	 * System is unusable.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function emergency($message, array $context = array()) {
		$this->log(\OCP\Util::FATAL, $message, $context);
	}

	/**
	 * Action must be taken immediately.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function alert($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Critical conditions.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function critical($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Runtime errors that do not require immediate action but should typically
	 * be logged and monitored.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function error($message, array $context = array()) {
		$this->log(\OCP\Util::ERROR, $message, $context);
	}

	/**
	 * Exceptional occurrences that are not errors.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function warning($message, array $context = array()) {
		$this->log(\OCP\Util::WARN, $message, $context);
	}

	/**
	 * Normal but significant events.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function notice($message, array $context = array()) {
		$this->log(\OCP\Util::INFO, $message, $context);
	}

	/**
	 * Interesting events.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function info($message, array $context = array()) {
		$this->log(\OCP\Util::INFO, $message, $context);
	}

	/**
	 * Detailed debug information.
	 *
	 * @param string $message
	 * @param array $context
	 *
	 * @return null
	 */
	public function debug($message, array $context = array()) {
		$this->log(\OCP\Util::DEBUG, $message, $context);
	}

	/**
	 * Normalises a message and logs it with an arbitrary level.
	 *
	 * @param mixed $level
	 * @param string $message
	 * @param array $context
	 *
	 * @return mixed
	 */
	public function log($level, $message, array $context = array()) {
		// interpolate $message as defined in PSR-3
		$replace = array();
		foreach ($context as $key => $val) {
			// Allows us to dump arrays, objects and exceptions to the log
			$val = $this->normalize($val);
			$replace['{' . $key . '}'] = $val;
		}

		// interpolate replacement values into the message and return
		$message = strtr($message, $replace);

		$this->logger->log(
			$level, $message,
			array(
				'app' => $this->appName
			)
		);

	}

	/**
	 * Converts Objects, Arrays and Exceptions to String
	 *
	 * @param $data
	 *
	 * @return string
	 */
	private function normalize($data) {
		if (null === $data || is_scalar($data)) {
			return $data;
		}

		if (is_array($data) || $data instanceof \Traversable) {
			return $this->normalizeTraversable($data);
		}

		if (is_object($data) && $data instanceof \Exception) {
			return $this->normalizeException($data);
		}

		if (is_object($data)) {
{'fraction_non_alphanumeric': 0.15818639798488665, 'fraction_numerical': 0.0055415617128463475, 'mean_word_length': 3.175078864353312, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 63, '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': '3188791', 'n_tokens_mistral': 2828, 'n_tokens_neox': 2489, 'n_words': 744}