Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Network Functions : syslog

syslog

Generate a system log message (PHP 4, PHP 5)
bool syslog ( int priority, string message )

syslog() generates a log message that will be distributed by the system logger.

For information on setting up a user defined log handler, see the syslog.conf(5) Unix manual page. More information on the syslog facilities and option can be found in the man pages for syslog(3) on Unix machines.

Parameters

priority

priority is a combination of the facility and the level. Possible values are:

Table207.syslog() Priorities (in descending order)

Constant Description
LOG_EMERG system is unusable
LOG_ALERT action must be taken immediately
LOG_CRIT critical conditions
LOG_ERR error conditions
LOG_WARNING warning conditions
LOG_NOTICE normal, but significant, condition
LOG_INFO informational message
LOG_DEBUG debug-level message


message

The message to send, except that the two characters %m will be replaced by the error message string (strerror) corresponding to the present value of errno.

Return Values

Returns TRUE on success or FALSE on failure.

Examples

Example1612.Using syslog()

<?php
define_syslog_variables
();
// open syslog, include the process ID and also send
// the log to standard error, and use a user defined
// logging mechanism
openlog("myScriptLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);

// some code

if (authorized_client()) {
// do something
} else {
// unauthorized client!
// log the attempt
$access = date("Y/m/d H:i:s");
syslog(LOG_WARNING, "Unauthorized client: $access {$_SERVER['REMOTE_ADDR']} ({$_SERVER['HTTP_USER_AGENT']})");
}

closelog();
?>


Notes

On Windows NT, the syslog service is emulated using the Event Log.

Note:

Use of LOG_LOCAL0 through LOG_LOCAL7 for the facility parameter of openlog() is not available in Windows.

Related Examples ( Source code ) » syslog


Code Examples / Notes » syslog

mavetju

With FreeBSD I can use: syslog(LOG_INFO,"test");
BSD/OS does not support this, I had to use the literal values for the priority (158: local3.info):
syslog(158,"test");


dpreece

To set up a custom log file via the syslog daemon (FreeBSD in this case)...
Add to /etc/syslog.conf a line that says all errors from the httpd process are to go to a file called (for example) /var/log/httpd-php.log
!httpd
*.*   {tab}   /var/log/httpd-php.log
Note the tab, being a tab character! Next create a blank file to be written to. I'm sure there are 1e+6 ways to do this, but I choose
# cat > httpd-php.log << EOF
? EOF
Finally find your syslog daemon and send it a sighup to inform it of the change:
# ps ax | grep syslogd
 133  ??  Ss     0:07.23 syslogd -s
# kill -1 133
Et voila! Php syslog calls will now arrive in /var/log/httpd-php.log


daniele dot patoner

This work for me, to redirect  logs to a separate syslog file
put this line in your /etc/syslog.conf :
local0.debug   /var/log/php.log
Then restart syslogd:
/etc/init.d/syslog restart
php example:
<?php
define_syslog_variables();
openlog("TextLog", LOG_PID, LOG_LOCAL0);
$data = date("Y/m/d H:i:s");
syslog(LOG_DEBUG,"Messagge: $data");
closelog();
?>


nospam \100 jraxis com that's right!

This *does* actually goto the system log as configured in /etc/syslog.conf (such as /var/log/messages), it doesn't goto Apache's ErrorLog (such as /var/log/httpd/error_log). At least under my Debian Potato with Apache 1.3.23.
Use error_log() to be sure it gets into Apache's ErrorLog.


fwo

The text was taken directly out of a unix man page. On a unix system do man 3 syslog and you wil get the info

gregj

The message string sent to the log file is limited to 500 characters.

bb

In Windows NT, use the following values of priority:
1 = error,
6 = info


monte

If you have php.ini setup to send PHP errors to syslog, they will all get dumped into /var/log/messages (at least it does with RedHat 9 by default). I wanted to figure out how to get PHP errors to go to their own syslog file.
After some trial and error, I figured out what facility and priority PHP uses, which is "user.notice". So, to get your PHP errors going to a separate syslog file, put this line in your /etc/syslog.conf :
user.notice   /var/log/php.log
Then restart syslogd:
/etc/init.d/syslog restart
Why PHP uses "user" as the facility I'm not sure, probably because it's the only one that works under Windows?
Monte


helly

If you are using syslog-ng and want errors send to syslog then use ini setting "error_log = syslog" and add something like the following to your syslog-ng.conf:
destination php { file("/var/log/php.log" owner(root) group(devel) perm(0620)); };
log { source(src); filter(f_php); destination(php); };


torsten

I had a problem trying to issue a syslog message with IIS 5.1 under Windows XP. The function call seemed to succeed, but the event viewer showed that no entry was made.
Finally I found out that the user account used for the webserver (IUSR_<Computername>) did not have enough permissions to issue syslog alerts. I changed this by adding this user to the Users group instead of only Guest.


rcgraves+php

For the-header-file-enabled:
man 3 syslog defines the priorities, but not the integer values. For that you'll need to read your system header file.
Let's suppose I want to log an informational message in the mail log (which happens to be true). The man page tells me I want LOG_MAIL|LOG_INFO. So I look in /usr/include/sys/syslog.h and find (this happens to be Linux, your system could be different):
#define LOG_INFO        6       /* informational */
#define LOG_MAIL        (2<<3)  /* mail system */
2<<3 means shift 3 bits left, which means multiply by 8. So I want 2*8 + 6 = 22. syslog(22,"this message will appear in the mail log"); And indeed it does.


gherson

Example of where to look for syslog's output:   /var/log/httpd/access_log
(on Red Hat Linux Secure Server v6.2).


adam _at_ lockdownnetworks _dot_ com

Be aware when using syslog() that if you set the timezone of environment to be something other than the standard, syslog() may log the time to the log(s) with the wrong time zone information. For example:
<?php
openlog('mylog', LOG_PID | LOG_ODELAY,LOG_LOCAL4);
putenv('TZ=UTC');
syslog(LOG_INFO, 'UTC Log line');
putenv('TZ=US/Pacific');
syslog(LOG_INFO, 'US/Pacific Log line');
closelog();
?>
Viewing the /usr/log/messages log will display these two lines:
Apr 11 01:25:39 hostname mylog[1400]: UTC Log line
Apr 10 18:25:39 hostname mylog[1400]: US/Pacific Log line
Adam.


Change Language


Follow Navioo On Twitter
checkdnsrr
closelog
debugger_off
debugger_on
define_syslog_variables
dns_check_record
dns_get_mx
dns_get_record
fsockopen
gethostbyaddr
gethostbyname
gethostbynamel
getmxrr
getprotobyname
getprotobynumber
getservbyname
getservbyport
header
headers_list
headers_sent
inet_ntop
inet_pton
ip2long
long2ip
openlog
pfsockopen
setcookie
setrawcookie
socket_get_status
socket_set_blocking
socket_set_timeout
syslog
eXTReMe Tracker