Current Location: Home> Latest Articles> Practical case: How to use the end function to quickly extract the latest error information with the log data?

Practical case: How to use the end function to quickly extract the latest error information with the log data?

M66 2025-06-02

During the development and operation and maintenance process, log files are usually the main basis for us to troubleshoot problems and obtain error information. Log files can be very large, so we need a quick and efficient way to extract the latest error information. In PHP, you can easily achieve this by utilizing the end() function.

This article will use a practical case to show how to quickly extract the latest error information by combining log data and PHP's end() function.

Scene description

Suppose we have a log file error.log , which contains error information when the program is executed, and an error event is recorded in each line. The content of the log file is as follows:

 [2025-04-20 14:00:05] ERROR: Unable to connect to database
[2025-04-20 14:01:23] ERROR: Invalid API key
[2025-04-20 14:05:10] ERROR: m66.net/api/v1/invalid-endpoint
[2025-04-20 14:06:00] ERROR: Server timeout
[2025-04-20 14:07:30] ERROR: Unauthorized access attempt

Our goal is to extract the latest error message in the log file, namely Unauthorized access attempt .

Step 1: Read the log file

First, we need to open and read the log file. PHP's file() function is very suitable for reading files and returning the file contents as an array. Each array element corresponds to one line in the file.

 <?php
// Read the log file and save each line into an array
$logFile = 'error.log';
$logData = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Output log data(Optional)
foreach ($logData as $line) {
    echo $line . PHP_EOL;
}
?>

In the above code, we use the file() function to read the error.log file, and remove the line break at the end of the line through the FILE_IGNORE_NEW_LINES parameter, and ignore blank lines using FILE_SKIP_EMPTY_LINES .

Step 2: Use the end() function to extract the latest error information

In PHP, the end() function can point the array pointer to the last element of the array and return that element. Since the log files record errors in chronological order, the last error is the latest error information we need.

 <?php
// Get the latest error message
$latestError = end($logData);

// Output the latest error message
echo "Latest error message: " . $latestError;
?>

Here, we call the end() function to get the last error message in the array. $latestError will contain the latest log record.

Step 3: Extract and display error message

If we are only interested in the specific content in the error message, we can extract the error content from each line of the log through regular expressions. For example, we can extract the error description after ERROR :.

 <?php
// Extract and output the latest error description
if (preg_match('/ERROR: (.+)/', $latestError, $matches)) {
    echo "Latest error description: " . $matches[1];
} else {
    echo "Failed to extract error description";
}
?>

In this example, preg_match() is used to match the error message part in the log line. The regular expression '/ERROR: (.+)/' matches everything after ERROR: and stores it in the $matches array.

Complete code example

 <?php
// Read the log file and save each line into an array
$logFile = 'error.log';
$logData = file($logFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

// Get the latest error message
$latestError = end($logData);

// Extract and output the latest error description
if (preg_match('/ERROR: (.+)/', $latestError, $matches)) {
    echo "Latest error description: " . $matches[1];
} else {
    echo "Failed to extract error description";
}
?>

Summarize

By combining PHP's end() function and regular expression, we can quickly extract the latest error information from the log file. This method is especially suitable for situations where log files are relatively large, and can effectively reduce memory consumption and improve processing speed.

Of course, the log file path error.log and log format in the above example code need to be adjusted according to your actual project. In addition, in actual development, you can also process log files more, such as displaying historical logs on pages, filtering logs by time period, etc.