Question

There is an option for cleaning log under system->config->sytem->log cleaning. Does it clear log tables or files under var/log. If I enable log cleaning how do find when and which logs are cleared?

Was it helpful?

Solution

The default Magento does not clear everything as mentioned by Tim.

Check out my detailed response How should I handle session files that become too numerous?. It includes a maintenance file that can be automated using cron jobs.

Source: How should I handle session files that become too numerous?

On all of our setups we have a maintenance.php file which takes care of the cleaning of the logs and var directory once in a while. Since the sessions have to be either saved in the database or on the file system, this maintenance file will clean them up both. (See code below).

You can run the following command as a cron job to clean up the logs:

php maintenance.php clean=log

The above command will produce the following output:

catalogindex_aggregation has been truncated
catalogindex_aggregation_tag has been truncated
catalogindex_aggregation_to_tag has been truncated
catalog_compare_item has been truncated
dataflow_batch_export has been truncated
dataflow_batch_import has been truncated
log_customer has been truncated
log_quote has been truncated
log_summary has been truncated
log_summary_type has been truncated
log_url has been truncated
log_url_info has been truncated
log_visitor has been truncated
log_visitor_info has been truncated
log_visitor_online has been truncated
report_compared_product_index has been truncated
report_event has been truncated
report_viewed_product_index has been truncated

You can run the following command as a cron job to clean up the var folder:

php maintenance.php clean=var

The above command will produce the following output:

downloader/.cache/* has been emptied
downloader/pearlib/cache/* has been emptied
downloader/pearlib/download/* has been emptied
var/cache/ has been emptied
var/locks/ has been emptied
var/log/ has been emptied
var/report/ has been emptied
var/session/ has been emptied
var/tmp/ has been emptied

The actual code (Don't forget to adjust the path to your local.xml file):

<?php
$xml = simplexml_load_file('./app/etc/local.xml', NULL, LIBXML_NOCDATA);

$db['host'] = $xml->global->resources->default_setup->connection->host;
$db['name'] = $xml->global->resources->default_setup->connection->dbname;
$db['user'] = $xml->global->resources->default_setup->connection->username;
$db['pass'] = $xml->global->resources->default_setup->connection->password;
$db['pref'] = $xml->global->resources->db->table_prefix;

if (!isset($argv[1]) || !stristr($argv[1], 'clean=')) {
    echo 'Please use one of the commands below:' . PHP_EOL;
    echo 'php maintenance.php clean=log' . PHP_EOL;
    echo 'php maintenance.php clean=var' . PHP_EOL;
    die;
}

$method = str_replace('clean=', '', $argv[1]);

switch ($method) {
case 'log':
    clean_log_tables();
    break;
case 'var':
    clean_var_directory();
    break;
default:
    echo 'Please use one of the commands below:' . PHP_EOL;
    echo 'php maintenance.php clean=log' . PHP_EOL;
    echo 'php maintenance.php clean=var' . PHP_EOL;
    break;
}

function clean_log_tables() {
    global $db;

    $tables = array(
        'catalogindex_aggregation',
        'catalogindex_aggregation_tag',
        'catalogindex_aggregation_to_tag',
        'catalog_compare_item',
        'dataflow_batch_export',
        'dataflow_batch_import',
        'log_customer',
        'log_quote',
        'log_summary',
        'log_summary_type',
        'log_url',
        'log_url_info',
        'log_visitor',
        'log_visitor_info',
        'log_visitor_online',
        'report_compared_product_index',
        'report_event',
        'report_viewed_product_index'
    );

    mysql_connect($db['host'], $db['user'], $db['pass']) or die(mysql_error());
    mysql_select_db($db['name']) or die(mysql_error());

    foreach($tables as $v => $k) {
        @mysql_query('TRUNCATE `'.$db['pref'].$k.'`');
        echo $db['pref'] . $k . ' has been truncated' . PHP_EOL;
    }
}

function clean_var_directory() {
    $dirs = array(
        'downloader/.cache/*',
        'downloader/pearlib/cache/*',
        'downloader/pearlib/download/*',
        'var/cache/',
        'var/locks/',
        'var/log/',
        'var/report/',
        'var/session/',
        'var/tmp/'
    );

    foreach($dirs as $v => $k) {
        exec('rm -rf '.$k);
        echo $k . ' has been emptied' . PHP_EOL;
    }
}

OTHER TIPS

By default Magento only cleans only the following tables:

log_quote
log_url
log_visitor_info
log_visitor
log_url_info
log_customer

Which are actually not all ^log* tables.

var/log directory is not cleaned. I think the reason is it would be gruesome to try to determine which log entries are expired and have to be removed.

However you can do whatever you like by observing either log_log_clean_before, or log_log_clean_after events and running something like:

$dirs = array(
    'downloader/.cache/*',               // MagentoConnect 2.0
    'downloader/pearlib/cache/*',        // MagentoConnect 1.0
    'downloader/pearlib/download/*',     // MagentoConnect 1.0
    'var/cache/',
    'var/log/',
    'var/report/',
    'var/tmp/'
);

exec('rm -rf ' . implode(' ', $dirs));

By using this way , you should manually clean up the log or automatically:

System > Configuration > Advanced > System > Log Cleaning

Magento hasn't any class to control its var/log files, only writing.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top