In this article, we'll create a simple logger system using the Singleton Pattern in PHP.
This logger will allow you to log messages to a file, ensuring that there is only one instance of the logger throughout your application.
Step-by-Step Implementation
Step 1: Create the Logger Class
We'll start by defining a Logger class that follows the Singleton Pattern. This class will have methods for logging messages and ensuring that only one instance exists.
class Logger {
private static ?Logger $instance = null;
private string $logFile;
// Private constructor to prevent direct instantiation
private function __construct() {
$this->logFile = 'app.log'; // Define the log file
}
// Method to get the single instance of Logger
public static function getInstance(): Logger {
if (self::$instance === null) {
self::$instance = new Logger();
}
return self::$instance;
}
// Method to log a message
public function log(string $message): void {
$timestamp = date('Y-m-d H:i:s');
$formattedMessage = "[$timestamp] $message" . PHP_EOL;
file_put_contents($this->logFile, $formattedMessage, FILE_APPEND);
}
// Optional: Method to read the log file (for testing purposes)
public function readLog(): string {
return file_get_contents($this->logFile);
}
}
Google Ad 1
Step 2: Usage Example
Now that we have our Logger class set up, let's see how to use it in a real application scenario.
// Usage of the Logger
$logger = Logger::getInstance();
$logger->log("Application started.");
// Simulating some actions
$logger->log("User logged in.");
$logger->log("Error: Unable to connect to the database.");
$logger->log("Application finished.");
Step 3: Testing the Logger
You can read the log file to verify that messages are being logged correctly. Here’s a small snippet to do that:
// Displaying the log contents
$logger = Logger::getInstance();
echo $logger->readLog();
Google Ad 2
Complete Example
Putting everything together, here’s the complete code for the Logger system.
class Logger {
private static ?Logger $instance = null;
private string $logFile;
private function __construct() {
$this->logFile = 'app.log'; // Define the log file
}
public static function getInstance(): Logger {
if (self::$instance === null) {
self::$instance = new Logger();
}
return self::$instance;
}
public function log(string $message): void {
$timestamp = date('Y-m-d H:i:s');
$formattedMessage = "[$timestamp] $message" . PHP_EOL;
file_put_contents($this->logFile, $formattedMessage, FILE_APPEND);
}
public function readLog(): string {
return file_get_contents($this->logFile);
}
}
// Usage of the Logger
$logger = Logger::getInstance();
$logger->log("Application started.");
$logger->log("User logged in.");
$logger->log("Error: Unable to connect to the database.");
$logger->log("Application finished.");
// Displaying the log contents
echo $logger->readLog();
In this hands-on exercise, we've successfully implemented a simple logger system using the Singleton Pattern in PHP. This design ensures that only one instance of the logger exists, which simplifies the logging process and avoids conflicts.
Thanks for reading the article, for more Science & Technology related articles read and subscribe to peoples blog articles.