Using PHP to write to logs

software development

#1

Hey Guys

I am trying to get PHP to write to my logs using syslog() but whenever I check the access or the error log nothing is outputted! When I run the script on my local machine using wamp, it works fine.

Anyone know what i must do to get this to work?

I only have access and error logs in my logs folder, am I looking in the wrong place?

Thanks very much for your help!


#2

What code are you using? You’d have to specify to print the messages to STDERR and probably use LOG_USER /w openlog to get messages to appear in error.log

Customer since 2000 :cool: openvein.org


#3

I have looked around on the internet for many ways of doing this and have been unlocky so far, the script that I have used most recently is:

echo openlog('PHP_Err: ', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER)
or die (‘Syslog could not be opened’);

syslog ( LOG_INFO , “GO ON PRINT SOMETHING!!” );
closelog();


#4

[quote]I have looked around on the internet for many ways of doing this and have been unlocky so far, the script that I have used most recently is:

echo openlog('PHP_Err: ', LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER)
or die (‘Syslog could not be opened’);

syslog ( LOG_INFO , “GO ON PRINT SOMETHING!!” );
closelog();[/quote]
…and thats not going to work. First of all you overlooked the fact that this logs messages using the system logger - this is a special operating system service - and those log files are not available to regular users.

It might be wise to read the PHP documentation. The function you want to use is actually:

error_log — Send an error message somewhere

You can either use 0 to have the message sent to STDERR (and appear in ~/logs/domain/http/error.log) or specify a file of your own.

Customer since 2000 :cool: openvein.org


#5

Hi

I did try to use error log with param 0 but it failed to work. I have now installed my own custom php.ini file in my setup and enabled error_log=syslog.

Now all errors are being printed to the error.log!!

Thanks for your time


#6

In order to get error messages into error.log, they just have to be written to the STDERR file handle.

You could have done that using this:

openlog('PHP_Err: ', LOG_PERROR | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER)

Where LOG_PERROR indicates to also write the message to STDERR in addition to the system logger.

Keep in mind the system logger is a separate daemon running on the server and keeps its own log files. So when you use it, it writes to log files in the /var/log directory and in this case your error messages will be written in two different places. It won’t break your script doing it this way, and only the root user can read the /var/log/user.log file as well.

Customer since 2000 :cool: openvein.org