CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Fomatting report

 



passat
Novice

May 13, 2005, 5:24 AM

Post #1 of 13 (1020 views)
Fomatting report Can't Post

I use simple script that keeps a log of everyone who "hits" the page, keeping the information in one file. For some reason, it produce unformatted log, without spaces and paragrafs, which is very difficult to browse:

Time: 05/13/05 08:09:12 EDT User: Host: Addr: 22.85.112.194 With: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Page: From: Time: 05/13/05 08:09:18 EDT User: Host: Addr: 22.85.112.194 With: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Page: From: Time: 05/13/05 08:09:28 EDT User: Host: Addr: 22.85.112.194 With: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Page: From: Time: 05/13/05 08:09:31 EDT User: Host: Addr: 22.85.112.194 With: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1) Page: From:

How to make text separated a little, like this:

Date: Fri May 13 04:00:08 2005
Page Visited: http://www.appt.org.uk/technical/calc_and_convert.htm
Host Name:
Visitor IP: 65.117.182.58
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1

Date: Fri May 13 04:00:44 2005
Page Visited: http://www.appt.org.uk/technical/size_chart.htm
Host Name:
Visitor IP: 65.117.182.58
Browser: Mozilla/5.0 (Macintosh; U; PPC Mac OS X Mach-O; en-US; rv:1.7.6) Gecko/20050225 Firefox/1.0.1



Code
  #! /usr/local/bin/perl# logger.cgi  
# version 1.0
#
# Create a file called main.log. Must have write permissions
# set to main.log$mainlog = "main.log";
$shortdate = `date +"%D %T %Z"`;
chop ($shortdate);
print "Content-type: text/plain\n\n ";
open (MAINLOG, ">>$mainlog");
print MAINLOG "Time: $shortdate\n";
print MAINLOG "User: $ENV{'REMOTE_IDENT'}\n";
print MAINLOG "Host: $ENV{'REMOTE_HOST'}\n";
print MAINLOG "Addr: $ENV{'REMOTE_ADDR'}\n";
print MAINLOG "With: $ENV{'HTTP_USER_AGENT'}\n";
print MAINLOG "Page: $ENV{'DOCUMENT_URI'}\n";
print MAINLOG "From: $ENV{'HTTP_REFERER'}\n\n";
close (MAINLOG);
exit;



KevinR
Veteran


May 13, 2005, 10:20 AM

Post #2 of 13 (1016 views)
Re: [passat] Fomatting report [In reply to] Can't Post

The way you are printing the report is fine, but if you are reading the report in a web browser you have to use HTML tags to format the display. You can use the <pre> tag to keep the formatting of your report or the <plaintext> tag. <plaintext> has no closing tag however, once inserted into an html document the rest of the document will render as text, html tags included. Or you can just use a text editing program to read the file instead of a browser.
-------------------------------------------------


(This post was edited by KevinR on May 13, 2005, 10:21 AM)


passat
Novice

May 13, 2005, 11:49 AM

Post #3 of 13 (1013 views)
Re: [KevinR] Fomatting report [In reply to] Can't Post

do you meant I should create html file to display logs? Or enough just print the proper HTML line breaks for line endings? How? The script creates log file as .txt Could you be more detail?


(This post was edited by passat on May 13, 2005, 12:25 PM)


KevinR
Veteran


May 13, 2005, 12:25 PM

Post #4 of 13 (1008 views)
Re: [passat] Fomatting report [In reply to] Can't Post

IF you are reading the report in a web browser, like Internet Explorer or Mozilla, you need to use HTML code to display the page properly like any other web page you see on the internet.

You could create a simple html file and record the data in that file or use a perl script to open the txt file and generate the html code to display the report formatted for a web browser.

One very simple way using perl would be:


Code
#! /usr/local/bin/perl 
# logger.cgi
# version 1.0
#
# read the file and format for display in a web browser

print "Content-type: text/plain\n\n ";
print "<html><head><title>Web Report</title></head>\n";
print "<body bgcolor=\"#FFFFFF\">";
print "<h1>Web Report</h1>\n";
print "<pre style=\"font: normal #000000 'Courier New';\">";

open (MAINLOG, "$mainlog") or die "$!";
while (<MAINLOG>) {
print;
}
close (MAINLOG);
print "</pre></body></html>";
exit;


most perl coders will recommend you use the CGI module for any html/cgi output, but for a short script like this I see no harm in using hard coded html. See how the above works, you can make it as plain or as fancy as you want by changing the html code.

Type the URL of the perl script into your web browsers address box tp run the script, example, if you name the script webreport.pl :

http://www.yoursite.com/cgi-bin/webreport.pl
-------------------------------------------------


(This post was edited by KevinR on May 13, 2005, 12:31 PM)


