Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Simple log script

Posted on 1999-07-07
17
Medium Priority
?
160 Views
Last Modified: 2010-03-04
I am making a script to log hits on my webpage (Yes I know that there are already a million out there).  Anyway..  how do i make the log entries be written to the top of the log file?  Also how can I limit the number of entries?  Some sample code would be nice.

Since this is kindof a double question I will increase the points some if I get a good answer.
0
Comment
Question by:TAF2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 5
  • 3
17 Comments
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213931
This should be OK, so long as your log file isn't huge.

open(IN_FH "<$log_name");
open(OUT_FH ">$tmp_log_name");
@all_lines = <IN_FH>;
unshift(@all_lines,$log_entry);
#Only savethe last x most recent records
foreach $line (1 .. $MAX_ENTRIES)
{  
    print(OUT_FH $line);
}
close(IN_FH);
close(OUT_FH);
rename($tmp_log_name,$log_name);

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213932
This should be OK, so long as your log file isn't huge.

open(IN_FH "<$log_name");
open(OUT_FH ">$tmp_log_name");
@all_lines = <IN_FH>;
unshift(@all_lines,$log_entry);
#Only savethe last x most recent records
foreach $line (1 .. $MAX_ENTRIES)
{  
    print(OUT_FH $line);
}
close(IN_FH);
close(OUT_FH);
rename($tmp_log_name,$log_name);

0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213933
Just realized I forgot something!

foreach $line (1 .. $MAX_ENTRIES)
{    
    print(OUT_FH $line);
    eof and last; # file hasn't reached max size yet
}

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213934
Sorry, I rushed the first answers a bit. Here is a more complete version.

$max_lines = 1000;
open(IN_FH "<$log_name");
open(OUT_FH ">$tmp_log_name");
@all_lines = <IN_FH>;
unshift(@all_lines,$log_entry);
#Only save the last x most recent records
$lines = 0;
foreach $line (@all_lines)
{
    ($lines > $max_lines) and last;  
    print(OUT_FH $line);
    $lines++;
}
close(IN_FH);
close(OUT_FH);
rename($tmp_log_name,$log_name);


0
 
LVL 84

Expert Comment

