[TomatoCart] Log errors in a separate date file

For security, you should disable the PHP display_error option on your live store. Otherwise, when an error occurs, the sensitive data will be shown directly on the page which can then be used by hackers.

Though what happens if you want to log any and all errors your store is throwing out enabling you to troubleshoot issues? Well, in TomatoCart, we have a logs folder by default so we could use this as an alternative. And in this article I will teach you just that, how to record any errors into this folder which is not accessible to the public.

Step 1. Find includes > application_top.php and admin > includes > application_top.php to search following code:

// set the level of error reporting to E_ALL except E_NOTICE
  if (version_compare(PHP_VERSION, '5.3.0') >= 0) {
    error_reporting(E_ALL & ~E_STRICT & ~E_NOTICE & ~E_DEPRECATED);
  } else {
    error_reporting(E_ALL & ~E_NOTICE);

Add following code snippet after it:

function error_handler($errno, $errstr, $errfile, $errline) {
  	switch ($errno) {
  		case E_NOTICE:
  		case E_USER_NOTICE:
  			$error = 'Notice';
  		case E_WARNING:
  		case E_USER_WARNING:
  			$error = 'Warning';
  		case E_ERROR:
  		case E_USER_ERROR:
  			$error = 'Fatal Error';
  			$error = 'Unknown';
  	$message = 'PHP ' . $error . ':  ' . $errstr . ' in ' . $errfile . ' on line ' . $errline;
  	$file = DIR_FS_CATALOG . 'includes/logs/' . date('Y-m-d') . '.log';
  	$handle = fopen($file, 'a+');
  	fwrite($handle, date('G:i:s') . ' - ' . print_r($message, true)  . "\n");
  	return true;
  // Error Handler

Step 2. Ensure the includes > logs folder is writable by your web server.

Now, if there is any error in your store it will be recorded into a file named with current date.


So, you could check the errors based on the date as follow:

Snip20150322_16A future TomatoCart update will contain a full logging mechanism so keep an eye out for that!

Looking for quality TomatoCart hosting? Check out Arvixe Web Solutions.

Tags: , , , , , , , , , | Posted under TomatoCart | RSS 2.0

Author Spotlight

Jack Yin

TomatoCart Developer & Co Founder - Arvixe Web Hosting / TomatoCart Community Liaison

2 Comments on [TomatoCart] Log errors in a separate date file

  1. Dartco says:

    Jack, I think that it’s not correct to use hard-coded paths because it’s very hard to maintain the code and do some global changes.
    And the second moment: For what reason you inserted LOGS and WORK in INCLUDES directory?
    INCLUDES shall be no writable directory.
    You need to move EXT to INCLUDES directory and move LOGS and WORK out.

    And the last. Why you do not use CACHE directory to store cached files and use directory with WORK title. I think that WORK is useless directory.

    If you try to do perfect (you may be not try, but I try to fix some stuff 🙂 store there shall be some logic in directory structure

    You can also add this to github and rework the directory structure (including subdirectories for cache of big stores as I asked you so long ago). I think it’s time to do this finally

  2. Dartco says:

    Forgot to add: it’s better to write log in XML format and then someone will be able to process the logs inside TC admin panel

Leave a Reply

Your email address will not be published. Required fields are marked *