Current Location: Home> Latest Articles> How to Efficiently Implement Log Recording with PHP Scripts in a Linux Environment

How to Efficiently Implement Log Recording with PHP Scripts in a Linux Environment

M66 2025-06-25

How to Use PHP Scripts for Log Recording in a Linux Environment

In a Linux environment, using PHP scripts for log recording is a common and efficient method. This article will explain how to implement log recording with PHP scripts on Linux, providing code examples to help you understand and apply it effectively.

1. Create a Log Directory

First, in your Linux system, create a directory to store the logs. You can use the following command to create a new log directory:

mkdir /var/log/mylog

In this directory, create a log file to store the access logs. Use the following command to create a file called “access.log”:

touch /var/log/mylog/access.log

2. Write a PHP Script for Log Recording

Next, we will write a PHP script to implement the log recording functionality. Create a file called “log.php” and add the following code:


<?php
<p>function writeLog($message) {<br>
$file = '/var/log/mylog/access.log';<br>
$timestamp = date('Y-m-d H:i:s');<br>
$log = $timestamp . ' - ' . $message . "\n";<br>
file_put_contents($file, $log, FILE_APPEND);<br>
}</p>
<p>// Example: Record a log<br>
$message = 'Accessed page - ' . $_SERVER['REQUEST_URI'];<br>
writeLog($message);</p>
<p>?><br>

In this code, we define a function called “writeLog”, which writes log messages (including timestamps) into the specified log file. Every time the function is called, a new log entry is recorded.

In this example, we record an access log containing the accessed page URL. You can modify the content of the log message according to your needs.

3. Using the PHP Script for Log Recording

To demonstrate how to use the log recording functionality in a real PHP project, we can call the “log.php” script within a web page. Create a file called “index.php” and add the following code:


<?php
<p>// Call the log.php script to record logs<br>
require_once('log.php');</p>
<p>// Example: Record a log<br>
$message = 'Accessed page - ' . $_SERVER['REQUEST_URI'];<br>
writeLog($message);</p>
<p>// Other business code...</p>
<p>?><br>

In this example, we include the “log.php” file using “require_once” and call the “writeLog” function to record an access log. You can add similar log recording code in other parts of your project as needed.

4. Verifying Log Recording

Now, you can verify the log recording by visiting the “index.php” page.

http://localhost/index.php

After accessing the page, check the “access.log” file to see if the access log was successfully recorded.

Conclusion

Through this article, we learned how to use PHP scripts for log recording in a Linux environment. We first created a log directory and wrote a PHP script to handle log recording. Then, we showed how to use the script in an actual project to record log information into the specified log file.

This method provides an efficient way to record logs, which is valuable for system maintenance and troubleshooting.