How can PHP scripts log information to an external process for centralized logging?

PHP scripts can log information to an external process for centralized logging by using a logging library like Monolog. Monolog allows you to send log messages to various handlers, including external processes like syslog, Elasticsearch, or a database. By configuring Monolog to use a handler that sends logs to an external process, PHP scripts can easily log information for centralized monitoring and analysis.

// Include the Monolog library
require 'vendor/autoload.php';

// Create a new Monolog logger instance
$log = new Monolog\Logger('name');

// Add a handler that sends logs to an external process (e.g., syslog)
$log->pushHandler(new Monolog\Handler\SyslogHandler('ident', LOG_USER, Monolog\Logger::DEBUG));

// Log a message
$log->info('This is an informational message');