[SOLVED] Auto trim log tables

Hi all,

Does anyone know if there’s anything to help keep Log Module tables to a reasonable size?

Either by, say, “auto-delete any log entries more than 30 days old”, or perhaps “auto-keep only the latest 5000 records”. Maybe something does this housekeeping once a day…

I can’t see anything that offers this, so do I presume I’ll need to set this up myself manually somewhere triggered by a cron job? And if so, has anyone got an example approach they can share?

TIA

Adz

Are you ise mssql or mysql?

It’s MariaDB (MySQL).

I have ended up adding a method to run on the login application.

It’s called from sc_validate_success() after a successfully validated login, and is currently set to delete records from the log table keeping just the latest 90 days’ worth of log entries:

$max_days = 90;		// Change "days" interval here

$delete_sql = "DELETE FROM sc_log WHERE inserted_date < NOW() - INTERVAL " . $max_days . " DAY";
sc_exec_sql($delete_sql);

Probably not the best place to trigger it, but it guarantees it’s regularly run and doesn’t rely on a cron job. Open to better placement suggestions…

Hello Adz.

You can create a sc_log_old table and create a stored procedure to copy from sc_log to sc_log_old all records that have been inserted for more than 30 days. After this you can delete these same records from sc_log. Just schedule this procedure in a daily job.

This way your sc_log table will always be light and still won’t lose any data. You can even make a specific query (GRID app) for sc_log_old with a mandatory filter by date range and not allowing very large ranges.

Sorry for google translate, I’m in Brazil. I’ve been participating in the scriptcase Brasil forum for 14 years (since version 4 of SC) but I’m new here.

2 Likes

Thanks Rodrigo - not a bad idea at all - I appreciate the reply :slight_smile:

1 Like