PHP openlog() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, openlog() prepares a connection to the system logger and sets a short identity string that is attached to later syslog() messages.

You pass option flags such as LOG_PID and a facility such as LOG_USER. Calling openlog() is optional because syslog() can open the log for you, but an explicit call keeps the identity consistent. Pair it with closelog() when you are finished.

Syntax

Call openlog() with the values you need. Start with this line.

PHP
openlog('myapp', LOG_PID | LOG_PERROR, LOG_USER);
CallWhat it does
openlog('myapp', LOG_PID, LOG_USER)Sets the identity to myapp and includes the process id in messages.
openlog('checkout', LOG_ODELAY, LOG_LOCAL0)Uses a local facility often reserved for custom apps.
openlog('demo', LOG_PERROR, LOG_USER)Also mirrors messages to the standard error stream when supported.

The first argument is the identity prefix. The second is a bitmask of LOG_* option constants. The third chooses the facility. On Windows the facility value is ignored. Modern PHP treats a successful openlog() as returning true.

Library declarations
PHP
openlog(string $ident, int $option, int $facility): bool

Parameters

ParameterDescription
identRequired. Short string added to each log message, usually your app or script name.
optionRequired. Bitmask of flags such as LOG_PID, LOG_PERROR, LOG_CONS, LOG_NDELAY, LOG_ODELAY, or LOG_NOWAIT.
facilityRequired. One of the LOG_* facility constants such as LOG_USER, LOG_MAIL, or LOG_LOCAL0 through LOG_LOCAL7.

Return Value

Return ValueDescription
booltrue when the logger connection was opened successfully.

PHP Examples

Example 1 — Open the System Logger

Call openlog() with a simple identity and print the boolean result.

PHP
<?php
$ok = openlog('codetofun', LOG_PID, LOG_USER);
var_dump($ok);
closelog();

How It Works

  1. openlog() sets the identity to codetofun.
  2. LOG_PID asks the logger to include the process id.
  3. LOG_USER is a common facility for user programs.
  4. closelog() closes the connection when the demo ends.

Example 2 — Combine Option Flags

Join LOG_PID and LOG_PERROR with a bitwise OR so messages also appear on stderr when the platform supports it.

PHP
<?php
$flags = LOG_PID | LOG_PERROR;
$ok = openlog('shop', $flags, LOG_USER);
echo $ok ? 'logger ready' : 'logger failed', PHP_EOL;
closelog();

How It Works

  1. Bitwise OR combines multiple openlog option flags.
  2. LOG_PERROR can echo messages to standard error.
  3. We print a friendly status based on the return value.
  4. closelog() cleans up after the call.

Example 3 — Open, Log, and Close

Show the usual openlog → syslog → closelog pattern for an application message.

PHP
<?php
openlog('billing', LOG_ODELAY, LOG_USER);
$written = syslog(LOG_INFO, 'Invoice job started');
closelog();

echo $written ? 'message accepted' : 'message failed', PHP_EOL;

How It Works

  1. openlog() configures the identity billing.
  2. syslog() writes one informational line to the system log.
  3. closelog() closes the logger connection.
  4. The demo prints whether syslog() accepted the message.

Frequently Asked Questions

It opens a connection to the system logger and sets the identity string and options used by later syslog() calls.
No. syslog() can open the log automatically, but calling openlog() first lets you choose the identity, flags, and facility.
It tells the system which category of program is logging, such as LOG_USER or LOG_MAIL. On Windows this argument is ignored.
Call closelog() when you are done. It has no parameters.
syslog($priority, $message). openlog() only prepares the connection.

Next: pfsockopen()

pfsockopen() opens a network or Unix-domain socket the same way fsockopen() does, but keep.

pfsockopen() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful