PHP closelog() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Network

Definition

In PHP, closelog() closes the connection to the system logger that was opened with openlog(). After you finish writing syslog() messages, call closelog() to clean up.

On most systems it always returns true. Pairing openlog(), syslog() and closelog() keeps log identity and options consistent for your script.

Syntax

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

PHP
closelog();
CallWhat it does
closelog()Closes the system logger handle opened by openlog().
openlog("myapp", LOG_PID, LOG_USER); closelog()Opens a log channel, then closes it when finished.
syslog(LOG_INFO, "done"); closelog()Writes a message, then closes the logger.

closelog() takes no parameters. Call it after openlog() and your syslog() writes. You can call syslog() without openlog(), but openlog() lets you set an identity and options first.

Library declarations
PHP
closelog(): true

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
trueAlways returns true.

PHP Examples

Example 1 — Open, Write, and Close the Logger

Open a log identity, write one message, then close the logger. Return values are shown as a local demo.

PHP
<?php
$opened = openlog("demo-app", LOG_PID | LOG_PERROR, LOG_USER);
$written = syslog(LOG_INFO, "Demo message from PHP");
$closed = closelog();

var_dump($opened, $written, $closed);

How It Works

  1. openlog() sets the identity "demo-app" and options.
  2. syslog() writes an informational message.
  3. closelog() closes the logger session.
  4. var_dump() shows that each call returned true in this demo.

Example 2 — Close After Several Messages

Write a few severity levels, then call closelog() once at the end.

PHP
<?php
openlog("demo-app", LOG_ODELAY, LOG_USER);

syslog(LOG_WARNING, "Disk space low");
syslog(LOG_ERR, "Could not open config");
syslog(LOG_INFO, "Shutdown complete");

echo closelog() ? "logger closed" : "close failed";

How It Works

  1. openlog() prepares the system logger for this script.
  2. Three syslog() calls write different severity levels.
  3. closelog() runs once after all messages are sent.
  4. We print a short status based on the return value.

Example 3 — Use a Helper That Always Closes

Wrap openlog/syslog/closelog so the logger is closed even in a simple helper function.

PHP
<?php
function log_once(string $message): void {
    openlog("demo-app", LOG_PID, LOG_USER);
    syslog(LOG_NOTICE, $message);
    closelog();
}

log_once("User signup finished");
echo "done";

How It Works

  1. The helper opens the logger with a fixed identity.
  2. syslog() records the notice-level message.
  3. closelog() always runs before the helper returns.
  4. The script prints "done" after the helper finishes.

Frequently Asked Questions

closelog() closes the system logger connection opened by openlog(). Call it when your script is finished writing syslog() messages.
No. closelog() takes no parameters and typically returns true.
It is good practice after openlog(). PHP will clean up at the end of the request, but calling closelog() makes the intent clear.
openlog() starts a logging session with an identity and options. closelog() ends that session. syslog() writes the actual messages.
Messages already written stay in the system log. closelog() only closes your script's logger handle; it does not remove old entries.

Next: dns_check_record()

dns_check_record() is an alias of checkdnsrr(). It checks whether DNS has records of a giv.

dns_check_record() 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