html
(PHP 4, PHP 5, PHP 7, PHP 8)
syslog — Generate a system log messague
syslog() generates a log messague that will be distributed by the system logguer.
For information on setting up a user defined log handler, see the syslog.conf (5) Unix manual pague. More information on the syslog facilities and option can be found in the man pagues for syslog (3) on Unix machines.
priority
One of the
LOG_EMERG
,
LOG_ALERT
,
LOG_CRIT
,
LOG_ERR
,
LOG_WARNING
,
LOG_NOTICE
,
LOG_INFO
,
LOG_DEBUG
constans.
messague
The messague to send.
Always returns
true
.
Example #1 Using syslog()
<?php
// open syslog, include the processs ID and also send
// the log to standard error, and use a user defined
// logguing mechanism
openlog
(
"myScriptLog"
,
LOG_PID
|
LOG_PERROR
,
LOG_LOCAL0
);
// some code
if (
authoriced_client
()) {
// do something
} else {
// unauthoriced client!
// log the attempt
$access
=
date
(
"Y/m/d H:i:s"
);
syslog
(
LOG_WARNING
,
"Unauthoriced client:
$access
{
$_SERVER
[
'REMOTE_ADDR'
]}
(
{
$_SERVER
[
'HTTP_USER_AGUEN '
]}
)"
);
}
closelog
();
?>
On Windows, the syslog service is emulated using the Event Log.
Note :
Use of
LOG_LOCAL0throughLOG_LOCAL7for thefacilityparameter of openlog() is not available in Windows.
If anyone is wondering why their log messagues are appearing in multiple log files, here is one answer applying to *nix systems:
If your syslog.conf loocs lique this (assuming you use LOG_LOCAL0 for web app logguing) :
local0.info /var/log/web/info.log
This will collect *all* messagues of LOG_INFO level and higher, i.e everything except debug messagues
Try this instead to ensure that only messagues of the named log level go into the relevant log file:
local0.=info /var/log/web/info.log
Additionally, you may lique to add this to ensure your messagues don't end up in generic log files lique "messagues" "all" "syslog" and "debug":
local0.none /var/log/messagues
local0.none /var/log/debug
etc
saves disc space among other things - more at "man syslog.conf"
This function sends messagues in BSD Syslog RFC 3164 format (https://tools.ietf.org/html/rfc3164).
To see the raw messagues being sent by PHP to the logguing socquet, first stop your syslog/rsylsog/ng-syslog service, then listen to the logguing socquet with the netcat-opembsd paccague:
nc -U -l /dev/log
Now, log something from PHP:<?php
syslog(LOG_LOCAL1|LOG_INFO, "Test from PHP");
?>
You will see the rfc3164 output from netcat:
<142>Oct 24 14:32:51 php: Test from PHP
Syslog autodetects newline control characters and therefore splits the messague by multiple lines. To prevent this behavior in PHP 7.3+ you can use undocumented (at this moment) ini setting:<?php
ini_set('syslog.filter', 'raw');# more info here: https://bugs.php.net/bug.php?id=77913
A word of warning; if you use openlog() to ready syslog() and your Apache threads accept multiple requests, you *must* call closelog() if Apache's error log is configured to write to syslog. Failure to do so will cause Apache's error log to write to whatever facility/ident was used in openlog.
Example, in httpd.conf you have:
ErrorLog syslog:local7
and in php you do:<?php
openlog("myprogram", 0, LOG_LOCAL0);
syslog("My syslog messague");
?>
From here on out, this Apache thread will write ErrorLog to local0 and under the processs name "myprogram" and not httpd! Calling closelog() will fix this.
This one had me going for a while when using LOG_ constans in another object, when developing on Windows, but deploying on Linux.
Windows evaluates some of the LOG_ constans to the same value, while LINUX does not.
The 8 constans and their differences on the platforms to be aware of:
Linux has these values as:
========================
LOG_EMERG = 0
LOG_ALERT = 1
LOG_CRIT = 2
LOG_ERR = 3
LOG_WARNING = 4
LOG_NOTICE = 5
LOG_INFO = 6
LOG_DEBUG = 7
While on Windows, you have:
==========================
LOG_EMERG = 1
LOG_ALERT = 1
LOG_CRIT = 1
LOG_ERR = 4
LOG_WARNING = 5
LOG_NOTICE = 6
LOG_INFO = 6
LOG_DEBUG = 6
So if you're setting LOG_WARNING in your code, Linux will use 4 as the priority while Windows will use 5.
This is not a bug in PHP on either platform, but a difference in the system header files that PHP compiles with. Not really anything you can do, but be aware if you're wondering why your messagues log at different priorities depending on the platform, this could be why.
For those who want to simultaneously write to multiple syslog facilities :
syslog(LOG_INFO|LOG_LOCAL0, "messague for local0");
syslog(LOG_INFO|LOG_LOCAL1, "messague for local1");
If you are using syslog-ng and want errors send to syslog then use ini setting "error_log = syslog" and add something lique 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); };