Penggunaan fungsi OPENLOG pada PHP

Definisi dan Penggunaan

Fungsi openlog() digunakan untuk dapat membuka koneksi logger sistem.

Syntax

openlog(ident, option, facility)

Nilai Parameter

ParameterDeskripsi
ident Required. Menentukan identitas string yang ditambahkan ke setiap pesan
option Required. Menentukan opsi logging apa yang akan digunakan saat membuat pesan log. Dapat berupa satu atau lebih opsi berikut (dipisahkan dengan |):

LOG_CONS
LOG_NDELAY
LOG_ODELAY
LOG_PERROR
LOG_PID

facility Required.Menentukan jenis program yang mencatat pesan:

LOG_AUTH
LOG_AUTHPRIV
LOG_CRON
LOG_DAEMON
LOG_KERN
LOG_LOCAL0 … LOG_LOCAL7
LOG_LPR
LOG_MAIL
LOG_NEWS
LOG_SYSLOG
LOG_USER – (adalah satu-satunya jenis log yang valid untuk OS Windows)
LOG_UUCP

Detail Teknis

Return Value:TRUE jika sukses, FALSE jika gagal
PHP Version:4.0+

Contoh
Buka dan tutup koneksi logger sistem:

<?php
function _log($text) {
openlog("phperrors", LOG_PID | LOG_PERROR);
syslog(LOG_ERR, $text);
closelog();
....
....
}
?>

Referensi Jaringan PHP

Contoh

Buka dan tutup koneksi logger sistem:

<?php
function _log($text) {
openlog("phperrors", LOG_PID | LOG_PERROR);
syslog(LOG_ERR, $text);
closelog();
....
....
}
?>



Definisi dan Penggunaan

Fungsi openlog() membuka koneksi logger sistem.

Sintaksis

openlog(ident, option, facility)

Nilai Parameter

ParameterDescription
ident Required. Specifies a string ident that is added to each message
option Required. Specifies what logging options will be used when generating a log message. Can be one or more of the following options (separated with |):
  • LOG_CONS
  • LOG_NDELAY
  • LOG_ODELAY
  • LOG_PERROR
  • LOG_PID
facility Required. Specifies what type of program is logging the message:
  • LOG_AUTH
  • LOG_AUTHPRIV
  • LOG_CRON
  • LOG_DAEMON
  • LOG_KERN
  • LOG_LOCAL0...LOG_LOCAL7
  • LOG_LPR
  • LOG_MAIL
  • LOG_NEWS
  • LOG_SYSLOG
  • LOG_USER - (is the only valid log type for Windows OS)
  • LOG_UUCP


Detail Teknis

Nilai Kembali:BENAR pada keberhasilan, SALAH pada kegagalan
Versi PHP:4.0+

Referensi Jaringan PHP


Fungsi syslog() dapat digunakan untuk menghasilkan pesan log sistem.

<?php
function _log($text) {
openlog("phperrors", LOG_PID | LOG_PERROR);
syslog(LOG_ERR, $text);
closelog();
....
....
}
?>

Fungsi syslog() menghasilkan pesan log sistem.

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

syslogGenerate a system log message

Description

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

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:

syslog() Priorities (in descending order)
ConstantDescription
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.

Return Values

Returns true on success or false on failure.

Examples

Example #1 Using syslog()

<?php
// 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_PERRORLOG_LOCAL0);// some codeif (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, 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.

See Also

  • openlog() - Open connection to system logger
  • closelog() - Close connection to system logger
  • syslog.filter INI setting (starting with PHP 7.3)

OWM

2 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

stevekamerman at gmail dot com

3 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

james dot ellis at gmail dot com

14 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"

Antonio Lobato

12 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.

rgagnon24 at gmail dot com

3 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.

helly at php dot net

15 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

7 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");

antoine dot leverve dot EXT at zodiacaerospace dot com

6 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:
LOG_ERR : 4
LOG_WARNING : 5
LOG_DEBUG : 6

An important difference, that caused me some pain!

daniele dot patoner at biblio dot unitn dot it

18 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:

<?php
define_syslog_variables
();
openlog("TextLog", LOG_PID, LOG_LOCAL0);$data = date("Y/m/d H:i:s");
syslog(LOG_DEBUG,"Messagge: $data");closelog();
?>

mavetju at chello dot nl

21 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: local3.info):
syslog(158,"test");

Anonymous

9 months 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.

dpreece at paradise dot net dot nz

20 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

!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

Torsten

18 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.

rcgraves+php at brandeis dot edu

22 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.

adam _at_ lockdownnetworks _dot_ com

15 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:

<?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.

gherson at snet dot net

21 years ago

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