Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Services;
- /**
- * Logger Service
- *
- * This class provides methods for logging messages to files.
- *
- * @package App\Services
- */
- class Logger
- {
- /**
- * @var string Log file path
- */
- private string $logFile;
- /**
- * @var string Minimum log level
- */
- private string $minLevel;
- /**
- * @var array Valid log levels
- */
- private const array LEVELS = [
- 'debug' => 0,
- 'info' => 1,
- 'notice' => 2,
- 'warning' => 3,
- 'error' => 4,
- 'critical' => 5,
- 'alert' => 6,
- 'emergency' => 7
- ];
- /**
- * Logger constructor
- *
- * @param string|null $logFile Path to the log file
- * @param string $minLevel Minimum log level
- */
- public function __construct(string $logFile = null, string $minLevel = 'debug')
- {
- $this->logFile = $logFile ?? dirname(__DIR__, 2) . '/logs/app.log';
- $this->minLevel = strtolower($minLevel);
- // Create logs directory if it doesn't exist
- $logDir = dirname($this->logFile);
- if (!is_dir($logDir)) {
- mkdir($logDir, 0755, true);
- }
- }
- /**
- * Log a message
- *
- * @param string $level Log level
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function log(string $level, string $message, array $context = []): bool
- {
- $level = strtolower($level);
- // Check if level is valid
- if (!isset(self::LEVELS[$level])) {
- return false;
- }
- // Check if level is above minimum level
- if (self::LEVELS[$level] < self::LEVELS[$this->minLevel]) {
- return false;
- }
- // Replace context placeholders
- $message = $this->interpolate($message, $context);
- // Format log entry
- $entry = $this->formatLogEntry($level, $message, $context);
- // Write to log file
- return file_put_contents($this->logFile, $entry, FILE_APPEND) !== false;
- }
- /**
- * Format a log entry
- *
- * @param string $level Log level
- * @param string $message Log message
- * @param array $context Additional context data
- * @return string Formatted log entry
- */
- private function formatLogEntry(string $level, string $message, array $context): string
- {
- $date = date('Y-m-d H:i:s');
- $level = strtoupper($level);
- $entry = '[' . $date . '] ' . $level . ': ' . $message;
- // Add context data if not empty
- if (!empty($context)) {
- $entry .= " " . json_encode($context);
- }
- return $entry . PHP_EOL;
- }
- /**
- * Replace context placeholders in the message
- *
- * @param string $message Message with placeholders
- * @param array $context Context data
- * @return string Message with replaced placeholders
- */
- private function interpolate(string $message, array $context): string
- {
- // Build a replacement array with braces around the context keys
- $replace = [];
- foreach ($context as $key => $val) {
- if (is_scalar($val) || (is_object($val) && method_exists($val, '__toString'))) {
- $replace['{' . $key . '}'] = $val;
- }
- }
- // Interpolate replacement values into the message and return
- return strtr($message, $replace);
- }
- /**
- * Log a debug message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function debug(string $message, array $context = []): bool
- {
- return $this->log('debug', $message, $context);
- }
- /**
- * Log an info message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function info(string $message, array $context = []): bool
- {
- return $this->log('info', $message, $context);
- }
- /**
- * Log a notice message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function notice(string $message, array $context = []): bool
- {
- return $this->log('notice', $message, $context);
- }
- /**
- * Log a warning message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function warning(string $message, array $context = []): bool
- {
- return $this->log('warning', $message, $context);
- }
- /**
- * Log an error message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function error(string $message, array $context = []): bool
- {
- return $this->log('error', $message, $context);
- }
- /**
- * Log a critical message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function critical(string $message, array $context = []): bool
- {
- return $this->log('critical', $message, $context);
- }
- /**
- * Log an alert message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function alert(string $message, array $context = []): bool
- {
- return $this->log('alert', $message, $context);
- }
- /**
- * Log an emergency message
- *
- * @param string $message Log message
- * @param array $context Additional context data
- * @return bool True if log was written successfully
- */
- public function emergency(string $message, array $context = []): bool
- {
- return $this->log('emergency', $message, $context);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement