Jump to content

writing error log but I'm not sure how to organize it


Recommended Posts

sigh can't believe I'm here asking again after my stupid mistake last night....I wonder if it's another stupid mistake I'm having again and over thinking....

 

 

I have two functions doing preg_match to check if the emp# and the email is valid. If one or both is not valid it will be printed to an error.log but I want to organize it in a way humm....let me see if I know how to example as simple as possible.

Let's say if the emp# is not valid then the error log will show

date()
emp# 

if the email is not valid then the error log will show

date()
emp# 

if both are not valid then the error log will show

date()
emp# 

The thing is when both happens I don't want it to print the date twice or more than one emp# or email is not valid the date will not repeat will only print like

date()
emp#
emp#
emp$ 

hopefully my explanation make sense...what I have now is

	if(((isEmailAddressWellFormed($column[3]) == false)) && ((isStudentNumberWellFormed($column[0]) == false)))
{
	$filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
	fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
	fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
	fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
	fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
	fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
	fclose($filehandle);		
}
else
{
if(isEmailAddressWellFormed($column[3]) == false)
{
	$filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
	fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
	fwrite($filehandle, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
	fwrite($filehandle, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
	fclose($filehandle);
}

if(isStudentNumberWellFormed($column[0]) == false)
{
	$filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
	fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
	fwrite($filehandle, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
	fwrite($filehandle, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
	fclose($filehandle);
}
} 

but I know it's not getting what I want....any simple way I can make it happen?

On way you could do it is to nest your if statements inside a bigger if statement.

 

This bigger one checks all the conditions, and if any of them are true, then we move into the if code block, print the date to the error log, then use the if statements you already have to print whatever error(s) there are.,

 

This would mean the date would only be put in there once.

 

Does that make sense?

 

Denno

On way you could do it is to nest your if statements inside a bigger if statement.

 

This bigger one checks all the conditions, and if any of them are true, then we move into the if code block, print the date to the error log, then use the if statements you already have to print whatever error(s) there are.,

 

This would mean the date would only be put in there once.

 

Does that make sense?

 

Denno

 

Hi Denno it's you again :P thanks but but....ah...I know what you mean...but I don't know what's wrong with my mind again...because I'm thinking what you are thinking too but I don't know why I kept on messing it up arg....

I have this now....

 

$fileContents = file("./courses/" . $_GET["filename"]);
foreach($fileContents as $row)
{
$column = preg_split("/,/", $row);
if(!(isEmailAddressWellFormed($column[3])) | !(isStudentNumberWellFormed($column[0])))
{
echo "error";
$error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
if(!(isEmailAddressWellFormed($column[3])))
{
fwrite($error_log, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
fwrite($error_log, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
}
elseif(!(isStudentNumberWellFormed($column[0])))
{
fwrite($error_log, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
fwrite($error_log, "$column[2] $column[1] $column[0]\n" . PHP_EOL);
}
fclose($error_log);
}


}//closing foreach

 

this is what I'm having now...omg...I don't know why I can't get out of my box these days and think more broad....

Edited by et4891

Your first if statement would need two pipes || for the or, you've got just the one.. That could be a problem

 

same :(

 

Starting to wonder if I should stop learning this.....fun to do it but sucks when getting stuck

Edited by et4891

What error are you getting then? Have you used some echo's or var_dumps to check that you're actually entering the if code blocks?

 

oh oh...I'm not getting errors...but like if I have the contents of

 

a00123456aa,michelle,jason,comp1920@telus.net

a00456789dd,baloney,tony,t_baloney@shaw.ca

a00111111,lasty,firsty,e@mail.ca

 

which means the first two will be having error since last night with the preg_match thingie....ya then the print out in error.log should be like

 


February 28, 2013 (02:16:01 am)
Improper student numbers from comp1920.txt :
jason michellea00123456aa

Improper student numbers from comp1920.txt :
tony baloney a00456789dd

 

but instead I'm getting two dates like this

 

February 28, 2013 (02:16:01 am)
Improper student numbers from comp1920.txt :
jason harrison a00123456aa
February 28, 2013 (02:16:01 am)
Improper student numbers from comp1920.txt :
tony baloney a00456789dd

 

I just realized it's because it's inside my foreach so each time it checks....it checks one row of the contents then the next maybe that's why it's printing the dates again....but where else outside foreach can I put for the date though.....since I need foreach to check the contents too...the if will sure be inside the foreach isn't it?

 

*hope I know what I'm talking about and makes sense*

a simple way could be to just use a flag.. So outside of the foreach, set $datePrinted = false,

 

Then inside the foreach, and inside an error detecting if, you can do this:

if(!$datePrinted){
 //print the date to file etc
 $datePrinted = true;
}

 

That way the date will only be printed once, no matter how many times the foreach loop runs :).

 

Denno

Edited by denno020

but but....my code is actually like this...

where should I put the flag you said and give it to try...

 

if(!(file_exists("./courses/" . $_GET["filename"])))
{
echo "no such file, sorry";
die();
}
else
{
$fileContents = file("./courses/" . $_GET["filename"]);
foreach($fileContents as $row)
{
 $column = preg_split("/,/", $row);
 if(!(isEmailAddressWellFormed($column[3])) || !(isStudentNumberWellFormed($column[0])))
 {
  echo "error";
  $error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
  fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
  if(!(isEmailAddressWellFormed($column[3])))
  {
  fwrite($error_log, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
  fwrite($error_log, "$column[2] $column[1] $column[3]\n" . PHP_EOL);
  }
  if(!(isStudentNumberWellFormed($column[0])))
  {
  fwrite($error_log, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
  fwrite($error_log, "$column[2] $column[1] $column[0]\n" . PHP_EOL);  
  }
  fclose($error_log);
 }


}//closing foreach

}//closing else

so like this?

 

$datePrinted = false;
foreach($fileContents as $row)
{
$column = preg_split("/,/", $row);

if(!(isEmailAddressWellFormed($column[3])) || !(isStudentNumberWellFormed($column[0])))
{
echo "error";
if(!$datePrinted){
$error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
$datePrinted = true;
}

 

because this gives me the error of

Warning: fwrite(): 5 is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\eee.php on line 50
Warning: fwrite(): 5 is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\eee.php on line 51
Warning: fclose(): 5 is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\test\eee.php on line 53

 

eyes is starting to hurt again hahaha...

Edited by et4891

Put the fopen as the first thing inside your foreach.

 

The error is appearing because the second time that the foreach loops, the file has been closed (at the end of the first loop), but won't be opened again because $datePrinted is set to true, so it won't go into that if block that contains fopen.

 

Fingers crossed that will fix all problems :)

 

Denno

someone gave me this...which worked...but I don't want to use someone's script esp if I can't totally understand it....

 

$errors = array();
foreach($fileContents as $row)
{
 $column = preg_split("/,/", $row);

if(!isEmailAddressWellFormed($column[3]))
{
   $errors[] = "Improper email address from " . $_GET["filename"] . " :";
   $errors[] = "$column[2] $column[1] $column[3]";
}
if(!isStudentNumberWellFormed($column[0]))
{
   $errors[] = "Improper student numbers from " . $_GET["filename"] . " :";
   $errors[] = "$column[2] $column[1] $column[0]";
}
}//closing foreach

if (!empty($errors))
{
   $filehandle = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
   fwrite($filehandle, date("F t, Y (h:i:s a)") . PHP_EOL);
   fwrite($filehandle, implode(PHP_EOL, $errors));
   fclose($filehandle);
}

Sorry put it as the first thing inside your first if error check.. outside of the "if date not set, set it" if thing

 

O.o a bit confused what you are saying here...

 

but this is what I did...

$datePrinted = false;
foreach($fileContents as $row)
{
 $column = preg_split("/,/", $row);
 $error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
 if(!(isEmailAddressWellFormed($column[3])) || !(isStudentNumberWellFormed($column[0])))
 {
  echo "error";
  if(!$datePrinted){
  fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
  $datePrinted = true;
  }
  if(!(isEmailAddressWellFormed($column[3])))
  {
  fwrite($error_log, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
  fwrite($error_log, "$column[2] $column[1] $column[3]" . PHP_EOL);
  }
  if(!(isStudentNumberWellFormed($column[0])))
  {
  fwrite($error_log, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
  fwrite($error_log, "$column[2] $column[1] $column[0]" . PHP_EOL);  
  }
 }


}//closing foreach
  fwrite($error_log, "=============================" . PHP_EOL);  
  fclose($error_log);

oh wait O.o if I don't want the error to print out like 10 times if there are 10 errors....I can use the flag as you used earlier right?

I don't understand this? You'll want an error printed for each line there is an error wouldn't you?

Anyway, this code should do it for you:

$datePrinted = false;
$error_log = fopen("./courses/path/error.log","a+") or die ("File can not be opened");
foreach($fileContents as $row){
$column = preg_split("/,/", $row);
if(!(isEmailAddressWellFormed($column[3])) || !(isStudentNumberWellFormed($column[0]))) {
 echo "error";
 if(!$datePrinted){
  fwrite($error_log, date("F t, Y (h:i:s a)") . PHP_EOL);
  $datePrinted = true;
 }
if(!(isEmailAddressWellFormed($column[3]))){
 fwrite($error_log, "Improper email address from " . $_GET["filename"] . " :" . PHP_EOL);
 fwrite($error_log, "$column[2] $column[1] $column[3]" . PHP_EOL);
}
if(!(isStudentNumberWellFormed($column[0]))) {
 fwrite($error_log, "Improper student numbers from " . $_GET["filename"] . " :" . PHP_EOL);
 fwrite($error_log, "$column[2] $column[1] $column[0]" . PHP_EOL);
}
}
}//closing foreach
fwrite($error_log, "=============================" . PHP_EOL);
fclose($error_log);

 

Give that a try

You should not output those to your error log instead output them to some other file as the error log is used by many other things aswell and it will become cluttered and really hard for you to retrieve the information.

 

PS Don't be too hard on yourself, experience comes from success and failure.

Denno: Thanks for all the help these two days...sigh still another part needs to be done darn it...that works even though another problem I just noticed *arg* let me put the problem below with another reply...also I want the error to show once because I'm told to put out a link of the error.log file so if there's an error the link will show. And so because of that requirement, if I put out ten links saying error error x 10 and all of them are linking to the same file then ya...you know what I mean ^_^

 

ignace: Thanks for the support...I do agree but just these two days just feels I bothered denno for so long trying to figuring out something I think is really easy to you guys. Also error.log because I'm told to use it and fwrite thing that's all I learned for now haha...but thanks for the comment :P Learned something new even though I can't understand it too well but not hard to guess ^_^

 

P.S. you guys are way more lovely than people in stackoverflow haha....

great :P so now in my error.log it shows...something like

 

 

 

February 28, 2013 (03:06:22 am)
Improper email address from comp1920.txt :
jason harrison comp1920@telus.netddddd

Improper student numbers from comp1920.txt :
jason harrison a00123456aa
Improper email address from comp1920.txt :
tony baloney t_baloney@shaw.caasdf

Improper student numbers from comp1920.txt :
tony baloney a00456789dd
Improper email address from comp1920.txt :
firsty lasty e@mail.caasdf
Improper student numbers from comp1920.txt :
firsty lasty a00111111asdf
=============================

 

I believe that's what I want (eyes really hurts) except the spaces...how can I get ride of them?

 

oh but another arg...problem is...after I uploaded it to a server...when I view the error.log...it actually shows...no line breaks...nothing at all...so the info just goes in one crazy line

*wait...is this what Ignace mean???*

oh but if I created a link to error.log it seems fine just that there are extra line breaks which I don't know why....but if I open error.log to view it in the server it's like crazy.....

 

something like

February 28, 2013 (03:20:39 am)Improper email address from comp1920.txt :jason harrison comp1920@telus.netdddddImproper student numbers from comp1920.txt :jason harrison a00123456aa

Edited by et4891

At the end of each strings that you fwrite to the error log, add '\n' (no quotes). This is a new line character. Hopefully this will translate over to the browser as well, but if not, then you'll need to use '<br>' instead.

 

And as for printing the link to the error log, I wasn't aware that this was something extra that had to be done, but you could do it after the foreach loop, and use the $datePrinted variable again. So if $datePrinted == true, then that means there must be been an error, as the date had to be printed, so echo the link to the error file. Again, this if statement will be outside of the foreach loop (after it to be exact).

 

Hope that helps?

 

Denno

At the end of each strings that you fwrite to the error log, add '\n' (no quotes). This is a new line character. Hopefully this will translate over to the browser as well, but if not, then you'll need to use '<br>' instead.

 

And as for printing the link to the error log, I wasn't aware that this was something extra that had to be done, but you could do it after the foreach loop, and use the $datePrinted variable again. So if $datePrinted == true, then that means there must be been an error, as the date had to be printed, so echo the link to the error file. Again, this if statement will be outside of the foreach loop (after it to be exact).

 

Hope that helps?

 

Denno

hehe :P already done that for the error ^_^ that way of doing thing really helps...I gotta remember it in my head....it's just weird a friend showed me his script....I don't see much difference but he said his works fine...*I couldn't check since our variables are different*

 

also >.<" don't know why \n and <br> doesn't work....weird...but I guess if in browser it didn't read it that way it's better than nothing at the moment...just weird why in browser there are extra line breaks....

ah....I created one txt with more contents I figured there's an extra break if there's an error to email but no extra break after student # let me try adding \n at the end of student and see if it'll make it easier to read

 

the \n is funny haha O.o

 

just here to share....nothing major at least the script works....I guess I'll start the next part tomorrow...arg..

 

the \n

 

if I didn't put \n in any of those lines....it prints out like..

 

February 28, 2013 (03:46:50 am)
Improper email address fromcomp0101.txt :
jason harrison comp1920@telus.netasdf

Improper student numbers fromcomp0101.txt :
firsty lasty a00111111a
Improper email address fromcomp0101.txt :
jason harrison comp1920@telus.netasdf

Improper email address fromcomp0101.txt :
tony baloney t_baloney@shaw.caasdf
Improper email address fromcomp0101.txt :
firsty lasty e@mail.caasdf

Improper email address fromcomp0101.txt :
tony baloney t_baloney@shaw.caasdf

Improper email address fromcomp0101.txt :
firsty lasty e@mail.caasdf
=============================

 

and then if I put \n at the end of the fwrite code for both if it'll be like....

 

February 28, 2013 (03:46:50 am)
Improper email address fromcomp0101.txt :
jason harrison comp1920@telus.netasdf


Improper student numbers fromcomp0101.txt :
firsty lasty a00111111a

Improper email address fromcomp0101.txt :
jason harrison comp1920@telus.netasdf

Improper email address fromcomp0101.txt :
tony baloney t_baloney@shaw.caasdf


Improper email address fromcomp0101.txt :
firsty lasty e@mail.caasdf


Improper email address fromcomp0101.txt :
tony baloney t_baloney@shaw.caasdf


Improper email address fromcomp0101.txt :
firsty lasty e@mail.caasdf
=============================

 

it's always the last two lines seem to line break better haha....

Edited by et4891
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.