Forum Moderators: coopster & phranque

Message Too Old, No Replies

Trying to html a log file

Search engine log file

         

qwerty12

5:50 pm on Jun 11, 2001 (gmt 0)



i'm currently working on a search engine directory.I can log all search queriesin a txt file but i want to enable users to look at the last 10 searchs carried out.Any ideas?
[thetiller.com...]

rencke

9:01 pm on Jun 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hello qwerty12 and Welcome to WebmasterWorld. I and my collegues hope that you will like our "University of Search Engine Optimization".

I took the liberty of moving your question from the European forum into Server Side Scripting, in the belief that
a) the solution lies within this field and
b) the people who frequent this forum will find it easy and have an answer right away. (I will be keeping an eye on them on your behalf though, just in case.)

One thought that crossed my mind is that if you base your site on ODP data, rather than on your own db, then the free ODP++ program happens to have exactly what you want, built in from the start. It can be downloaded from www.portalscripts.com.

sugarkane

9:23 pm on Jun 11, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming the queries are stored one per line in plain text, this is the basic code in Perl (you can edit the 'print' line to include whatever HTML formatting you need to make it fit into the page):

#!/usr/bin/perl

open(FP,"query.log");
chomp(@lines=<FP>);
close(FP);

@lines=reverse(@lines);

for ($i=0;$i < 10;$i++) {
print "$lines[$i]<br>";
}

exit;

Note: this won't scale well as it reads the whole log file into memory every time it runs. If you use this method I'd advise you keep a close eye on logfile size...

Any ideas for a more scalable solution anyone?

Brett_Tabke

5:23 pm on Jun 12, 2001 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



>I can log all search queriesin a txt file.

Log them to two files. The one is your "keeper" file to save for data storage and the other will be your "spy" file to view in real time.

#!/usr/bin/perl

open(FP,"spy.log");
chomp(@lines=<FP>);
close(FP);
open(FILE,">spy.log")
@lines=reverse(@lines);

for ($i=0;$i < 10;$i++) {
print "$lines[$i]<br>";
print FILE "$lines[$i]\n";
}
close FILE;
exit;

qwerty12

5:33 pm on Jun 12, 2001 (gmt 0)



Thanks for all your help

sugarkane

6:36 pm on Jun 12, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> Log them to two files

Nice idea...

The script has a gotcha though - the reverse(@lines) will constantly flip-flop the order of the lines, and the last 10 entries won't be preserved.

#!/usr/bin/perl

open(FP,"+<spy.log");
flock FP,2;
chomp(@lines=<FP>);
$count=$#lines;

truncate FP,0;
seek FP,0,0;

for ($i=($count-9);$i <= $count;$i++) {
print "$lines[$i]<br>";
print FP "$lines[$i]\n";
}
close(FP);
exit;