What are some best practices for creating and formatting log files in PHP?

When creating and formatting log files in PHP, it is important to follow best practices to ensure readability and efficiency. Some key tips include using a consistent log format, including relevant information such as timestamps and log levels, rotating log files to prevent them from becoming too large, and securing log files to prevent unauthorized access.

// Example of creating and formatting log files in PHP

$logFile = 'app.log';
$logMessage = 'Error message here';

// Log format: [timestamp] [log level] [message]
$logFormat = "[" . date('Y-m-d H:i:s') . "] [ERROR] " . $logMessage . PHP_EOL;

// Write log message to file
file_put_contents($logFile, $logFormat, FILE_APPEND);