DateManager.php
3 weeks ago
EventManager.php
1 week ago
LogManager.php
3 weeks ago
OptionManager.php
3 weeks ago
PolicyManager.php
3 weeks ago
LogManager.php
173 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * File-based logger writing leveled messages to a configurable log path. |
| 5 | * Supports debug, info, warning, error, and other PSR-like severity levels with timestamps. |
| 6 | * Defaults to framework.log in the application base path. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Managers |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Managers; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use DateTime; |
| 16 | use Kirki\Framework\Supports\Facades\File; |
| 17 | use function Kirki\Framework\base_path; |
| 18 | class LogManager |
| 19 | { |
| 20 | /** |
| 21 | * The path. |
| 22 | * |
| 23 | * @var string |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | protected string $path; |
| 28 | /** |
| 29 | * Create a new instance. |
| 30 | * |
| 31 | * @param ?string $path The path. |
| 32 | * |
| 33 | * @return void |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | public function __construct(?string $path = null) |
| 38 | { |
| 39 | $this->path = $path ?? base_path('framework.log'); |
| 40 | } |
| 41 | /** |
| 42 | * Log a debug message. |
| 43 | * |
| 44 | * @param mixed $message The message. |
| 45 | * |
| 46 | * @return void |
| 47 | * |
| 48 | * @since 1.0.0 |
| 49 | */ |
| 50 | public function debug($message) |
| 51 | { |
| 52 | $this->write($message, 'debug'); |
| 53 | } |
| 54 | /** |
| 55 | * Log an info message. |
| 56 | * |
| 57 | * @param mixed $message The message. |
| 58 | * |
| 59 | * @return void |
| 60 | * |
| 61 | * @since 1.0.0 |
| 62 | */ |
| 63 | public function info($message) |
| 64 | { |
| 65 | $this->write($message, 'info'); |
| 66 | } |
| 67 | /** |
| 68 | * Log a warning message. |
| 69 | * |
| 70 | * @param mixed $message The message. |
| 71 | * |
| 72 | * @return void |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function warning($message) |
| 77 | { |
| 78 | $this->write($message, 'warning'); |
| 79 | } |
| 80 | /** |
| 81 | * Log an error message. |
| 82 | * |
| 83 | * @param mixed $message The message. |
| 84 | * |
| 85 | * @return void |
| 86 | * |
| 87 | * @since 1.0.0 |
| 88 | */ |
| 89 | public function error($message) |
| 90 | { |
| 91 | $this->write($message, 'error'); |
| 92 | } |
| 93 | /** |
| 94 | * Log an emergency message. |
| 95 | * |
| 96 | * @param mixed $message The message. |
| 97 | * |
| 98 | * @return void |
| 99 | * |
| 100 | * @since 1.0.0 |
| 101 | */ |
| 102 | public function emergency($message) |
| 103 | { |
| 104 | $this->write($message, 'emergency'); |
| 105 | } |
| 106 | /** |
| 107 | * Log a critical message. |
| 108 | * |
| 109 | * @param mixed $message The message. |
| 110 | * |
| 111 | * @return void |
| 112 | * |
| 113 | * @since 1.0.0 |
| 114 | */ |
| 115 | public function critical($message) |
| 116 | { |
| 117 | $this->write($message, 'critical'); |
| 118 | } |
| 119 | /** |
| 120 | * Log an alert message. |
| 121 | * |
| 122 | * @param mixed $message The message. |
| 123 | * |
| 124 | * @return void |
| 125 | * |
| 126 | * @since 1.0.0 |
| 127 | */ |
| 128 | public function alert($message) |
| 129 | { |
| 130 | $this->write($message, 'alert'); |
| 131 | } |
| 132 | /** |
| 133 | * Clear the log file. |
| 134 | * |
| 135 | * @return void |
| 136 | * |
| 137 | * @since 1.0.0 |
| 138 | */ |
| 139 | public function clear() |
| 140 | { |
| 141 | @\file_put_contents($this->path, ''); |
| 142 | } |
| 143 | /** |
| 144 | * Format the message. |
| 145 | * |
| 146 | * @param mixed $message The message. |
| 147 | * @param mixed $type The type. |
| 148 | * |
| 149 | * @return string |
| 150 | * |
| 151 | * @since 1.0.0 |
| 152 | */ |
| 153 | protected function format($message, $type) |
| 154 | { |
| 155 | return \sprintf("[%s] [%s] %s\n", (new DateTime())->format('Y-m-d H:i:s'), \strtoupper($type), $message); |
| 156 | } |
| 157 | /** |
| 158 | * Write the message to the log file. |
| 159 | * |
| 160 | * @param mixed $message The message. |
| 161 | * @param mixed $type The type. |
| 162 | * |
| 163 | * @return void |
| 164 | * |
| 165 | * @since 1.0.0 |
| 166 | */ |
| 167 | protected function write($message, $type) |
| 168 | { |
| 169 | File::make_dir($this->path); |
| 170 | @\file_put_contents($this->path, $this->format($message, $type), \FILE_APPEND); |
| 171 | } |
| 172 | } |
| 173 |