|
123456789101112131415161718192021222324252627282930313233343536373839404142 |
- <?php
-
- namespace App\Patrones\Singleton;
-
- class Logger extends Singleton
- {
- /**
- * A file pointer resource of the log file.
- */
- private $fileHandle;
-
- /**
- * Since the Singleton's constructor is called only once, just a single file
- * resource is opened at all times.
- *
- * Note, for the sake of simplicity, we open the console stream instead of
- * the actual file here.
- */
- protected function __construct()
- {
- $this->fileHandle = fopen('php://stdout', 'w');
- }
-
- /**
- * Write a log entry to the opened file resource.
- */
- public function writeLog(string $message): void
- {
- $date = date('Y-m-d');
- fwrite($this->fileHandle, "$date: $message\n");
- }
-
- /**
- * Just a handy shortcut to reduce the amount of code needed to log messages
- * from the client code.
- */
- public static function log(string $message): void
- {
- $logger = static::getInstance();
- $logger->writeLog($message);
- }
- }
|