Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Network openlog() Function



The PHP Network openlog() function is used to set up a connection with the system logger. A system logger is a service that collects and records messages for later analysis or debugging. This function is optional to use. If you do not call openlog(), the syslog() function will do it automatically as necessary.

The openlog() function accepts three arguments: a prefix, flags, and facility. The prefix is a string put at the beginning of each log message to identify it. Flags control the behavior of logging. For example, options like LOG_CONS ensure that messages are routed to the console if the logging system fails.

The function informs the system about the type of program that is sending the message. Examples are LOG_AUTH for authentication and LOG_MAIL for mail servers.

Syntax

Below is the syntax of the PHP Network openlog() function −

bool openlog ( string $prefix, int $flags, int $facility )

Parameters

Here are the parameters of the openlog() function −

  • $prefix − (Required) It is the string prefix is added to each message.

  • $flags − (Required) It is used to specify the logging options that will be utilized when creating a log message.

  • $facility − (Required) It is used to specify what type of program is logging the message.

Return Value

The openlog() function returns TRUE on success. And FALSE on failure.

PHP Version

First introduced in core PHP 4, the openlog() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the simple program which shows the usage of the PHP Network openlog() to set a prefix and log a basic message to the system logger.

<?php
   // Open connection to system logger 
   openlog("ConsoleLogger", LOG_PERROR | LOG_PID, LOG_USER);

   // Log a message 
   syslog(LOG_INFO, "This message is logged to the console and the system logger.");

   // Close the system logger
   closelog();
?>

Output

Here is the outcome of the following code −

Jan 22 03:14:29  ConsoleLogger[5566] <Info>: This message is logged to the console and the system logger.

Example 2

In the below program the openlog() function creates a connection with the system logger, allow the program to send log messages. It detects and configures the "FileLogger" logger, with its settings and log type.

<?php
   // Specify a log file
   $logFile = "custom_log.log";

   // Open connection to system logger
   openlog("FileLogger", LOG_PID, LOG_USER);

   // Redirect logs to a custom file
   file_put_contents($logFile, "Log started\n", FILE_APPEND);
   syslog(LOG_INFO, "This is a log message written to a custom file.");
   file_put_contents($logFile, "This is a custom log message.\n", FILE_APPEND);

   // Close the system logger
   closelog();

   // Display the file contents
   echo "Logs written to $logFile:\n";
   echo file_get_contents($logFile);
?> 

Output

This will generate the below output −

Logs written to custom_log.log:
Log started
This is a custom log message.

Example 3

The openlog() function connects to the system logger named "SeverityLogger." It uses the options LOG_PERROR (to print logs to the console) and LOG_PID (to store the process ID), as well as the LOG_USER facility to classify messages.

<?php
   // Open connection to system logger
   openlog("SeverityLogger", LOG_PERROR | LOG_PID, LOG_USER);

   // Log messages with different severity levels
   syslog(LOG_DEBUG, "Debugging information.");
   syslog(LOG_WARNING, "This is a warning message.");
   syslog(LOG_ERR, "An error occurred!");

   // Close the system logger
   closelog();
?> 

Output

This will create the below output −

Jan 22 04:19:11  SeverityLogger[5653] <Debug>: Debugging information.
Jan 22 04:19:11  SeverityLogger[5653] <Warning>: This is a warning message.
Jan 22 04:19:11  SeverityLogger[5653] <Error>: An error occurred!
php_function_reference.htm
Advertisements