As a widely used server-side programming language, PHP plays a crucial role in web application development and deployment. Log handling and monitoring are key to ensuring application stability and performance optimization. This article introduces commonly used and practical methods for log handling and monitoring in PHP packaging and deployment, accompanied by sample code to help you efficiently manage and monitor your PHP applications.
In PHP, using professional logging libraries can simplify the log recording process. Monolog is one of the most popular logging libraries, supporting various log levels, formatting, and storage options to meet diverse logging needs. Below is an example demonstrating how to use Monolog to record logs:
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
<p>// Create a logger instance<br>
$log = new Logger('name');</p>
<p>// Add a StreamHandler to write logs to a file, with log level set to WARNING<br>
$log->pushHandler(new StreamHandler('/path/to/your.log', Logger::WARNING));</p>
<p>// Record a warning level log<br>
$log->warning('Foo');<br>
If you prefer not to rely on third-party libraries, you can implement log recording yourself. Below is a simple example of a custom log function:
function writeLog($message) {
// Open the log file in append mode
$file = fopen('/path/to/your.log', 'a');
// Write the log message with timestamp
fwrite($file, date('Y-m-d H:i:s') . ' ' . $message . "\n");
// Close the file resource
fclose($file);
}
<p>// Call the custom log function<br>
writeLog('This is a log message.');<br>
By using professional monitoring tools, you can obtain real-time application runtime status and performance metrics. For example, Prometheus is a popular open-source monitoring solution that collects application metrics via exporters. Below is an example showing how to monitor web application request duration using Prometheus and the Guzzle HTTP client:
use GuzzleHttp\Client;
<p>$client = new Client();</p>
<p>// Record the start time of the request<br>
$start = microtime(true);</p>
<p>// Send the HTTP request<br>
$response = $client->get('<a rel="noopener" target="_new" class="" href="http://example.com">http://example.com</a>');</p>
<p>// Record the end time of the request<br>
$end = microtime(true);</p>
<p>// Calculate the duration of the request<br>
$duration = $end - $start;</p>
<p>// Send metric data to Prometheus Exporter<br>
$client->post('<a rel="noopener" target="_new" class="" href="http://localhost:9091/metrics/job/myapp">http://localhost:9091/metrics/job/myapp</a>', [<br>
'body' => "myapp_request_duration_seconds $duration"<br>
]);<br>
Besides using existing monitoring tools, you can also implement custom functions to collect and store performance metrics. Here's an example:
function monitor($metric, $value) {
// Connect to the database to store monitoring data
$pdo = new PDO("mysql:host=localhost;dbname=myapp", "username", "password");
$pdo->exec("INSERT INTO metrics (metric, value, timestamp) VALUES ('$metric', '$value', NOW())");
}
<p>// Use the custom monitoring function to record request count<br>
monitor('request_count', 1);<br>
In summary, PHP packaging and deployment can handle logging by using mature logging libraries or custom functions, while monitoring can rely on open-source tools or self-implemented monitoring logic. Selecting methods according to project requirements effectively ensures the stable operation and performance optimization of PHP applications.