PHP syslog() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, syslog() writes a message to the system logger using a priority such as LOG_INFO or LOG_ERR.

If you already called openlog(), the message uses that identity and facility. If not, PHP opens the logger for you. Priorities help operators filter important errors from routine notices. Close with closelog() when your script is finished logging.

Syntax

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

PHP
syslog(LOG_INFO, 'Backup finished');
CallWhat it does
syslog(LOG_INFO, 'Backup finished')Writes an informational message.
syslog(LOG_ERR, 'Payment failed')Writes an error-level message.
syslog(LOG_WARNING, $message)Writes a warning with a variable message string.

The first argument is a LOG_* priority constant. The second is the text to record. openlog() is optional but recommended so every line shares a clear identity. Modern PHP returns true when the message is accepted by the logging layer.

Library declarations
PHP
syslog(int $priority, string $message): bool

Parameters

ParameterDescription
priorityRequired. One of the LOG_* priority constants such as LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, or LOG_DEBUG.
messageRequired. The text to send to the system log.

Return Value

Return ValueDescription
booltrue when the logging system accepted the message.

PHP Examples

Example 1 — Write an Info Message

Open the logger, write one informational line, and print the return value.

PHP
<?php
openlog('codetofun', LOG_PID, LOG_USER);
$ok = syslog(LOG_INFO, 'Tutorial demo message');
closelog();
var_dump($ok);

How It Works

  1. openlog() sets the identity codetofun.
  2. LOG_INFO marks a normal informational event.
  3. syslog() hands the text to the system logger.
  4. closelog() closes the connection for a tidy demo.

Example 2 — Choose Different Priorities

Send two messages at different severity levels and confirm both calls succeed.

PHP
<?php
openlog('jobs', LOG_ODELAY, LOG_USER);
$a = syslog(LOG_WARNING, 'Disk space low');
$b = syslog(LOG_ERR, 'Job aborted');
closelog();

echo $a && $b ? 'both accepted' : 'something failed', PHP_EOL;

How It Works

  1. LOG_WARNING is for recoverable problems.
  2. LOG_ERR is for failures that need attention.
  3. Both calls return true when accepted.
  4. Operators can filter severities in the system log viewer.

Example 3 — Log Without Calling openlog() First

Call syslog() alone so PHP opens the logger with defaults, then close it.

PHP
<?php
$ok = syslog(LOG_NOTICE, 'No openlog() was called first');
closelog();
echo $ok ? 'logged with defaults' : 'failed', PHP_EOL;

How It Works

  1. syslog() can open the system logger automatically.
  2. Without openlog(), the identity prefix may be empty or generic.
  3. LOG_NOTICE is a middle severity between info and warning.
  4. closelog() still cleans up afterward.

Frequently Asked Questions

It writes a message to the operating system logger at the priority you choose.
It is optional. syslog() can open the log itself, but openlog() lets you set a clear identity and facility.
Into the system log configured on the server, such as the journal or a syslog file. They are not printed in the HTTP response.
Constants such as LOG_EMERG, LOG_ALERT, LOG_CRIT, LOG_ERR, LOG_WARNING, LOG_NOTICE, LOG_INFO, and LOG_DEBUG.
Call closelog() when you are finished. It closes the connection opened by openlog() or by syslog().

Back to Network Functions

Browse all PHP network function tutorials.

Network functions →

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