Example of a PHP system log cut

Source: Internet
Author: User

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 ');

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.