<commit_before><?php

require_once 'Swat/SwatErrorLogger.php';

/**
 * An error logger that creates HTML files containing error details and puts a
 * link in the system error log to the details file
 *
 * @package   Site
 * @copyright 2006-2009 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteErrorLogger extends SwatErrorLogger
{
	// {{{ protected properties

	/**
	 * Location in which to store detailed error logs
	 *
	 * This path should include a trailing slash.
	 *
	 * @var string
	 */
	protected $log_location;

	/**
	 * Base URI to use to construct a link to the log file
	 *
	 * If null, the log file name is used instead.
	 *
	 * @var string
	 */
	protected $base_uri;

	/**
	 * Unix group to use when creating new dirs
	 *
	 * If null, the current group is used.
	 *
	 * @var string
	 */
	protected $unix_group;

	/**
	 * Error identifier
	 *
	 * Unique id used for filenames and paths. Generated once for each error.
	 *
	 * @var string
	 */
	protected $id;

	/**
	 * Directory in which logged errors will be stored
	 *
	 * Not necessarily unique. Generated once for each error.
	 *
	 * @var string
	 */
	protected $directory;

	// }}}
	// {{{ public function __construct()

	/**
	 * Creates a new error logger
	 *
	 * @param string $log_location the location in which to store detailed
	 *                              error log files.
	 * @param string $base_uri optional.
	 * @param integer $unix_group optional.
	 */
	public function __construct($log_location, $base_uri = null,
		$unix_group = null)
	{
		$this->log_location = $log_location;
		$this->base_uri     = $base_uri;
		$this->unix_group   = $unix_group;
		$this->id           = md5(uniqid());
		$this->directory    = date('Y-m-d');
	}

	// }}}
	// {{{ public function log()

	/**
	 * Logs an error
	 */
	public function log(SwatError $e)
	{
		$log_path     = $this->getLogPath();
		$log_filepath = $this->getLogFilePath();

		// create path if it does not exist
		if (!file_exists($log_path)) {
			mkdir($log_path, 0770, true);
			chmod($log_path, 0770);

			if ($this->unix_group !== null)
				chgrp($log_path, $this->unix_group);
		}

		// save detailed error log
		if (($log_file = fopen($log_filepath, 'w')) !== false) {
			fwrite($log_file, $this->getBody($e));
			fclose($log_file);

			if ($this->unix_group !== null)
				chgrp($log_filepath, $this->unix_group);
		}

		// add to syslog
		$this->logSummary($this->getSummary($e));
	}

	// }}}
	// {{{ protected function logSummary()

	protected function logSummary($summary)
	{
		error_log($summary, 0);
	}<commit_msg>Filter useless GC warning messages from the log. This is similar to how we filter 404 exceptions in the exception logger.<commit_after><?php

require_once 'Swat/SwatErrorLogger.php';

/**
 * An error logger that creates HTML files containing error details and puts a
 * link in the system error log to the details file
 *
 * @package   Site
 * @copyright 2006-2009 silverorange
 * @license   http://www.gnu.org/copyleft/lesser.html LGPL License 2.1
 */
class SiteErrorLogger extends SwatErrorLogger
{
	// {{{ protected properties

	/**
	 * Location in which to store detailed error logs
	 *
	 * This path should include a trailing slash.
	 *
	 * @var string
	 */
	protected $log_location;

	/**
	 * Base URI to use to construct a link to the log file
	 *
	 * If null, the log file name is used instead.
	 *
	 * @var string
	 */
	protected $base_uri;

	/**
	 * Unix group to use when creating new dirs
	 *
	 * If null, the current group is used.
	 *
	 * @var string
	 */
	protected $unix_group;

	/**
	 * Error identifier
	 *
	 * Unique id used for filenames and paths. Generated once for each error.
	 *
	 * @var string
	 */
	protected $id;

	/**
	 * Directory in which logged errors will be stored
	 *
	 * Not necessarily unique. Generated once for each error.
	 *
	 * @var string
	 */
	protected $directory;

	// }}}
	// {{{ public function __construct()

	/**
	 * Creates a new error logger
	 *
	 * @param string $log_location the location in which to store detailed
	 *                              error log files.
	 * @param string $base_uri optional.
	 * @param integer $unix_group optional.
	 */
	public function __construct($log_location, $base_uri = null,
		$unix_group = null)
	{
		$this->log_location = $log_location;
		$this->base_uri     = $base_uri;
		$this->unix_group   = $unix_group;
		$this->id           = md5(uniqid());
		$this->directory    = date('Y-m-d');
	}

	// }}}
	// {{{ public function log()

	/**
	 * Logs an error
	 */
	public function log(SwatError $e)
	{
		if ($this->filter($e))
		$log_path     = $this->getLogPath();
		$log_filepath = $this->getLogFilePath();

		// create path if it does not exist
		if (!file_exists($log_path)) {
			mkdir($log_path, 0770, true);
			chmod($log_path, 0770);

			if ($this->unix_group !== null)
				chgrp($log_path, $this->unix_group);
		}

		// save detailed error log
		if (($log_file = fopen($log_filepath, 'w')) !== false) {
			fwrite($log_file, $this->getBody($e));
			fclose($log_file);

			if ($this->unix_group !== null)
				chgrp($log_filepath, $this->unix_group);
		}

		// add to syslog
		$this->logSummary($this->getSummary($e));
	}

	// }}}
	// {{{ protected function filter()

	protected function filter(SwatError $e)
	{
		if ($e->severity === E_WARNING) {
			$pattern = '/GC cache entry .* was on gc-list for .* seconds/';
			if (preg_match($pattern, $e->message))
				return true;
		}

		return false;
	}

	// }}}
	// {{{ protected function logSummary()

	protected function logSummary($summary)
	{
		error_log($summary, 0);
	}
{'fraction_non_alphanumeric': 0.13784505788067675, 'fraction_numerical': 0.007658058771148709, 'mean_word_length': 2.9245283018867925, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4788913', 'n_tokens_mistral': 2042, 'n_tokens_neox': 1800, 'n_words': 550}