passat
Novice

May 13, 2005, 12:34 PM

Post #5 of 13 (1005 views)
Re: [KevinR] Fomatting report [In reply to] Can't Post

Thank you. But my logger script itself could not print data already with line break for line ending?


(This post was edited by passat on May 13, 2005, 12:35 PM)


KevinR
Veteran


May 13, 2005, 2:50 PM

Post #6 of 13 (1001 views)
Re: [passat] Fomatting report [In reply to] Can't Post


In Reply To
Thank you. But my logger script itself could not print data already with line break for line ending?


hmmm.... that should be no problem, what happend when you tried to run the script? Post your code and a link to the script if possible.
-------------------------------------------------


rork
User

May 14, 2005, 2:27 AM

Post #7 of 13 (996 views)
Re: [passat] Fomatting report [In reply to] Can't Post

Instead of writing a .log file you might consider writing a .txt file. Most browsers can read these.

And then there are other solutions like printing newline in html tags to the log-file

Code
print MAINLOG "Time: $shortdate<BR>\n";


or substitute it while reading the file

Code
while (<LOG>) { 
print $_ . "<BR>";
}

--
Don't reinvent the wheel, use it, abuse it or hack it.


KevinR
Veteran


May 14, 2005, 10:29 AM

Post #8 of 13 (986 views)
Re: [rork] Fomatting report [In reply to] Can't Post

well, if you are going to insert html tags into a file you might as well make it a .html file instead of a .txt file and include all the appropriate html tags. But you also have to save the file in a public access directory instead of the cgi-bin.
-------------------------------------------------


passat
Novice

May 16, 2005, 4:32 AM

Post #9 of 13 (968 views)
Re: [KevinR] Fomatting report [In reply to] Can't Post

I tried the script you show that must open the txt file and generate the html.

Some things in unclear: if the main.log logfile will be located in cgi-bin directory (that weblog.pl could find it there), the log file should have 777 chmod, that all world can write in. The chmod777 in cgi-bin impossible due security reasons, so probably main.log need be located outside a cgi-bin directory. Is this correct?

In that case, there are should be specified path to log file in the weblog.pl script mentioned? Please specify.

What is the right place to store main.log file?


(This post was edited by passat on May 16, 2005, 6:04 AM)


KevinR
Veteran


May 16, 2005, 4:32 PM

Post #10 of 13 (951 views)
Re: [passat] Fomatting report [In reply to] Can't Post

The log file should need either 600 or 644 permissions, not 777. Only scripts need 7XX (and folders too I think) permission since they need to execute.

If the log file is not in the same directory as the script then yes, you have to provide the path to where the script is.

There is no "right" place to store the log file. It all depends on who you want to have acess to the file. If you want anyone to be able to type in the address of the file into a browser then it has to be in a www accessible folder. If you want your script to open and display the log file then it can be in just about any folder.
-------------------------------------------------


passat
Novice

May 17, 2005, 7:04 AM

Post #11 of 13 (941 views)
Re: [KevinR] Fomatting report [In reply to] Can't Post

I tried, its not work for me. Dont know what's wrong. Log.cgi and weblog.pl - located in cgi-bin and main.log created in web directory.

Weblog.pl just show empty page.

http://octave.netfirms.com/cgi-bin/weblog.pl
http://octave.netfirms.com/cgi-bin/log.cgi
http://octave.netfirms.com/main.log


KevinR
Veteran


May 17, 2005, 10:04 AM

Post #12 of 13 (939 views)
Re: [passat] Fomatting report [In reply to] Can't Post

It's probably because you are on netfirms, which has some strange setup from what other people have said.
-------------------------------------------------


passat
Novice

May 17, 2005, 10:27 AM

Post #13 of 13 (937 views)
Re: [KevinR] Fomatting report [In reply to] Can't Post

However, script creates main.log file, and weblog.pl show html page, but empty.

Probabaly, there are missing path to logfile in that code:

$mainlog = '/mnt/web_c/d31/s02/a000ras1/www/main.log';

it should be with path to logfile specified?



Code
 #!/usr/bin/perl  
# weblog.pl
# version 1.0
#
# read the file and format for display in a web browser


$mainlog = '/mnt/web_c/d31/s02/a000ras1/www/main.log';

print "Content-type: text/plain\n\n ";
print "<html><head><title>Web Report</title></head>\n";
print "<body bgcolor=\"#FFFFFF\">";
print "<h4>Web Report</h4>\n";
print "<pre style=\"font: normal #000000 'Courier New';\">";

open (MAINLOG, "$mainlog") or die "$!";
while (<MAINLOG>) {
print;
}
close (MAINLOG);
print "</pre></body></html>";
exit;



(This post was edited by passat on May 18, 2005, 4:42 AM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives