writing log files in PHP

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
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

writing log files in PHP

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uh..

Code: Select all

<?php

echo PHP_OS;

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Code: Select all

<?php
if (PHP_OS == "WIN32" || PHP_OS == "WINNT") 
{
 //Windows systems!
} 
else 
{
 //Non Windows systems!
}
?>
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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 :|
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post 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.
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
davidklonski
Forum Contributor
Posts: 128
Joined: Mon Mar 22, 2004 4:55 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I believe so, yes.
Post Reply