Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

Logger.php 1017 B

há 2 anos
123456789101112131415161718192021222324252627282930313233343536373839404142
  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. }