
- PHP - Home
- PHP - Roadmap
- PHP - Introduction
- PHP - Installation
- PHP - History
- PHP - Features
- PHP - Syntax
- PHP - Hello World
- PHP - Comments
- PHP - Variables
- PHP - Echo/Print
- PHP - var_dump
- PHP - $ and $$ Variables
- PHP - Constants
- PHP - Magic Constants
- PHP - Data Types
- PHP - Type Casting
- PHP - Type Juggling
- PHP - Strings
- PHP - Boolean
- PHP - Integers
- PHP - Files & I/O
- PHP - Maths Functions
- PHP - Heredoc & Nowdoc
- PHP - Compound Types
- PHP - File Include
- PHP - Date & Time
- PHP - Scalar Type Declarations
- PHP - Return Type Declarations
- PHP - Operators
- PHP - Arithmetic Operators
- PHP - Comparison Operators
- PHP - Logical Operators
- PHP - Assignment Operators
- PHP - String Operators
- PHP - Array Operators
- PHP - Conditional Operators
- PHP - Spread Operator
- PHP - Null Coalescing Operator
- PHP - Spaceship Operator
- PHP Control Statements
- PHP - Decision Making
- PHP - If…Else Statement
- PHP - Switch Statement
- PHP - Loop Types
- PHP - For Loop
- PHP - Foreach Loop
- PHP - While Loop
- PHP - Do…While Loop
- PHP - Break Statement
- PHP - Continue Statement
- PHP Arrays
- PHP - Arrays
- PHP - Indexed Array
- PHP - Associative Array
- PHP - Multidimensional Array
- PHP - Array Functions
- PHP - Constant Arrays
- PHP Functions
- PHP - Functions
- PHP - Function Parameters
- PHP - Call by value
- PHP - Call by Reference
- PHP - Default Arguments
- PHP - Named Arguments
- PHP - Variable Arguments
- PHP - Returning Values
- PHP - Passing Functions
- PHP - Recursive Functions
- PHP - Type Hints
- PHP - Variable Scope
- PHP - Strict Typing
- PHP - Anonymous Functions
- PHP - Arrow Functions
- PHP - Variable Functions
- PHP - Local Variables
- PHP - Global Variables
- PHP Superglobals
- PHP - Superglobals
- PHP - $GLOBALS
- PHP - $_SERVER
- PHP - $_REQUEST
- PHP - $_POST
- PHP - $_GET
- PHP - $_FILES
- PHP - $_ENV
- PHP - $_COOKIE
- PHP - $_SESSION
- PHP File Handling
- PHP - File Handling
- PHP - Open File
- PHP - Read File
- PHP - Write File
- PHP - File Existence
- PHP - Download File
- PHP - Copy File
- PHP - Append File
- PHP - Delete File
- PHP - Handle CSV File
- PHP - File Permissions
- PHP - Create Directory
- PHP - Listing Files
- Object Oriented PHP
- PHP - Object Oriented Programming
- PHP - Classes and Objects
- PHP - Constructor and Destructor
- PHP - Access Modifiers
- PHP - Inheritance
- PHP - Class Constants
- PHP - Abstract Classes
- PHP - Interfaces
- PHP - Traits
- PHP - Static Methods
- PHP - Static Properties
- PHP - Namespaces
- PHP - Object Iteration
- PHP - Encapsulation
- PHP - Final Keyword
- PHP - Overloading
- PHP - Cloning Objects
- PHP - Anonymous Classes
- PHP Web Development
- PHP - Web Concepts
- PHP - Form Handling
- PHP - Form Validation
- PHP - Form Email/URL
- PHP - Complete Form
- PHP - File Inclusion
- PHP - GET & POST
- PHP - File Uploading
- PHP - Cookies
- PHP - Sessions
- PHP - Session Options
- PHP - Sending Emails
- PHP - Sanitize Input
- PHP - Post-Redirect-Get (PRG)
- PHP - Flash Messages
- PHP AJAX
- PHP - AJAX Introduction
- PHP - AJAX Search
- PHP - AJAX XML Parser
- PHP - AJAX Auto Complete Search
- PHP - AJAX RSS Feed Example
- PHP XML
- PHP - XML Introduction
- PHP - Simple XML Parser
- PHP - SAX Parser Example
- PHP - DOM Parser Example
- PHP Login Example
- PHP - Login Example
- PHP - Facebook Login
- PHP - Paypal Integration
- PHP - MySQL Login
- PHP Advanced
- PHP - MySQL
- PHP.INI File Configuration
- PHP - Array Destructuring
- PHP - Coding Standard
- PHP - Regular Expression
- PHP - Error Handling
- PHP - Try…Catch
- PHP - Bugs Debugging
- PHP - For C Developers
- PHP - For PERL Developers
- PHP - Frameworks
- PHP - Core PHP vs Frame Works
- PHP - Design Patterns
- PHP - Filters
- PHP - JSON
- PHP - Exceptions
- PHP - Special Types
- PHP - Hashing
- PHP - Encryption
- PHP - is_null() Function
- PHP - System Calls
- PHP - HTTP Authentication
- PHP - Swapping Variables
- PHP - Closure::call()
- PHP - Filtered unserialize()
- PHP - IntlChar
- PHP - CSPRNG
- PHP - Expectations
- PHP - Use Statement
- PHP - Integer Division
- PHP - Deprecated Features
- PHP - Removed Extensions & SAPIs
- PHP - PEAR
- PHP - CSRF
- PHP - FastCGI Process
- PHP - PDO Extension
- PHP - Built-In Functions
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!