You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

43 lines
1017 B

  1. <?php
  2. namespace App\Patrones\Singleton;
  3. class Logger extends Singleton
  4. {
  5. /**
  6. * A file pointer resource of the log file.
  7. */
  8. private $fileHandle;
  9. /**
  10. * Since the Singleton's constructor is called only once, just a single file
  11. * resource is opened at all times.
  12. *
  13. * Note, for the sake of simplicity, we open the console stream instead of
  14. * the actual file here.
  15. */
  16. protected function __construct()
  17. {
  18. $this->fileHandle = fopen('php://stdout', 'w');
  19. }
  20. /**
  21. * Write a log entry to the opened file resource.
  22. */
  23. public function writeLog(string $message): void
  24. {
  25. $date = date('Y-m-d');
  26. fwrite($this->fileHandle, "$date: $message\n");
  27. }
  28. /**
  29. * Just a handy shortcut to reduce the amount of code needed to log messages
  30. * from the client code.
  31. */
  32. public static function log(string $message): void
  33. {
  34. $logger = static::getInstance();
  35. $logger->writeLog($message);
  36. }
  37. }