Post by straightlight » Thu Oct 08, 2015 8:10 am

I would like to address a customized and more handled file size limitation log constructor compared to the original one as a possible consideration on future releases:

Code: Select all

<?php
class Log {
	protected $handle;
	protected $registry;

	public function __construct($filename, $registry) {
		$this->handle = fopen(DIR_LOGS . $filename, 'a');
		
		$this->registry = $registry;
	}
	
	public function __get($name) {
		return $this->registry->get($name);
	}
	
	public function write($message) {
		$file_size = ($this->handle ? @filesize(DIR_LOGS . $filename) : false);
		
		$file_size = $this->formatFileSize($file_size);
		
		if ((int)$file_size < (int)$this->config->get('config_log_filesize_limit')) {
			fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($message, true) . "\n");
			
		} else {
			$this->handle = fopen(DIR_LOGS . 'fullsize_reached.log', 'a');
			
			fwrite($this->handle, date('Y-m-d G:i:s') . ' - ' . print_r($this->language->get('text_fullsize_reached'), true) . "\n");
		}
	}
	
	public function formatFileSize($file_size = 0) {
		if (!$file_size) {
			return $this->language->get('text_not_available'); // n/a
		}
		
		if (strlen($file_size) <= 9 && strlen($file_size) >= 7) {
			$file_size = number_format($file_size / 1048576, 1);
			
			return preg_replace('~{text_filesize_mb}~', $file_size, $this->language->get('text_filesize_mb')); // $file_size."&nbsp;MB"
		
		} elseif (strlen($file_size) >= 10) {
			$file_size = number_format($file_size / 1073741824, 1);
			
			return preg_replace('~{text_filesize_gb}~', $file_size, $this->language->get('text_filesize_gb')); // $file_size."&nbsp;GB"
	  
		} else {
			$file_size = number_format($file_size / 1024, 1);
			
			return preg_replace('~{text_filesize_kb}~', $file_size, $this->language->get('text_filesize_kb')); // $file_size."&nbsp;KB"
		}
	}

	public function __destruct() {
		fclose($this->handle);
	}
}
As a basic concept, this would actually obtain the file size results from the admin settings. If the size exceeds, it would write an error on a second log file to indicate that the original log file is full. Additionally, a validation could be added with a time-to-live for the exceed file or to the original $filename rather than setting the file size in the admin settings but I will leave these codes as it is.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by qahar » Fri Oct 09, 2015 3:25 am

Since once the site is live, it's rare to check error log, another usefull feature is site administrator get mail if the log file get first 10 line. To ensure they know something is wrong and remind them to keep error log clean once the issue done.

Well, this would be simple usefull as an extensions ;D

User avatar
Expert Member

Posts

Joined
Tue Jun 29, 2010 10:24 pm
Location - Indonesia

Post by straightlight » Fri Oct 09, 2015 7:25 am

qahar wrote:Since once the site is live, it's rare to check error log, another usefull feature is site administrator get mail if the log file get first 10 line. To ensure they know something is wrong and remind them to keep error log clean once the issue done.

Well, this would be simple usefull as an extensions ;D
I did thought of the email notification. Although, the reason why it wasn't suggested on my previous reply is because Opencart can implode many email addresses as each of administrators would be notified about in case the first administrator would not be around. This methodology could lead to a mass use of email messages to be sent for a simple modulo validation.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON
Who is online

Users browsing this forum: No registered users and 56 guests