syslog

(PHP 4, PHP 5, PHP 7, PHP 8)

syslogシステムログのメッセージを生成する

説明

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

syslog() はシステムログが出力するログメッセージを生成します。

ユーザー定義のログハンドラの設定に関する情報については、Unix マニュアルの syslog.conf (5) を参照ください。 syslog の facility と option に関するより詳細な情報は、 Unix マシンの syslog (3) にあります。

パラメータ

priority

priority は、容易さ (facility) とレベル (level) の組み合わせです。以下の値が使用できます。

syslog() の優先順位 (降順)
定数 説明
LOG_EMERG システムは使用不可
LOG_ALERT アクションを直ちにおこす必要がある
LOG_CRIT 致命的な条件
LOG_ERR エラーを発生する条件
LOG_WARNING 警告を発生する条件
LOG_NOTICE 通常の動作だが、特徴的な条件
LOG_INFO 情報を与えるメッセージ
LOG_DEBUG デバッグ用のメッセージ

message

送信するメッセージ

戻り値

常に true を返します。

例1 syslog() の使用例

<?php
// syslogをオープンし、プロセスIDをインクルードし、標準エラー出力にも
// ログを出力します。そして、ユーザー定義のログ記録機構を使用します。
openlog("myScriptLog", LOG_PID | LOG_PERROR, LOG_LOCAL0);

// 何らかのコード

if (authorized_client()) {
// 何かをする
} else {
// クライアントは未認証!
// ログを記録する
$access = date("Y/m/d H:i:s");
syslog(LOG_WARNING, "Unauthorized client: $access {$_SERVER['REMOTE_ADDR']} ({$_SERVER['HTTP_USER_AGENT']})");
}

closelog();
?>

注意

Windows では、syslog サービスはイベントログを使用してエミュレートされます。

注意:

Windows 環境では、openlog()facility パラメータに LOG_LOCAL0 から LOG_LOCAL7 までを使用することはできません。

参考

  • openlog() - システムのロガーへの接続をオープンする
  • closelog() - システムログへの接続を閉じる
  • syslog.filter INI ディレクティブ(PHP 7.3 以降)

add a note

User Contributed Notes 8 notes

up
11
james dot ellis at gmail dot com
17 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) :

local0.info /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
etc

saves disk space among other things - more at "man syslog.conf"
up
6
stevekamerman at gmail dot com
6 years ago
This function sends messages in BSD Syslog RFC 3164 format (https://tools.ietf.org/html/rfc3164).

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:

<?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
up
5
OWM
4 years ago
Syslog autodetects newline control characters and therefore splits the message 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
up
8
Antonio Lobato
14 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:

<?php
openlog
("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.
up
3
rgagnon24 at gmail dot com
6 years ago
This one had me going for a while when using LOG_ constants in another object, when developing on Windows, but deploying on Linux.

Windows evaluates some of the LOG_ constants to the same value, while LINUX does not.

The 8 constants 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 messages log at different priorities depending on the platform, this could be why.
up
2
huangyg11 at gmail dot com
9 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");
up
2
helly at php dot net
17 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); };
up
0
Anonymous
3 years ago
There's no point to manually timestamp the message (as shown in docs' example) as all sane logging systems timestamp all entries by themselves.
To Top