PHP 5.6.40 Released


(PHP 4, PHP 5, PHP 7)

syslogGénère un message dans l'historique système


syslog ( int $priority , string $message ) : bool

syslog() génère un message qui sera inscrit dans l'historique par le système.

Pour plus d'informations sur comment mettre en place un gestionnaire d'historique, reportez-vous au manuel Unix, page 5 syslog.conf (5). D'autres informations sur les systèmes d'historique et leurs options sont aussi disponibles dans le manuel syslog (3) des machines Unix.

Liste de paramètres


priority est une combinaison des valeurs d'accès et de niveau. Les valeurs possibles sont :

Priorités syslog() (en ordre décroissant)
Constante Description
LOG_EMERG système inutilisable
LOG_ALERT une décision doit être prise immédiatement
LOG_CRIT condition critique
LOG_ERR condition d'erreur
LOG_WARNING condition d'alerte
LOG_NOTICE condition normale, mais significative
LOG_INFO message d'information
LOG_DEBUG message de déboguage


Le message à envoyer. Attention : les caractères %m seront remplacés par l'erreur (sous forme de chaîne), présente dans errno.

Valeurs de retour

Cette fonction retourne TRUE en cas de succès ou FALSE si une erreur survient.


Exemple #1 Exemple avec syslog()

// ouverture de syslog, ajout du PID et envoi simultané du
// message à la sortie standard et à un mécanisme
// spécifique
openlog("myScriptLog"LOG_PID LOG_PERRORLOG_LOCAL0);

// quelques lignes de code

if (authorized_client()) {
// faire quelquechose
} else {
// client non autorisé!
    // notation de la tentative
$access date("Y/m/d H:i:s");
syslog(LOG_WARNING"Unauthorized client: $access {$_SERVER['REMOTE_ADDR']} ({$_SERVER['HTTP_USER_AGENT']})");



Avec Windows NT, l'historique est pris en charge par le log d'événements.


L'utilisation de LOG_LOCAL0 à LOG_LOCAL7 pour le paramètre facility de la fonction openlog() n'est pas disponible sous Windows.

Voir aussi

  • openlog() - Ouvre la connexion à l'historique système
  • closelog() - Ferme la connexion à l'historique système

add a note add a note

User Contributed Notes 16 notes

Antonio Lobato
8 years ago
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:

("myprogram", 0, LOG_LOCAL0);
syslog("My syslog message");

From here on out, this Apache thread will write ErrorLog to local0 and under the process name "myprogram" and not httpd!  Calling closelog() will fix this.
helly at php dot net
11 years ago
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); };
huangyg11 at gmail dot com
3 years ago
For those who want to simultaneously write to multiple syslog facilities :

syslog(LOG_INFO|LOG_LOCAL0, "message for local0");
syslog(LOG_INFO|LOG_LOCAL1, "message for local1");
james dot ellis at gmail dot com
11 years ago
If anyone is wondering why their log messages are appearing in multiple log files, here is one answer applying to *nix systems:

If your syslog.conf looks like this (assuming you use LOG_LOCAL0 for web app logging) :    /var/log/web/info.log

This will collect *all* messages of LOG_INFO level and higher, i.e everything except debug messages

Try this instead to ensure that only messages of the named log level go into the relevant log file:

local0.=info    /var/log/web/info.log

Additionally, you may like to add this to ensure your messages don't end up in generic log files like "messages"  "all" "syslog" and "debug":

local0.none    /var/log/messages
local0.none    /var/log/debug

saves disk space among other things - more at "man syslog.conf"
antoine dot leverve dot EXT at zodiacaerospace dot com
3 years ago
The documentation is incorrect when it says "Priorities (in descending order)", as the table that follows is actually in **ascending** order.

For example my output says:

An important difference, that caused me some pain!
stevekamerman at gmail dot com
2 months ago
This function sends messages in BSD Syslog RFC 3164 format (

To see the raw messages being sent by PHP to the logging socket, first stop your syslog/rsylsog/ng-syslog service, then listen to the logging socket with the netcat-openbsd package:

nc -U -l /dev/log

Now, log something from PHP:

(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
daniele dot patoner at biblio dot unitn dot it
15 years ago
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:

openlog("TextLog", LOG_PID, LOG_LOCAL0);

$data = date("Y/m/d H:i:s");
syslog(LOG_DEBUG,"Messagge: $data");

gregj at pdxperts dot com
15 years ago
The message string sent to the log file is limited to 500 characters.
dpreece at paradise dot net dot nz
16 years ago
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

*.*   {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

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
nospam \100 jraxis com (that's right!)
16 years ago
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.
mavetju at chello dot nl
17 years ago
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:
rcgraves+php at brandeis dot edu
18 years ago
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.
bb at lb-data dot co dot at
19 years ago
In Windows NT, use the following values of priority:
1 = error,
6 = info
14 years ago
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.
adam _at_ lockdownnetworks _dot_ com
11 years ago
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:



syslog(LOG_INFO, 'UTC Log line');

syslog(LOG_INFO, 'US/Pacific Log line');



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

gherson at snet dot net
18 years ago
Example of where to look for syslog's output:   /var/log/httpd/access_log
(on Red Hat Linux Secure Server v6.2).
To Top