ZetCode

PHP closelog Function

last modified April 4, 2025

The PHP closelog function closes the connection to the system logger. It's used after logging messages with openlog and syslog.

Basic Definition

closelog terminates the connection to the system logger that was established with openlog. It's part of PHP's syslog functions.

Syntax: closelog(): bool. Returns true on success, false on failure. No parameters are needed as it closes the current connection.

Basic Logging Example

This example shows a complete logging cycle with openlog, syslog, and closelog.

basic_logging.php
<?php

declare(strict_types=1);

// Open connection to system logger
openlog("MyPHPApp", LOG_PID | LOG_PERROR, LOG_LOCAL0);

// Send a log message
syslog(LOG_WARNING, "This is a warning message");

// Close the logger connection
closelog();

echo "Log message sent and connection closed";

This demonstrates proper resource management by closing the logger after use. The closelog frees system resources used for logging.

Error Handling with closelog

This example shows how to handle errors when closing the system logger.

error_handling.php
<?php

declare(strict_types=1);

openlog("ErrorHandlingApp", LOG_PID, LOG_USER);

syslog(LOG_ERR, "An error occurred in the application");

if (!closelog()) {
    echo "Failed to close system logger connection";
} else {
    echo "Logger connection closed successfully";
}

Checking the return value of closelog helps identify issues. While failures are rare, proper error handling makes applications more robust.

Logging in a Function

This example demonstrates proper logger cleanup in a function context.

function_logging.php
<?php

declare(strict_types=1);

function logEvent(string $message, int $priority = LOG_INFO): void {
    openlog("FunctionLogger", LOG_PID, LOG_LOCAL1);
    syslog($priority, $message);
    closelog();
}

logEvent("User logged in successfully");
logEvent("Failed login attempt", LOG_WARNING);

Each function call properly manages its logging resources by calling closelog. This prevents resource leaks in long-running scripts.

Conditional Logging

This example shows conditional logging with proper cleanup in all code paths.

conditional_logging.php
<?php

declare(strict_types=1);

function processData($data) {
    openlog("DataProcessor", LOG_PID, LOG_USER);
    
    try {
        if (empty($data)) {
            syslog(LOG_WARNING, "Empty data received");
            return false;
        }
        
        syslog(LOG_INFO, "Processing data: " . substr($data, 0, 20));
        // Process data here
        return true;
    } finally {
        closelog();
    }
}

processData("Sample data to process");

The finally block ensures closelog is called regardless of how the function exits. This is a robust logging pattern.

Logging in a Class

This example demonstrates proper logger management in an object-oriented context.

class_logging.php
<?php

declare(strict_types=1);

class ApplicationLogger {
    public function logMessage(string $message, int $priority): void {
        openlog("AppLoggerClass", LOG_PID, LOG_LOCAL0);
        syslog($priority, $message);
        closelog();
    }
    
    public function __destruct() {
        // Ensure logger is closed if still open
        closelog();
    }
}

$logger = new ApplicationLogger();
$logger->logMessage("Application started", LOG_INFO);

The class properly closes the logger after each message and includes a destructor as a safety net. This prevents resource leaks in object-oriented applications.

Best Practices

Source

PHP closelog Documentation

This tutorial covered the PHP closelog function with practical examples for proper system logging resource management in various scenarios.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all PHP System Functions.