This hook performs various WHMCS log garbage collection, done daily.

This hook runs daily, at a given hour you can define at your own in line 18, and removes from logs various unuseful stuff.

In lines 19 to 23 you can find various examples of queries to purge different logs:

  • whoislog: removes every record oldest than 3 days
  • activitylog: removes "automated task starting", "Domain Sync Cron completed" and "Cron running" records oldest than 2 days

You can add further queries to clean other kinds of logs, used by addon or registrar module (i.e. in rows 23 to 25 you have code examples to clean dns_manager2_log, useful only if you're using the DNS Manager Addon module by Modulesgarden).

You can customize the number of days that different kind of record is retained before deleting it ( strtotime('-2 days'), strtotime('-3 days') , strtotime('-5 days') etc.)

<?php

use WHMCS\Database\Capsule;

//
// various log garbage collection, done daily
//
// this script run daily, at a given hour you can define at your own in line 18, and remove from logs various unuseful stuff 
// in line 19 to 23 you have various examples of queries to purge different logs:
// whoislog: removes every record oldest than 3 days
// activitylog: removes "automated task starting", "Domain Sync Cron completed" and "Cron running" records oldest than 2 days
// You can add further queries to clean other kind of logs, used by addon or registrar module (i.e. in rows 23 to 25 you have code examples to clean dns_manager2_log, useful only if
// you're using the DNS Manager Addon module by Modulesgarden
// You can customize the number of days any kind of record is retained before deleting it (  strtotime('-2 days'),  strtotime('-3 days') , strtotime('-5 days') etc.)

function logcleaner($vars) {
  
    if (date('H') == '17' && intval(date('i')) < 5) {
       $deletedtblwhoislog=Capsule::table('tblwhoislog')->where('date', '<',date('Y-m-d', strtotime('-3 days')))->delete();
       $deletedtblactivitylog=Capsule::table('tblactivitylog')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('description','LIKE','%Automated Task: Starting%')->delete();
       $deletedtblactivitylog2=Capsule::table('tblactivitylog')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('description','LIKE','%Domain Sync Cron: Completed%')->delete();
       $deletedtblactivitylog3=Capsule::table('tblactivitylog')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('description','LIKE','%Cron running at%')->delete(); 
//       $deletedtbldns1=Capsule::table('dns_manager2_log')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('value','LIKE','Cron Log Cleaner Started')->delete(); 
//       $deletedtbldns2=Capsule::table('dns_manager2_log')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('value','LIKE','Cron Cleaner Started')->delete(); 
//       $deletedtbldns3=Capsule::table('dns_manager2_log')->where('date', '<',date('Y-m-d', strtotime('-2 days')))->where('value','LIKE','Cron Status Zone Started')->delete(); 
       
      logActivity('log cleaning: tblwhoislog deleted '.$deletedtblwhoislog.' records - tblactivitylog deleted '.($deletedtblactivitylog+$deletedtblactivitylog2+$deletedtblactivitylog3).' records - dns_manager2_log deleted '.($deletedtbldns1+$deletedtbldns2+$deletedtbldns3).' records');
    }  
}

add_hook("AfterCronJob",5,"logcleaner");

This script is available also on DomainRegister's GiHub Repository.

 

Found this article interesting?
Subscribe to DomainRegister´s newsletter!

You can unsubscribe at any time by simply clicking the link in the footer of our emails. For information about our privacy practices, please visit our website.

We use Mailchimp as our marketing platform. By clicking below to subscribe, you acknowledge that your information will be transferred to Mailchimp for processing. Learn more about Mailchimp s privacy practices here.

  • WHMCS
  • 0 uporabniku/om je bi članek v pomoč
Vam je bil odgovor v pomoč?

Povezani članki

 How to add a favicon in WHMCS

To add a favicon to your WHMCS install: copy your favicon (es. myfavicon.ico) in WHMCS folder...

 How to comment in template (.tpl) files

If you need to insert a comment in a .tpl file, or comment a block of code to disable it, you...

 How to insert a Youtube video in WHMCS knowledgebase article

To insert a YouTube video in an article of the WHMCS knowledge base, you need to insert following...

 How to disable a customer to pay invoices partly with credit balance

in WHMCS if a customer has not enough credit balance for full payment of an invoice, WHMCS allows...

 How to add a cookie bar to WHMCS

In order to add a cookie bar to your own installation of WHMCS: copy somewhere on your WHMCS...