CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Need a hand with this log file management code, please
Pages: [1]

Author Topic: Need a hand with this log file management code, please  (Read 1596 times)

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Need a hand with this log file management code, please
March 24, 2011, 11:12:26 pm
Currently multiple log files in this directory can grow to 256MB each and CiviCRM will not clean them out until upgrade to the next version.  This can cause disk space issues, so I've endeavored to make CiviCRM clean up after itself.

In theory this should scan the ConfigandLog directory for files older than 31 days and delete them.  I considered making the cutoff as few as 7 days.  Input on that welcome...but anyway...

This script will work as a standalone independent .php file out of CiviCRM (replacing {$config->configAndLogDir} with the actual path, of course) in the context in my filesystem.  However, the DirectoryIterator class (standard with PHP 5.2) doesn't seem to work in CiviCRM.   My guess is that CiviCRM has its own filesystem functions and classes it likes to use, but I don't know where they are or how to implement them.  Or maybe it's that my OOP skills are rusty and I just don't get how to call the DirectoryIterator class within CiviCRM's classes.... Help?

Would Lobo or one of the senior devs please review the code and advise?   thanks.


CRM/Core/Error.php, function debug_log_message (about line 412, after "rename( $fileName..." line)

Code: [Select]
//clean up older files
$file_log = Log::singleton( 'file',$fileName );
$file_log->log("checked for cleanup: {$config->configAndLogDir}\n");
foreach (new DirectoryIterator('{$config->configAndLogDir}') as $logFiles ) {
$fullLogPath = '{$config->configAndLogDir}'.$logFiles->getFilename();
if ($logFiles->isDot() ||
    $logFiles->isDir() ||
$logFiles->getFilename() == '.htaccess' ||
$logFiles->getFilename() == 'Config.IDS.ini' ) continue; //skip these
  if ( $logFiles->getCTime() < strtotime('-31 days') ) {  //find old files
      if ( unlink($fullLogPath) ) {
$file_log->log("deleted old log: " . $fullLogPath . "\n"); //delete them
}
  }
}
« Last Edit: March 25, 2011, 12:31:12 am by Stoob »
Try CiviTeacher: the online video tutorial CiviCRM learning library.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Need a hand with this log file management code, please
March 25, 2011, 11:27:21 am

In the interest of not reinventing the wheel and using the right tools for the right job, shouldnt we be using something like logrotate?

http://linuxcommand.org/man_pages/logrotate8.html

I think Civi should focus on doing the right thing with generating log files, and then some other tools should do rotation compression etc.

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

mdlueck

  • Ask me questions
  • ****
  • Posts: 382
  • Karma: 4
  • CiviCRM version: 4.7.24
  • CMS version: Drupal 6.x
  • MySQL version: 5.5.54
  • PHP version: 5.3.10
Re: Need a hand with this log file management code, please
March 25, 2011, 12:39:48 pm
Subscribe
--
Michael Lueck
Lueck Data Systems
http://www.lueckdatasystems.com/

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Need a hand with this log file management code, please
March 26, 2011, 10:12:04 am
I agree that logrotate is a good tool.  However, I wouldn't know where to begin using logrotate, which appears to be a Linux command, within a CiviCRM script.   

Is the plan?  To create another .php file in <civicrm>/bin directory that will need to be run by cron that contains things such as logrotate?

If the above listed in the strategy, sorry but I don't know how to implement it.

I suppose we could just use this command logrotate in a .sh file in my filesystem elsewhere and run it by cron, avoiding creating a .php file for CiviCRM altogether?  I've already written a .sh file that clears out the Config and Log directory regularly anyway of log files, and it seems to be working.  That satisfies me for the timebeing.






Try CiviTeacher: the online video tutorial CiviCRM learning library.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Need a hand with this log file management code, please
March 26, 2011, 10:21:14 am

You would call logrotate directly from cron. quite a few articles on the web about this. check:

http://www.techrepublic.com/article/manage-linux-log-files-with-logrotate/1052474
http://www.google.com/search?q=running+logrotate+from+cron&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

CiviTeacher.com

  • I live on this forum
  • *****
  • Posts: 1282
  • Karma: 118
    • CiviTeacher
  • CiviCRM version: 3.4 - 4.5
  • CMS version: Drupal 6&7, Wordpress
  • MySQL version: 5.1 - 5.5
  • PHP version: 5.2 - 5.4
Re: Need a hand with this log file management code, please
March 27, 2011, 01:47:26 pm
Ok, gotcha.  Sorry ... I must have misunderstood initially.  I thought you were proposing to change the CiviCRM logging code in the .php file.    From what I am hearing, it doesn't seem like we need to make any changes to CiviCRM code at this time.  Am I correct?  Sorry if I am not understanding this properly.

Instead, are you saying should we just update the documentation to show people how to use cron + logrotate to rotate and archive the .log files on their server if the .log files become a problem?  This would mean, however, that someone who doesn't bother to add logrotate to their cron still might have disk space issues.  But maybe the .log file disk-space problem is not widespread, and I am trying to make solutions that don't need to be made.

« Last Edit: March 27, 2011, 01:49:14 pm by Stoob »
Try CiviTeacher: the online video tutorial CiviCRM learning library.

Donald Lobo

  • Administrator
  • I’m (like) Lobo ;)
  • *****
  • Posts: 15963
  • Karma: 470
    • CiviCRM site
  • CiviCRM version: 4.2+
  • CMS version: Drupal 7, Joomla 2.5+
  • MySQL version: 5.5.x
  • PHP version: 5.4.x
Re: Need a hand with this log file management code, please
March 27, 2011, 07:24:42 pm

i think if there are changes that need to be done, so civi plays better with logrotate (or other log tools) we should do so. I'm not sure what they are (if any)

Adding it to the list of periodic tasks that a civi admin should check on a regular basis is probably a good thing to do

In general, admins do need to use other tools to monitor things like disk space / cpu etc. The Civi MySQL DB will grow over time and could grow quite fast depending on how much its used. We dont have any "archival" stuff as yet, but i suspect we'll need to tackle that to (but this is related to disk space)

lobo
A new CiviCRM Q&A resource needs YOUR help to get started. Visit our StackExchange proposed site, sign up and vote on 5 questions

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Old sections (read-only, deprecated) »
  • Developer Discussion (Moderator: Donald Lobo) »
  • Need a hand with this log file management code, please

This forum was archived on 2017-11-26.