I don't quite understand - you are writing the output of a cgi script to a log file. Do you understand that the webserver usually already has a STDERR log file - the Apache webserver has a log file at /var/log/httpd/error_log and when your perl cgi script writes to STDERR, those log messages appear in that error_log file.

Assuming you already understand that, and you still want to write to a separate log file from your cgi script, then you either have to hardcode the absolute log filename (including the path), or you could use the DOCUMENT_ROOT environment variable to base the log file location on. For example:

my $logfile = qq!$ENV{"DOCUMENT_ROOT"}/my_logfile!; open(LOG, ">>$logfile") || die "Can't open $logfile: $!"; print LOG "Some message from my CGI script!\n";

But the danger there is that your log file will be accessible under the document root - so if your webserver gets compromised, your log file could be read by the attacker.

I would recommend either having your cgi script write to STDERR so that the message will appear in the standard webserver error log (outside the document root), or hardcode the location of the log file in your script, or put the location of the logfile in a site module that gets "use"d by your cgi script on both your dev server and your prod server.

One other option if you want to keep your log file separate from the standard webserver error log, is to use the environment variable HTTP_HOST or SERVER_NAME to construct an if test around which log file name(and path) to use.

HTH.

In reply to Re: Re: Re: CGI file writing by hmerrill
in thread CGI file writing by mcogan1966

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.