I'm used to setting the log path is this
/home/www/logs/domain name. log
Like what
/home/www/logs/www.yundaiwei.com.log
For ease of administration, logs need to be kept in one file by day and a log of the specified number of days, which is deleted over time.
Share the Script
#!/usr/bin/php
<?php
$logdir = '/home/www/logs/';
Reserved days include the same day
$log _save_day = 7;
$files = Glob ("{$logdir}/*");
foreach ($files as $path) {
$filename = basename ($path);
Preg_match ("/(\d{8}) \.log/", $filename, $preg);
$date = @ $preg [1];
if (empty ($date)) {
// Day log, change filename
$newpath = $logdir. '/' . Str_replace (' Log ', date (' Ymd ', Strtotime ("-1 day '))." Log ', $filename);
Rename ($path, $newpath);
echo "$path >>> $newpath \ n";
}else{
//More than reserved days, delete
if (Time () +10-strtotime ($date) > 3600*24* $log _save_day) {
unlink ($path);
echo "$path delete!\n";
}
}
}
Shell_exec ('/etc/init.d/nginx reload &>/dev/null ');