diff options
| author | Asko Nõmm <asko@nth.ee> | 2024-11-10 22:28:03 +0200 |
|---|---|---|
| committer | Asko Nõmm <asko@nth.ee> | 2024-11-10 22:28:03 +0200 |
| commit | 12b6c4b3008c2df545c537943d4e38323cfc174e (patch) | |
| tree | 11e427a794832ac73657da675f3d79949443a3f6 /src/Drivers/FileSystemDriver.php | |
Initial commit
Diffstat (limited to 'src/Drivers/FileSystemDriver.php')
| -rw-r--r-- | src/Drivers/FileSystemDriver.php | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/Drivers/FileSystemDriver.php b/src/Drivers/FileSystemDriver.php new file mode 100644 index 0000000..16c1461 --- /dev/null +++ b/src/Drivers/FileSystemDriver.php @@ -0,0 +1,53 @@ +<?php + +namespace Asko\Loggr\Drivers; + +use Asko\Loggr\Driver; +use DateTime; + +/** + * @author Asko Nõmm <asko@faultd.com> + */ +readonly class FileSystemDriver implements Driver +{ + public function __construct( + private string $directory, + ) {} + + /** + * Logs a serialized message to a file. The log file is created on a daily basis. + * If the required directory or log file does not exist, they are created. + * Ensures that the log file is writable before attempting to write the message. + * + * @param string $serializedMessage The message to be logged, serialized as a string. + * @return void + * @throws \RuntimeException If the log file or directory cannot be created, or if the log file is not writable. + */ + public function log(string $serializedMessage): void + { + // If there's no parent directory, try creating one + if (!is_dir($this->directory)) { + mkdir($this->directory, 0600, true); + } + + $date = new DateTime(); + $file_name = "{$date->format('Y-m-d')}.log"; + $path = $this->directory . DIRECTORY_SEPARATOR . $file_name; + + // If the log file does not exist, try creating one + if (!file_exists($path) && !touch($path)) { + throw new \RuntimeException('Log file could not be created'); + } + + // We managed to get this far, but still can't write to the log file + if (!is_writeable($path)) { + throw new \RuntimeException('Log file is not writeable'); + } + + file_put_contents( + filename: $path, + data: $serializedMessage . PHP_EOL, + flags: FILE_APPEND + ); + } +}
\ No newline at end of file |