by:ozo
ID: 1213935
without a lock, that could miss some entrys is two people try to add to the log at the same time.
(but that may be ok, since you don't mind missing entrys after receiving 1000 new ones)
0
 

Author Comment

by:TAF2000
ID: 1213936
Okay, I am sorry I have not responeded sooner.  I am still trying to get that to work.  Where do I put the part that actually gets the information?

This part:

 print "Content-type: text/html\n\n ";
    print LOG " [ <B>Time:</B> $getdate ] - \n";
    print LOG " [ <B>User IP:</B> $ENV{'REMOTE_ADDR'} ] - \n";
    print LOG " [ <B>With:</B> $ENV{'HTTP_USER_AGENT'} ] - \n";
    print LOG " [ <B>Host:</B> $ENV{'REMOTE_HOST'} ] - \n";
    print LOG "[ <B>From:</B> <A HREF=$ENV{'HTTP_REFERER'}>$ENV{'HTTP_REFERER'}</A> ]<BR><HR><BR>\n\n";


Thanks.
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213937
Not completely sure what you mean. When you create the file handle LOG, you must assign it a full path name. This is the same name you should use for $log_name in the line:

open(IN_FH "<$log_name");

0
 

Author Comment

by:TAF2000
ID: 1213938
I don't understand your answer, I don't think that it is what I am looking for.  
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213939
$max_lines = 1000;
open(OLD_LOG "<$log_name");
open(LOG ">$tmp_log_name");
@all_lines = <OLD_LOG>;

# This adds the latest entry
$latest = " [ <B>Time:</B> $getdate ] - \n";
$latest .= " [ <B>User IP:</B> $ENV{'REMOTE_ADDR'} ] - \n";
$latest .= " [ <B>With:</B> $ENV{'HTTP_USER_AGENT'} ] - \n";
$latest .= " [ <B>Host:</B> $ENV{'REMOTE_HOST'} ] - \n";
$latest .= "[ <B>From:</B> <A HREF=$ENV{'HTTP_REFERER'}>$ENV{'HTTP_REFERER'}</A> ]<BR><HR><BR>\n\n";
unshift(@all_lines,$latest);

#Only save the last x most recent records
$lines = 0;
foreach $line (@all_lines)
{
    ($lines > $max_lines) and last;    
    print(LOG $line);
    $lines++;
}
close(OLD_LOG);
close(LOG);
rename($tmp_log_name,$log_name);

#
# Let me know of any problems



0
 

Author Comment

by:TAF2000
ID: 1213940
what is wrong with this?  It still isn't working...The logfile is blank.


#!/usr/bin/perl


$log_name = "/web2/seek-it/public_html/stats/newmp3.html";
$tmp_log_name = "/web2/seek-it/public_html/stats/newmp3tmp.html";


$getdate = `date +"%D %T %Z"`;
chop ($getdate);

$max_lines = 1000;
open(OLD_LOG "<$log_name");
open(LOG ">$tmp_log_name");
@all_lines = <OLD_LOG>;

# This adds the latest entry
$latest = " [ <B>Time:</B> $getdate ] - \n";
$latest .= " [ <B>User IP:</B> $ENV{'REMOTE_ADDR'} ] - \n";
$latest .= " [ <B>With:</B> $ENV{'HTTP_USER_AGENT'} ] - \n";
$latest .= " [ <B>Host:</B> $ENV{'REMOTE_HOST'} ] - \n";
$latest .= "[ <B>From:</B> <A HREF=$ENV{'HTTP_REFERER'}>$ENV{'HTTP_REFERER'}</A> ]<BR><HR><BR>\n\n";
unshift(@all_lines,$latest);

#Only save the last x most recent records
$lines = 0;
foreach $line (@all_lines)
{
    ($lines > $max_lines) and last;    
    print(LOG $line);
    $lines++;
}
close(OLD_LOG);
close(LOG);
rename($tmp_log_name,$log_name);

exit;


I am getting these errors:
Missing comma after first argument to open function at /web2/seek-it/public_html/cgi-bin/newmp3.pl line 26, near ""<$log_name")"
Missing comma after first argument to open function at /web2/seek-it/public_html/cgi-bin/newmp3.pl line 26, near ""<$log_name")"
Missing comma after first argument to open function at /web2/seek-it/public_html/cgi-bin/newmp3.pl line 27, near "">$tmp_log_name")"
Missing comma after first argument to open function at /web2/seek-it/public_html/cgi-bin/newmp3.pl line 27, near "">$tmp_log_name")"
Execution of /web2/seek-it/public_html/cgi-bin/newmp3.pl aborted due to compilation errors.
Execution of /web2/seek-it/public_html/cgi-bin/newmp3.pl aborted due to compilation errors.
[Mon Aug  2 07:27:35 1999] [error] [client 171.212.249.107] Premature end of script headers: /web2/seek-it/public_html/cgi-bin/newmp3.pl
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213941
open(OLD_LOG,"<$log_name");
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213942
Thanks ozo, also
open(LOG, ">$tmp_log_name");

0
 

Author Comment

by:TAF2000
ID: 1213943
Yeah, I figured the second one out.  I have modified it alot more and it works good now.  Thanks.  Go ahead and answer it again so I can give you my points.
0
 
LVL 19

Accepted Solution

by:
Kim Ryan earned 240 total points
ID: 1213944
OK, here is final solution, with optional file locking added

#!/usr/bin/perl

$log_name = "/web2/seek-it/public_html/stats/newmp3.html";
$tmp_log_name = "/web2/seek-it/public_html/stats/newmp3tmp.html";

$getdate = `date +"%D %T %Z"`;
chop ($getdate);

$max_lines = 1000;
open(OLD_LOG, "<$log_name");
flock(OLD_LOG,2); # apply write lock
open(LOG, ">$tmp_log_name");
flock(LOG,2); # apply write lock
@all_lines = <OLD_LOG>;


# This adds the latest entry
$latest = " [ <B>Time:</B> $getdate ] - \n";
$latest .= " [ <B>User IP:</B> $ENV{'REMOTE_ADDR'} ] - \n";
$latest .= " [ <B>With:</B> $ENV{'HTTP_USER_AGENT'} ] - \n";
$latest .= " [ <B>Host:</B> $ENV{'REMOTE_HOST'} ] - \n";
$latest .= "[ <B>From:</B> <A HREF=$ENV{'HTTP_REFERER'}>$ENV{'HTTP_REFERER'}</A> ]<BR><HR><BR>\n\n";
unshift(@all_lines,$latest);

#Only save the last x most recent records
$lines = 0;
foreach $line (@all_lines)
{
    ($lines > $max_lines) and last;      
    print(LOG $line);
    $lines++;
}

flock(LOG,8);  # remove lock
close(LOG);
flock(LOG,8);  
close(OLD_LOG);
 
rename($tmp_log_name,$log_name);


0
 

Author Comment

by:TAF2000
ID: 1213945
Yeah..like I said I have it working fine now..  What is file locking?
0
 
LVL 19

Expert Comment

by:Kim Ryan
ID: 1213946
It gives you exclusive rights to read and/or update a file, preventing any other processes from updating until you release the lock. Makes sense for highly trafficed web sites where you get many hits at almost the same time.
0
 
LVL 84

Expert Comment

by:ozo
ID: 1213947
Unfortunately, the way you've used the locks, it looks like you've left room between the unlock and the rename for someone to come in and replace your new entry.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question