Post by justcurious » Fri Feb 06, 2015 9:35 pm

How frustrating is it when you're trying to develop a mod and you're getting unexpected results. To try and establish where the script is going wrong or falling over can be quite tricky, especially when you can't "echo" valuable information to the screen.

I got fed up writing several lines of code in order to capture certain information or write it to a file, so I developed the "Simple Debug Tool'. With one line of code at the appropriate point, I can leave messages, record the value of variables and the content of arrays.

Whether you're trying to debug the controller or model files on the catalog or the admin side of your mod, just a simple line of code puts all the information you're looking for into a log file where you can easily check what's happening.

This simple modification extends the "Log" class and add two new methods:

Code: Select all

$this->log->debug($output);

$this->log->resetDebug()

$this->log->debug($output)
This line of code writes $output to a file (system/logs/debug.txt). $output can be a string, a variable or an array and can be helpful in debugging a script that is giving unexpected results. Simply add the line at an appropriate point in the script eg.

Code: Select all

// (Adds the string to the log)
$this->log->debug('I have reached "X" point in the script');

// (Adds the content of the variable $language_id to the log)
$this->log->debug($language_id);

// (Adds the content of the POSTed variable 'status' to the log)
$this->log->debug($this->request->post['status']);

// (Adds the content of the complete POST array to the log)
$this->log->debug($this->request->post);

// (Adds the content of the session variable 'user_id' to the log)
$this->log->debug($this->session->data['user_id']);

// (Adds the selected database data to the log)
$this->log->debug($query->rows);

You can also combine two or more elements to provide the information you need. For example, if your Mysql(i) query is returning an unexpected result, you can test the actual query that is being sent:

Code: Select all

$this->log->debug("SELECT DISTINCT * FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '" . (int)$product_id . "' AND pd.language_id = '" . (int)$this->config->get('config_language_id') . "'");
The above code output:

Code: Select all

2015-02-06 13:20:58 - SELECT DISTINCT * FROM oc_product p LEFT JOIN oc_product_description pd ON (p.product_id = pd.product_id) WHERE p.product_id = '30' AND pd.language_id = '1'
Optional 2nd Parameter
You can use an optional second parameter to provide a "label" for the output eg:

Code: Select all

$this->log->debug('The complete POST array', $this->request->post);
This has the same effect as if you had written two separate lines of code eg:

Code: Select all

$this->log->debug('The complete POST array');. 
$this->log->debug($this->request->post);



Output
The following piece of code:

Code: Select all

$this->log->debug('Session Variables', $this->session->data);
Would write to system/logs/debug.txt:

Code: Select all

2015-02-06 09:57:44 - Session Variables
2015-02-06 09:57:44 - Array
(
    [currency] => USD
    [backup_scheduled_date] => 
    [user_id] => 1
    [token] => 02a3b5fcb99d883713d8d19fbec3bf11
)
$this->log->resetDebug()
Simply empties the log file so you don't have to scroll to the end of a long file if you haven't deleted previous entries.


Available Now
Get it from here: http://www.opencart.com/index.php?route ... n_id=20809
Last edited by justcurious on Sun Oct 15, 2017 4:30 pm, edited 1 time in total.

Google Product Feed - Get your products into Google Shopping. Includes a bulk update facility.
Backup Pro - Backup (on demand or scheduled), Restore and Clone your store.
Freestyle Box - Add multiple information boxes on multiple pages of your store. Includes optional "Code Mode".
View my other extensions


User avatar
Active Member

Posts

Joined
Sat Dec 24, 2011 4:36 pm
Location - UK

Post by alexmath » Fri Oct 13, 2017 6:40 am

Hello, this would be very helpful but it doesnt work in OC 3. is there a fix?

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by justcurious » Sun Oct 15, 2017 4:31 pm

I'll try to update it for OC3.x in the next week or so.

Google Product Feed - Get your products into Google Shopping. Includes a bulk update facility.
Backup Pro - Backup (on demand or scheduled), Restore and Clone your store.
Freestyle Box - Add multiple information boxes on multiple pages of your store. Includes optional "Code Mode".
View my other extensions


User avatar
Active Member

Posts

Joined
Sat Dec 24, 2011 4:36 pm
Location - UK

Post by alexmath » Wed Oct 18, 2017 8:19 am

Alright, just post here once you do or let me know. It works perfectly with OC 2.x. probably with those before it as well. Thanks for the efforts. Makes development really easy and fast.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by justcurious » Wed Oct 18, 2017 2:48 pm

I have updated the extension this morning

Kind regards

Google Product Feed - Get your products into Google Shopping. Includes a bulk update facility.
Backup Pro - Backup (on demand or scheduled), Restore and Clone your store.
Freestyle Box - Add multiple information boxes on multiple pages of your store. Includes optional "Code Mode".
View my other extensions


User avatar
Active Member

Posts

Joined
Sat Dec 24, 2011 4:36 pm
Location - UK

Post by alexmath » Sun Oct 22, 2017 1:49 am

oh thanks, am going to download it now. Gratitude to you and the vqmod writer guy. It made me learn opencart in about 2 weeks or less, so easily. really I appreciate it. Am sure others also do and many more will definitely appreciate it. I think you should create a donation link for your tool.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am

Post by alexmath » Sun Oct 22, 2017 2:17 am

Oh i forgot something, I think you need to update your readme doc on the site about the location of the debug.txt file.
It's in system/storage/logs for OC 2.3x and OC 3.0.2x. I dont know about others.

New member

Posts

Joined
Sat Sep 23, 2017 7:35 am
Who is online

Users browsing this forum: No registered users and 6 guests