You are here: Re: Trimming a large log file? « PHP Programming Language « IT news, forums, messages
Re: Trimming a large log file?

Posted by petersprc on 11/29/06 20:13

Another poster mentioned logrotate which is a very easy way to do it.
You can also do it with something like log4php
(http://logging.apache.org/log4php/), if you're willing to use an
external lib. Here's an example configuration:

- Create log4php.properties in your app's dir. Set the file attribute
to your log file path.

log4php.debug=false
log4php.rootLogger=DEBUG, LOG
log4php.appender.LOG=LoggerAppenderRollingFile
log4php.appender.LOG.file=/tmp/my-app
log4php.appender.LOG.layout=LoggerLayoutTTCC
log4php.appender.LOG.maxFileSize=10KB
log4php.appender.LOG.maxBackupIndex=3

- Initialize log4php in an include or your script:

define('LOG4PHP_CONFIGURATION', dirname(__FILE__) .
'/log4php.properties');
require_once(dirname(__FILE__) .
'/log4php/src/log4php/LoggerManager.php');
register_shutdown_function(array('LoggerManager', 'shutdown'));

- Log away in your scripts:

$log =& LoggerManager::getLogger('MyApp');
$log->debug("Debug test.");

If not using log4php, basically you would check the size of the log
file before appending to it (use ftell or filesize), and if it exceeds
your maximum, close the file, move it to a backup or delete it, then
re-open the file, and proceed to write. The LoggerAppenderRollingFile
class in log4php/src/log4php/appenders/LoggerAppenderRollingFile.php is
a good example of this.

Not precisely the rotating buffer that you wanted, but it might do for
most apps... If you do need a rotating buffer, you could do it in the
way you described...

veg_all@yahoo.com wrote:
> I am looking for a simple way to keep my log files from growing too
> large. Basically I would want something that truncates the off first 25
> kb of a 100kb log file. I could do something with reading in the file
> twice, first to determine number of lines. Then a second time to write
> a copy of the reduced log file. Any simpler workarounds?

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация