writing log files in PHP
Moderator: General Moderators
-
davidklonski
- Forum Contributor
- Posts: 128
- Joined: Mon Mar 22, 2004 4:55 pm
writing log files in PHP
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
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
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:
Does this help?
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
}- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
uh..
Code: Select all
<?php
echo PHP_OS;
?>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
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?
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?
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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 workfeyd 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'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.
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
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
I believe you would use [php_man]set_error_handler[/php_man]().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
You may need to experiment here, but I think it would work across the entire page load..Also, do I need to set it just once or at the beginning of every PHP file?
thanks
-
davidklonski
- Forum Contributor
- Posts: 128
- Joined: Mon Mar 22, 2004 4:55 pm