Custom error log

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Custom error log

Post by mikusan »

Hi, i have recently been cracking my head at making my own error function, and although i feel i have it all right, it is not exactly behaving as though "allright" :) well... here is the code...if someone has suggestions or can see a flaw in my code...

Code: Select all

function site_log($error)
{
	if ($file = fopen('/home/mysite/site_log.txt' , 'a'))
	{
		//Display Date/Time
		$dt = getdate(); 
		$month = $dt['month']; 
		$wday = $dt['wday']; 
		$year = $dt['year']; 
		$hours = $dt['hours']; 
		$minutes = $dt['minutes']; 
		$seconds = $dt['seconds']; 

		$date = '[' . $year . '-' . $month .'] - ' . $wday . ' - ' . $hours . ' '
				. $minutes . ' ' . $seconds . ' -> ';
		fwrite($file, $date . $error . '\n');
		fclose($file);
	}

	else
	{
		return(false);
	}
}
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

BTW... The file in question has CHMOD 664 while the folder 755...
User avatar
DoraBear
Forum Newbie
Posts: 3
Joined: Tue May 20, 2003 10:10 am
Location: China

Post by DoraBear »

That's good, but this code:

Code: Select all

<?php
//Display Date/Time 
      $dt = getdate(); 
      $month = $dt['month']; 
      $wday = $dt['wday']; 
      $year = $dt['year']; 
      $hours = $dt['hours']; 
      $minutes = $dt['minutes']; 
      $seconds = $dt['seconds']; 

      $date = '[' . $year . '-' . $month .'] - ' . $wday . ' - ' . $hours . ' ' 
            . $minutes . ' ' . $seconds . ' -> '; 

?>
can be replaced by "date()" function,
which can work better.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

That's not the problem, it does not seem to write to the file, besides i wanted a customized output for the date function which i don't think is covered by just the date().

Although it does not write to the designated file still the idea is that all errors are logged in the manner
[Year - Month] - Weekday - Hours Minutes Seconds ->

I am not concerned with that just yet...i need to be able to see at least one log so that i can confirm my site_log function actually works...this has not been the case yet.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Your function is working.

sitelog.php:

Code: Select all

<?php
function site_log($error) {
   if ($file = fopen('site_log.txt' , 'a')) { // note I changed this line, creating the file in the sitelog.php root dir
//... 
}
site_log('This should be sent to file.');
?>
The script runs without error and creates the site_log.txt file:

Code: Select all

&#1111;2003-May] - 2 - 18 23 24 -&gt; This should be sent to file.\n
Note the \n at the end of the line. \n is parsed when it is between " like this "\n".

I think there is a permission problem. You said the file is 664 and the directory 755. So only the directory owner has write permission to create files in this directory.

Some answers should help:
- Which user/group your server runs as? (it is commonly nobody/nobody)
- Does this user have write premission to '/home/mysite/site_log.txt'?
- Do you think there is another thing that matters?

Regards,
Scorphus.

ps.: please excuse my english.
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Awkward like many things on my website it fixed itself... but anyone know why the "\n" doen't quite work when it's written to the file... i mean it wrote it in as \n but no line break

on another note anyone know a good CHMOD tutorial available online... there are many but one that is worthwile reading and learning from...i am concerned with the good ways to do it that are secure at the same time ...thx
User avatar
mikusan
Forum Contributor
Posts: 247
Joined: Thu May 01, 2003 1:48 pm

Post by mikusan »

Oh i see, it doesn't like single quotes for the line breaks '\n'...hmmm weird...

Anyone know good sites?
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Single quotes and double quotes work differently:

Code: Select all

<?php
echo ',\n'; // print ,\n
echo ",\n"; // print , and add a new line
$var = 'PHP rocks';
echo ',\n$var';
echo ",\n$var";
$var2 = ',\n$var -> just \$var';
echo $var2;
$var2 = ",\n$var -> \$var contents"; // note the \ to scape $
echo $var2;
?>
Above code's output:

Code: Select all

,\n,
,\n$var,
PHP rocks,\n$var -&gt; just \$var,
PHP rocks -&gt; $var contents
About chmod I think you could check out this article: http://en.tldp.org/linuxfocus/English/J ... cle77.html

I hope it helps. Let us know if it doesn't.
Cheers,
Scorphus.
Post Reply