Page 1 of 1

writing log files in PHP

Posted: Tue Jul 20, 2004 5:34 pm
by davidklonski
Hello

I have written my own logging mechanism that allows me to send debug messages to a local file.
That worked fine on Windows.

I am now moving my application to a Linux environment and would like to know how can I write my code so that it will work seamlessly on both operating system.
Each operating system defines its file structure differently.

Can I check on what operating system I am running and refer the debug message to the appropriate log file?

Or maybe there is a known (not to me) logging system in PHP that I could utilize. That system would generate log files regardless of the operating system it is used on.

regards
David

Posted: Tue Jul 20, 2004 5:42 pm
by feyd

Posted: Tue Jul 20, 2004 7:39 pm
by nigma
If you decide you want to use your own logging functions then one way you could determine what os your working with is to check for the existence of a file that is pretty much always in the same place on a certain operating system.

For example, say you want to figure out whether you're on a unix/linux system or a windows system. The following might help:

Code: Select all

if (file_exists('/etc/passwd')) {
  // you're dealing with a unix/linux system
}
else {
  // you're dealing with a non unix/linux system
}
Does this help?

Posted: Tue Jul 20, 2004 7:53 pm
by feyd
uh..

Code: Select all

<?php

echo PHP_OS;

?>

Posted: Tue Jul 20, 2004 9:24 pm
by Joe

Code: Select all

<?php
if (PHP_OS == "WIN32" || PHP_OS == "WINNT") 
{
 //Windows systems!
} 
else 
{
 //Non Windows systems!
}
?>

Posted: Tue Jul 20, 2004 11:58 pm
by davidklonski
I am not talking about error logging but about debug logging.
for example, sending to the log file comments or a database query before it is sent to the database...

maybe I could utilize the error log for that, but I would like to keep them separate.

Also, assuming I know what operating system I am on, how can I make sure I have permission to write to a file?
I want the logs to be generated no matter where the application is deployed. This means that the file structure on which the application resides may be different from one deployment to another. How can I trust that a certain file or file directory will exist?

Posted: Wed Jul 21, 2004 12:01 am
by feyd
you can write anything you wish to the error logs..

If you use the error logging system, you are fairly guaranateed it'll be there.. You could have an option to drop logs to a database..

Posted: Wed Jul 21, 2004 12:15 am
by John Cartwright
feyd wrote:you can write anything you wish to the error logs..

If you use the error logging system, you are fairly guaranateed it'll be there.. You could have an option to drop logs to a database..
I think a database would be far more suitable than a flatfile because db's can be easily be manipulated to output very pretty stats but flatfiles take a little more work :|

Posted: Wed Jul 21, 2004 12:25 am
by infolock
i'd have to agree there.. when i built my windows media log file reporter, i had to decide wether or not i wanted to just grab that data from the file, or just dump it all into a db and sort it quicker/easier with fields, inshtead of having to read char by char in a flatfile.

dumping to a db was by far the easiest.

so phenom hit the head on the nail. just throwing whatever it is you want into a db would not only be nicer looking, but faster, nad easier to work with.

Posted: Wed Jul 21, 2004 8:22 am
by davidklonski
how can I set the file that the errors will be sent to?
I know that I can set it in php.ini, but I would like it be set in runtime based on the operating system

Also, do I need to set it just once or at the beginning of every PHP file?
thanks

Posted: Wed Jul 21, 2004 12:10 pm
by feyd
davidklonski wrote:how can I set the file that the errors will be sent to?
I know that I can set it in php.ini, but I would like it be set in runtime based on the operating system
I believe you would use [php_man]set_error_handler[/php_man]().
Also, do I need to set it just once or at the beginning of every PHP file?
thanks
You may need to experiment here, but I think it would work across the entire page load..

Posted: Wed Jul 21, 2004 1:26 pm
by davidklonski
ok, but if it is defined in page A, and the user goes from page A to page B, do I need to redefine it in page B?

Posted: Wed Jul 21, 2004 1:31 pm
by feyd
I believe so, yes.