| @@ -2,157 +2,175 @@ | ||
| 2 | 2 | namespace Forge12\Shared; |
| 3 | 3 | |
| 4 | 4 | // Sicherstellen, dass Interface existiert |
| 5 | 5 | if (! interface_exists('Forge12\Shared\LoggerInterface')) { |
| 6 | - require_once __DIR__ . '/logger.interface.php'; | |
| 6 | + require_once __DIR__ . '/logger.interface.php'; | |
| 7 | 7 | } |
| 8 | 8 | |
| 9 | +if (!defined('F12_DEBUG')) { | |
| 10 | + define('F12_DEBUG', false); | |
| 11 | +} | |
| 12 | + | |
| 13 | +if (!defined('F12_DEBUG_LOG_LEVEL')) { | |
| 14 | + define('F12_DEBUG_LOG_LEVEL', 200); | |
| 15 | +} | |
| 16 | + | |
| 9 | 17 | if ( ! class_exists( 'Forge12\Shared\Logger' ) ) { |
| 10 | - class Logger implements LoggerInterface { | |
| 11 | - private static $instance; | |
| 12 | - private $log_file; | |
| 13 | - private $log_level; | |
| 14 | - private $log_dir; | |
| 18 | + class Logger implements LoggerInterface { | |
| 19 | + private static $instance; | |
| 20 | + private $log_file; | |
| 21 | + private $log_level; | |
| 22 | + private $log_dir; | |
| 15 | 23 | |
| 16 | - const DEBUG = 100; | |
| 17 | - const INFO = 200; | |
| 18 | - const NOTICE = 250; | |
| 19 | - const WARNING = 300; | |
| 20 | - const ERROR = 400; | |
| 21 | - const CRITICAL = 500; | |
| 24 | + const DEBUG = 100; | |
| 25 | + const INFO = 200; | |
| 26 | + const NOTICE = 250; | |
| 27 | + const WARNING = 300; | |
| 28 | + const ERROR = 400; | |
| 29 | + const CRITICAL = 500; | |
| 22 | 30 | |
| 23 | - private function __construct() { | |
| 24 | - $upload_dir = wp_upload_dir(); | |
| 25 | - $this->log_dir = $upload_dir['basedir'] . '/f12-logs'; | |
| 31 | + private function __construct() { | |
| 32 | + if ( ! defined( 'F12_DEBUG' ) || ! F12_DEBUG ) { | |
| 33 | + // Wenn F12_DEBUG nicht definiert ist oder false, beenden wir. | |
| 34 | + return; | |
| 35 | + } | |
| 26 | 36 | |
| 27 | - if ( ! file_exists( $this->log_dir ) ) { | |
| 28 | - wp_mkdir_p( $this->log_dir ); | |
| 29 | - } | |
| 37 | + $upload_dir = wp_upload_dir(); | |
| 38 | + $this->log_dir = $upload_dir['basedir'] . '/f12-logs'; | |
| 30 | 39 | |
| 31 | - // Tagesbasiert loggen | |
| 32 | - $this->log_file = $this->log_dir . '/plugins-' . date('Y-m-d') . '.log'; | |
| 40 | + if ( ! file_exists( $this->log_dir ) ) { | |
| 41 | + wp_mkdir_p( $this->log_dir ); | |
| 42 | + } | |
| 33 | 43 | |
| 34 | - // Setze Level je nach WP_DEBUG | |
| 35 | - $this->log_level = defined( 'WP_DEBUG' ) && WP_DEBUG ? self::DEBUG : self::INFO; | |
| 36 | - } | |
| 44 | + // Tagesbasiert loggen | |
| 45 | + $this->log_file = $this->log_dir . '/plugins-' . date('Y-m-d') . '.log'; | |
| 37 | 46 | |
| 38 | - public static function getInstance() { | |
| 39 | - if ( ! self::$instance ) { | |
| 40 | - self::$instance = new self(); | |
| 41 | - } | |
| 42 | - return self::$instance; | |
| 43 | - } | |
| 47 | + // Setze Level je nach WP_DEBUG | |
| 48 | + $this->log_level = F12_DEBUG_LOG_LEVEL; | |
| 49 | + } | |
| 44 | 50 | |
| 45 | - private function sanitizeContext(array $context): array { | |
| 46 | - foreach ($context as $key => $value) { | |
| 47 | - if (in_array(strtolower($key), ['ip', 'user_ip'])) { | |
| 48 | - $context[$key] = $this->mask_ip($value); | |
| 49 | - } | |
| 50 | - if (in_array(strtolower($key), ['email', 'user_email'])) { | |
| 51 | - $context[$key] = $this->mask_email($value); | |
| 52 | - } | |
| 53 | - if (in_array(strtolower($key), ['password', 'pwd'])) { | |
| 54 | - $context[$key] = $this->mask_password($value); | |
| 55 | - } | |
| 56 | - } | |
| 57 | - return $context; | |
| 58 | - } | |
| 51 | + public static function getInstance() { | |
| 52 | + if ( ! self::$instance ) { | |
| 53 | + self::$instance = new self(); | |
| 54 | + } | |
| 55 | + return self::$instance; | |
| 56 | + } | |
| 59 | 57 | |
| 60 | - private function mask_password(string $password): string { | |
| 61 | - return '********'; | |
| 62 | - } | |
| 58 | + private function sanitizeContext(array $context): array { | |
| 59 | + foreach ($context as $key => $value) { | |
| 60 | + if (in_array(strtolower($key), ['ip', 'user_ip'])) { | |
| 61 | + $context[$key] = $this->mask_ip($value); | |
| 62 | + } | |
| 63 | + if (in_array(strtolower($key), ['email', 'user_email'])) { | |
| 64 | + $context[$key] = $this->mask_email($value); | |
| 65 | + } | |
| 66 | + if (in_array(strtolower($key), ['password', 'pwd'])) { | |
| 67 | + $context[$key] = $this->mask_password($value); | |
| 68 | + } | |
| 69 | + } | |
| 70 | + return $context; | |
| 71 | + } | |
| 63 | 72 | |
| 64 | - private function mask_email(string $email): string { | |
| 65 | - if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| 66 | - return 'invalid'; | |
| 67 | - } | |
| 68 | - [$user, $domain] = explode('@', $email, 2); | |
| 69 | - $len = strlen($user); | |
| 70 | - if ($len <= 2) { | |
| 71 | - $maskedUser = substr($user, 0, 1) . '*'; | |
| 72 | - } else { | |
| 73 | - $maskedUser = substr($user, 0, 1) . str_repeat('*', $len - 2) . substr($user, -1); | |
| 74 | - } | |
| 75 | - return $maskedUser . '@' . $domain; | |
| 76 | - } | |
| 73 | + private function mask_password(string $password): string { | |
| 74 | + return '********'; | |
| 75 | + } | |
| 77 | 76 | |
| 78 | - private function mask_ip(string $ip): string { | |
| 79 | - if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
| 80 | - $parts = explode('.', $ip); | |
| 81 | - $parts[3] = '0'; | |
| 82 | - return implode('.', $parts); | |
| 83 | - } | |
| 84 | - if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | |
| 85 | - return substr($ip, 0, 20) . '::'; | |
| 86 | - } | |
| 87 | - return 'unknown'; | |
| 88 | - } | |
| 77 | + private function mask_email(string $email): string { | |
| 78 | + if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { | |
| 79 | + return 'invalid'; | |
| 80 | + } | |
| 81 | + [$user, $domain] = explode('@', $email, 2); | |
| 82 | + $len = strlen($user); | |
| 83 | + if ($len <= 2) { | |
| 84 | + $maskedUser = substr($user, 0, 1) . '*'; | |
| 85 | + } else { | |
| 86 | + $maskedUser = substr($user, 0, 1) . str_repeat('*', $len - 2) . substr($user, -1); | |
| 87 | + } | |
| 88 | + return $maskedUser . '@' . $domain; | |
| 89 | + } | |
| 89 | 90 | |
| 90 | - private function rotateLogs(int $maxSize = 52428800, int $maxFiles = 5): void { | |
| 91 | - if (file_exists($this->log_file) && filesize($this->log_file) > $maxSize) { | |
| 92 | - for ($i = $maxFiles - 1; $i >= 1; $i--) { | |
| 93 | - $old = $this->log_file . '.' . $i; | |
| 94 | - $new = $this->log_file . '.' . ($i + 1); | |
| 95 | - if (file_exists($old)) { | |
| 96 | - rename($old, $new); | |
| 97 | - } | |
| 98 | - } | |
| 99 | - rename($this->log_file, $this->log_file . '.1'); | |
| 100 | - } | |
| 101 | - } | |
| 91 | + private function mask_ip(string $ip): string { | |
| 92 | + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) { | |
| 93 | + $parts = explode('.', $ip); | |
| 94 | + $parts[3] = '0'; | |
| 95 | + return implode('.', $parts); | |
| 96 | + } | |
| 97 | + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) { | |
| 98 | + return substr($ip, 0, 20) . '::'; | |
| 99 | + } | |
| 100 | + return 'unknown'; | |
| 101 | + } | |
| 102 | 102 | |
| 103 | - private function cleanupOldLogs(int $days = 7): void { | |
| 104 | - foreach (glob($this->log_dir . '/plugins-*.log*') as $file) { | |
| 105 | - if (filemtime($file) < strtotime("-{$days} days")) { | |
| 106 | - @unlink($file); | |
| 107 | - } | |
| 108 | - } | |
| 109 | - } | |
| 103 | + private function rotateLogs(int $maxSize = 52428800, int $maxFiles = 5): void { | |
| 104 | + if (file_exists($this->log_file) && filesize($this->log_file) > $maxSize) { | |
| 105 | + for ($i = $maxFiles - 1; $i >= 1; $i--) { | |
| 106 | + $old = $this->log_file . '.' . $i; | |
| 107 | + $new = $this->log_file . '.' . ($i + 1); | |
| 108 | + if (file_exists($old)) { | |
| 109 | + rename($old, $new); | |
| 110 | + } | |
| 111 | + } | |
| 112 | + rename($this->log_file, $this->log_file . '.1'); | |
| 113 | + } | |
| 114 | + } | |
| 110 | 115 | |
| 111 | - private function writeLog( $level, $levelName, $message, array $context = [] ) { | |
| 112 | - if ( $level < $this->log_level ) { | |
| 113 | - return; | |
| 114 | - } | |
| 116 | + private function cleanupOldLogs(int $days = 7): void { | |
| 117 | + foreach (glob($this->log_dir . '/plugins-*.log*') as $file) { | |
| 118 | + if (filemtime($file) < strtotime("-{$days} days")) { | |
| 119 | + @unlink($file); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + } | |
| 115 | 123 | |
| 116 | - // Cleanup & Rotation bei jedem Schreiben | |
| 117 | - $this->cleanupOldLogs(); | |
| 118 | - $this->rotateLogs(); | |
| 124 | + private function writeLog( $level, $levelName, $message, array $context = [] ) { | |
| 125 | + if ( ! defined( 'F12_DEBUG' ) || ! F12_DEBUG ) { | |
| 126 | + // Wenn F12_DEBUG nicht definiert ist oder false, beenden wir. | |
| 127 | + return; | |
| 128 | + } | |
| 119 | 129 | |
| 120 | - $context = $this->sanitizeContext($context); | |
| 130 | + if ( $level < $this->log_level ) { | |
| 131 | + return; | |
| 132 | + } | |
| 121 | 133 | |
| 122 | - $time = date( 'Y-m-d H:i:s' ); | |
| 123 | - $plugin = $context['plugin'] ?? 'unknown'; | |
| 124 | - $msg = sprintf( "[%s] [%s] [%s] %s %s\n", | |
| 125 | - $time, | |
| 126 | - strtoupper( $levelName ), | |
| 127 | - $plugin, | |
| 128 | - $message, | |
| 129 | - $context ? json_encode( $context ) : '' | |
| 130 | - ); | |
| 131 | - error_log( $msg, 3, $this->log_file ); | |
| 132 | - } | |
| 134 | + // Cleanup & Rotation bei jedem Schreiben | |
| 135 | + $this->cleanupOldLogs(); | |
| 136 | + $this->rotateLogs(); | |
| 133 | 137 | |
| 134 | - public function debug( $message, array $context = [] ) :void { | |
| 135 | - $this->writeLog( self::DEBUG, 'DEBUG', $message, $context ); | |
| 136 | - } | |
| 138 | + $context = $this->sanitizeContext($context); | |
| 137 | 139 | |
| 138 | - public function info( $message, array $context = [] ) : void { | |
| 139 | - $this->writeLog( self::INFO, 'INFO', $message, $context ); | |
| 140 | - } | |
| 140 | + $time = date( 'Y-m-d H:i:s' ); | |
| 141 | + $plugin = $context['plugin'] ?? 'unknown'; | |
| 142 | + $msg = sprintf( "[%s] [%s] [%s] %s %s\n", | |
| 143 | + $time, | |
| 144 | + strtoupper( $levelName ), | |
| 145 | + $plugin, | |
| 146 | + $message, | |
| 147 | + $context ? json_encode( $context ) : '' | |
| 148 | + ); | |
| 149 | + error_log( $msg, 3, $this->log_file ); | |
| 150 | + } | |
| 141 | 151 | |
| 142 | - public function error( $message, array $context = [] ) : void { | |
| 143 | - $this->writeLog( self::ERROR, 'ERROR', $message, $context ); | |
| 144 | - } | |
| 152 | + public function debug( $message, array $context = [] ) :void { | |
| 153 | + $this->writeLog( self::DEBUG, 'DEBUG', $message, $context ); | |
| 154 | + } | |
| 145 | 155 | |
| 146 | - public function warning( $message, array $context = [] ) : void { | |
| 147 | - $this->writeLog( self::WARNING, 'WARNING', $message, $context ); | |
| 148 | - } | |
| 156 | + public function info( $message, array $context = [] ) : void { | |
| 157 | + $this->writeLog( self::INFO, 'INFO', $message, $context ); | |
| 158 | + } | |
| 149 | 159 | |
| 150 | - public function notice( $message, array $context = [] ) : void { | |
| 151 | - $this->writeLog( self::NOTICE, 'NOTICE', $message, $context ); | |
| 152 | - } | |
| 160 | + public function error( $message, array $context = [] ) : void { | |
| 161 | + $this->writeLog( self::ERROR, 'ERROR', $message, $context ); | |
| 162 | + } | |
| 153 | 163 | |
| 154 | - public function critical( $message, array $context = [] ) : void { | |
| 155 | - $this->writeLog( self::CRITICAL, 'CRITICAL', $message, $context ); | |
| 156 | - } | |
| 157 | - } | |
| 164 | + public function warning( $message, array $context = [] ) : void { | |
| 165 | + $this->writeLog( self::WARNING, 'WARNING', $message, $context ); | |
| 166 | + } | |
| 167 | + | |
| 168 | + public function notice( $message, array $context = [] ) : void { | |
| 169 | + $this->writeLog( self::NOTICE, 'NOTICE', $message, $context ); | |
| 170 | + } | |
| 171 | + | |
| 172 | + public function critical( $message, array $context = [] ) : void { | |
| 173 | + $this->writeLog( self::CRITICAL, 'CRITICAL', $message, $context ); | |
| 174 | + } | |
| 175 | + } | |
| 158 | 176 | } |