Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts

    Write new log file daily

    Hi

    I have a little function that sends a email if a webpage is not found
    and logs the status of either up or down, the log gets big if I don't
    delete it often, can some show how I can create a new log daily
    or once a week something like that?

    Thanks
    Sonny

    PHP Code:
    if (!check($host$find)) alert($to$host$body$headers) ;//Send the email
    else 
    $content  
    '
    .$host' UP ' date'Y-m-d l h:i:s a' ).'' ;//Write the log
    $file 'status.log'//log file
    if (is_writable($file)) {

    if (!
    $handle fopen($file'a')) {
    exit;
    }
    if (
    fwrite($handle$content) === FALSE) {
    echo 
    "Cannot write to file ($file)";
    exit;
    }
    fclose($handle);
    }
    ?> 

  • #2
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by sonny View Post
    I have a little function that sends a email if a webpage is not found and logs the status of either up or down, the log gets big if I don't delete it often, can some show how I can create a new log daily or once a week something like that?
    Name your log files with a date and include logic to delete log files that are over x number of days old any time the function is run.

    PHP Code:
    <?php
    $KeepDays 
    6;  # how many days of log files we'll keep on hand
    $logname  date("Ymd") . '.status.log';
    $OldFile  date('Ymd',mktime(0,0,0,date("m"),(date("d") - ($KeepDays 1)),date("Y"))) . '.status.log';
    if (
    file_exists($OldFile)) { unlink($OldFile); } # erase oldest log file
    #
    # Add the rest of your logic here
    #
    # $logname is your log file name
    #
    ?>
    Dave .... HostMonster for all of your hosting needs

  • Users who have thanked djm0219 for this post:

    sonny (06-19-2011)

  • #3
    Regular Coder sonny's Avatar
    Join Date
    Apr 2008
    Location
    United States
    Posts
    567
    Thanks
    88
    Thanked 0 Times in 0 Posts
    Thanks, will try that.


    shouldn't this below create a file if it did not exist?
    but it doesn't, I even add a +, which did not help

    I was trying to name the file by a date, so another
    would get created everyday, but could not get it to
    create a file for some reason.

    your code is way better anyway, but wouldn't that,
    need to create a new file as we'll?
    PHP Code:
    if (!$handle fopen($file'a')) { // does not create
    if (!$handle fopen($file'a+')) {// does not create 

    Thanks
    Sonny

  • #4
    Regular Coder
    Join Date
    Jul 2010
    Posts
    271
    Thanks
    3
    Thanked 40 Times in 40 Posts
    PHP Code:
    {
    $content  ' '.$host' UP ' date'Y-m-d l h:i:s a' ).'' ;//Write the log 
    $file date(mdy).'status.txt'//log file 
    if (file_exists($file)) 
       { 
       
    $handle fopen($file'a'); 
       } else {
       
    $handle fopen($file'w'); 
       }
    fputs($handle"$content");
    fclose($handle);

    If you can't stand behind your troops, feel free to stand in front of them
    Semper Fidelis

  • Users who have thanked jimhill for this post:

    sonny (06-19-2011)

  • #5
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Using
    PHP Code:
    if (!$handle fopen($file'a')) 
    will work just fine. As the documentation states:

    'a' Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.
    You'll likely find it simpler to use something like

    PHP Code:
    $LogOut fopen($logname,'a') or die("Could not open our log file for append"); 
    to get your log file ready to write to. You could of course echo something instead of using die if desired.
    Dave .... HostMonster for all of your hosting needs

  • #6
    Senior Coder djm0219's Avatar
    Join Date
    Aug 2003
    Location
    Wake Forest, North Carolina
    Posts
    1,302
    Thanks
    4
    Thanked 204 Times in 201 Posts
    Quote Originally Posted by jimhill View Post
    PHP Code:
    {
    $content  ' '.$host' UP ' date'Y-m-d l h:i:s a' ).'' ;//Write the log 
    $file date(mdy).'status.txt'//log file 
    if (file_exists($file)) 
       { 
       
    $handle fopen($file'a'); 
       } else {
       
    $handle fopen($file'w'); 
       }
    fputs($handle"$content");
    fclose($handle);

    Using the append modifier with fopen precludes the need to check to see if the file exists.
    Dave .... HostMonster for all of your hosting needs


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •