Last active
October 28, 2025 22:05
-
-
Save PawelGIX/ea6f9790624509638359ae46909df884 to your computer and use it in GitHub Desktop.
SimpleLogger.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| use Psr\Log\LoggerInterface; | |
| use Psr\Log\LogLevel; | |
| class SimpleFileLogger implements LoggerInterface | |
| { | |
| protected string $path; | |
| protected int $maxLines = 1000; | |
| public function __construct(string $logFilePath) | |
| { | |
| $this->path = $logFilePath; | |
| } | |
| // --- PSR-3 Required Methods --- | |
| public function emergency(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::EMERGENCY, $message, $context); | |
| } | |
| public function alert(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::ALERT, $message, $context); | |
| } | |
| public function critical(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::CRITICAL, $message, $context); | |
| } | |
| public function error(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::ERROR, $message, $context); | |
| } | |
| public function warning(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::WARNING, $message, $context); | |
| } | |
| public function notice(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::NOTICE, $message, $context); | |
| } | |
| public function info(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::INFO, $message, $context); | |
| } | |
| public function debug(string|\Stringable $message, array $context = []): void | |
| { | |
| $this->log(LogLevel::DEBUG, $message, $context); | |
| } | |
| public function log($level, string|\Stringable $message, array $context = []): void | |
| { | |
| $message = $this->interpolate($message, $context); | |
| $logLine = sprintf( | |
| '[%s] [%s]: %s%s', | |
| (new \DateTimeImmutable())->format('Y-m-d H:i:s'), | |
| strtoupper((string) $level), | |
| trim($message), | |
| PHP_EOL | |
| ); | |
| // Ensure file exists | |
| if (!file_exists($this->path)) { | |
| touch($this->path); | |
| } | |
| // Append new line | |
| file_put_contents($this->path, $logLine, FILE_APPEND); | |
| // Trim old lines if file is too long | |
| $this->trimLogFile(); | |
| } | |
| protected function trimLogFile(): void | |
| { | |
| $lines = @file($this->path, FILE_IGNORE_NEW_LINES); | |
| if ($lines === false) { | |
| return; | |
| } | |
| $count = count($lines); | |
| if ($count > $this->maxLines) { | |
| // Keep only the last N lines | |
| $lines = array_slice($lines, -$this->maxLines); | |
| file_put_contents($this->path, implode(PHP_EOL, $lines) . PHP_EOL, LOCK_EX); | |
| } | |
| } | |
| protected function interpolate(string|\Stringable $message, array $context = []): string | |
| { | |
| if (empty($context)) { | |
| return $message; | |
| } | |
| $replace = []; | |
| foreach ($context as $key => $val) { | |
| $replace['{' . $key . '}'] = is_scalar($val) ? $val : json_encode($val); | |
| } | |
| return strtr($message, $replace); | |
| } | |
| } | |
| // $logger = new SimpleFileLogger(__DIR__ . '/logs/google.log'); | |
| // $logger->alert('Alert message'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment