Advertisement
ArcaniSGK507

Untitled

Mar 16th, 2025
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.32 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Services;
  4.  
  5. /**
  6.  * Logger Service
  7.  *
  8.  * This class provides methods for logging messages to files.
  9.  *
  10.  * @package App\Services
  11.  */
  12. class Logger
  13. {
  14.     /**
  15.      * @var string Log file path
  16.      */
  17.     private string $logFile;
  18.  
  19.     /**
  20.      * @var string Minimum log level
  21.      */
  22.     private string $minLevel;
  23.  
  24.     /**
  25.      * @var array Valid log levels
  26.      */
  27.     private const array LEVELS = [
  28.         'debug' => 0,
  29.         'info' => 1,
  30.         'notice' => 2,
  31.         'warning' => 3,
  32.         'error' => 4,
  33.         'critical' => 5,
  34.         'alert' => 6,
  35.         'emergency' => 7
  36.     ];
  37.  
  38.     /**
  39.      * Logger constructor
  40.      *
  41.      * @param string|null $logFile Path to the log file
  42.      * @param string $minLevel Minimum log level
  43.      */
  44.     public function __construct(string $logFile = null, string $minLevel = 'debug')
  45.     {
  46.         $this->logFile = $logFile ?? dirname(__DIR__, 2) . '/logs/app.log';
  47.         $this->minLevel = strtolower($minLevel);
  48.  
  49.         // Create logs directory if it doesn't exist
  50.         $logDir = dirname($this->logFile);
  51.         if (!is_dir($logDir)) {
  52.             mkdir($logDir, 0755, true);
  53.         }
  54.     }
  55.  
  56.     /**
  57.      * Log a message
  58.      *
  59.      * @param string $level Log level
  60.      * @param string $message Log message
  61.      * @param array $context Additional context data
  62.      * @return bool True if log was written successfully
  63.      */
  64.     public function log(string $level, string $message, array $context = []): bool
  65.     {
  66.         $level = strtolower($level);
  67.  
  68.         // Check if level is valid
  69.         if (!isset(self::LEVELS[$level])) {
  70.             return false;
  71.         }
  72.  
  73.         // Check if level is above minimum level
  74.         if (self::LEVELS[$level] < self::LEVELS[$this->minLevel]) {
  75.             return false;
  76.         }
  77.  
  78.         // Replace context placeholders
  79.         $message = $this->interpolate($message, $context);
  80.  
  81.         // Format log entry
  82.         $entry = $this->formatLogEntry($level, $message, $context);
  83.  
  84.         // Write to log file
  85.         return file_put_contents($this->logFile, $entry, FILE_APPEND) !== false;
  86.     }
  87.  
  88.     /**
  89.      * Format a log entry
  90.      *
  91.      * @param string $level Log level
  92.      * @param string $message Log message
  93.      * @param array $context Additional context data
  94.      * @return string Formatted log entry
  95.      */
  96.     private function formatLogEntry(string $level, string $message, array $context): string
  97.     {
  98.         $date = date('Y-m-d H:i:s');
  99.         $level = strtoupper($level);
  100.  
  101.         $entry = '[' . $date . '] ' . $level . ': ' . $message;
  102.  
  103.         // Add context data if not empty
  104.         if (!empty($context)) {
  105.             $entry .= " " . json_encode($context);
  106.         }
  107.  
  108.         return $entry . PHP_EOL;
  109.     }
  110.  
  111.     /**
  112.      * Replace context placeholders in the message
  113.      *
  114.      * @param string $message Message with placeholders
  115.      * @param array $context Context data
  116.      * @return string Message with replaced placeholders
  117.      */
  118.     private function interpolate(string $message, array $context): string
  119.     {
  120.         // Build a replacement array with braces around the context keys
  121.         $replace = [];
  122.         foreach ($context as $key => $val) {
  123.             if (is_scalar($val) || (is_object($val) && method_exists($val, '__toString'))) {
  124.                 $replace['{' . $key . '}'] = $val;
  125.             }
  126.         }
  127.  
  128.         // Interpolate replacement values into the message and return
  129.         return strtr($message, $replace);
  130.     }
  131.  
  132.     /**
  133.      * Log a debug message
  134.      *
  135.      * @param string $message Log message
  136.      * @param array $context Additional context data
  137.      * @return bool True if log was written successfully
  138.      */
  139.     public function debug(string $message, array $context = []): bool
  140.     {
  141.         return $this->log('debug', $message, $context);
  142.     }
  143.  
  144.     /**
  145.      * Log an info message
  146.      *
  147.      * @param string $message Log message
  148.      * @param array $context Additional context data
  149.      * @return bool True if log was written successfully
  150.      */
  151.     public function info(string $message, array $context = []): bool
  152.     {
  153.         return $this->log('info', $message, $context);
  154.     }
  155.  
  156.     /**
  157.      * Log a notice message
  158.      *
  159.      * @param string $message Log message
  160.      * @param array $context Additional context data
  161.      * @return bool True if log was written successfully
  162.      */
  163.     public function notice(string $message, array $context = []): bool
  164.     {
  165.         return $this->log('notice', $message, $context);
  166.     }
  167.  
  168.     /**
  169.      * Log a warning message
  170.      *
  171.      * @param string $message Log message
  172.      * @param array $context Additional context data
  173.      * @return bool True if log was written successfully
  174.      */
  175.     public function warning(string $message, array $context = []): bool
  176.     {
  177.         return $this->log('warning', $message, $context);
  178.     }
  179.  
  180.     /**
  181.      * Log an error message
  182.      *
  183.      * @param string $message Log message
  184.      * @param array $context Additional context data
  185.      * @return bool True if log was written successfully
  186.      */
  187.     public function error(string $message, array $context = []): bool
  188.     {
  189.         return $this->log('error', $message, $context);
  190.     }
  191.  
  192.     /**
  193.      * Log a critical message
  194.      *
  195.      * @param string $message Log message
  196.      * @param array $context Additional context data
  197.      * @return bool True if log was written successfully
  198.      */
  199.     public function critical(string $message, array $context = []): bool
  200.     {
  201.         return $this->log('critical', $message, $context);
  202.     }
  203.  
  204.     /**
  205.      * Log an alert message
  206.      *
  207.      * @param string $message Log message
  208.      * @param array $context Additional context data
  209.      * @return bool True if log was written successfully
  210.      */
  211.     public function alert(string $message, array $context = []): bool
  212.     {
  213.         return $this->log('alert', $message, $context);
  214.     }
  215.  
  216.     /**
  217.      * Log an emergency message
  218.      *
  219.      * @param string $message Log message
  220.      * @param array $context Additional context data
  221.      * @return bool True if log was written successfully
  222.      */
  223.     public function emergency(string $message, array $context = []): bool
  224.     {
  225.         return $this->log('emergency', $message, $context);
  226.     }
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement