|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace V\Site\Logger; |
| 4 | + |
| 5 | +use Psr\Log\AbstractLogger; |
| 6 | +use Stringable; |
| 7 | +use TYPO3\CMS\Core\Core\Environment; |
| 8 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 9 | +use TYPO3\CMS\Extbase\Utility\DebuggerUtility; |
| 10 | + |
| 11 | +class HtmlFileLogger extends AbstractLogger |
| 12 | +{ |
| 13 | + private static int $logCalls = 0; |
| 14 | + |
| 15 | + public function __construct() |
| 16 | + { |
| 17 | + $directoryPath = Environment::getPublicPath() . '/logs/'; |
| 18 | + if (!is_dir($directoryPath)) { |
| 19 | + GeneralUtility::mkdir_deep($directoryPath); |
| 20 | + } |
| 21 | + } |
| 22 | + |
| 23 | + public function log($level, Stringable|string $message, array $context = []): void |
| 24 | + { |
| 25 | + self::$logCalls++; |
| 26 | + |
| 27 | + $backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
| 28 | + $normalizedClassName = ltrim(str_replace(['\\', '/'], '_', $backtrace[2]['class']), '_'); |
| 29 | + |
| 30 | + $logEntry = '<h3><strong>Message:</strong></h3>'; |
| 31 | + $logEntry .= '<p>' . $message . '</p>'; |
| 32 | + $logEntry .= '<h3>Data:</h3>'; |
| 33 | + $logEntry .= '<hr>'; |
| 34 | + if (self::$logCalls > 1) { |
| 35 | + $logEntry .= $this->getDebuggerUtilityCss(); |
| 36 | + } |
| 37 | + |
| 38 | + $logEntry .= DebuggerUtility::var_dump($context, null, 20, false, false, true); |
| 39 | + GeneralUtility::writeFile( |
| 40 | + Environment::getPublicPath() . '/logs/' . $normalizedClassName . '.html', |
| 41 | + $logEntry |
| 42 | + ); |
| 43 | + } |
| 44 | + |
| 45 | + /* |
| 46 | + * DebuggerUtility::var_dump does add css ony once on first call. But we need to add it every time because we store output in separate files. |
| 47 | + */ |
| 48 | + protected function getDebuggerUtilityCss(): string |
| 49 | + { |
| 50 | + return ' |
| 51 | + <style> |
| 52 | + .extbase-debugger-tree{position:relative} |
| 53 | + .extbase-debugger-tree input{position:absolute !important;float: none !important;top:0;left:0;height:14px;width:14px;margin:0 !important;cursor:pointer;opacity:0;z-index:2} |
| 54 | + .extbase-debugger-tree input~.extbase-debug-content{display:none} |
| 55 | + .extbase-debugger-tree .extbase-debug-header:before{position:relative;top:3px;content:"";padding:0;line-height:10px;height:12px;width:12px;text-align:center;margin:0 3px 0 0;background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkViZW5lXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMTIgMTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDEyIDEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PHN0eWxlIHR5cGU9InRleHQvY3NzIj4uc3Qwe2ZpbGw6Izg4ODg4ODt9PC9zdHlsZT48cGF0aCBpZD0iQm9yZGVyIiBjbGFzcz0ic3QwIiBkPSJNMTEsMTFIMFYwaDExVjExeiBNMTAsMUgxdjloOVYxeiIvPjxnIGlkPSJJbm5lciI+PHJlY3QgeD0iMiIgeT0iNSIgY2xhc3M9InN0MCIgd2lkdGg9IjciIGhlaWdodD0iMSIvPjxyZWN0IHg9IjUiIHk9IjIiIGNsYXNzPSJzdDAiIHdpZHRoPSIxIiBoZWlnaHQ9IjciLz48L2c+PC9zdmc+);display:inline-block} |
| 56 | + .extbase-debugger-tree input:checked~.extbase-debug-content{display:inline} |
| 57 | + .extbase-debugger-tree input:checked~.extbase-debug-header:before{background-image:url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz48c3ZnIHZlcnNpb249IjEuMSIgaWQ9IkViZW5lXzEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgeG1sbnM6eGxpbms9Imh0dHA6Ly93d3cudzMub3JnLzE5OTkveGxpbmsiIHg9IjBweCIgeT0iMHB4IiB2aWV3Qm94PSIwIDAgMTIgMTIiIHN0eWxlPSJlbmFibGUtYmFja2dyb3VuZDpuZXcgMCAwIDEyIDEyOyIgeG1sOnNwYWNlPSJwcmVzZXJ2ZSI+PHN0eWxlIHR5cGU9InRleHQvY3NzIj4uc3Qwe2ZpbGw6Izg4ODg4ODt9PC9zdHlsZT48cGF0aCBpZD0iQm9yZGVyIiBjbGFzcz0ic3QwIiBkPSJNMTEsMTFIMFYwaDExVjExeiBNMTAsMUgxdjloOVYxeiIvPjxnIGlkPSJJbm5lciI+PHJlY3QgeD0iMiIgeT0iNSIgY2xhc3M9InN0MCIgd2lkdGg9IjciIGhlaWdodD0iMSIvPjwvZz48L3N2Zz4=)} |
| 58 | + .extbase-debugger{display:block;text-align:left;background:#2a2a2a;border:1px solid #2a2a2a;box-shadow:0 3px 0 rgba(0,0,0,.5);color:#000;margin:20px;overflow:hidden;border-radius:4px} |
| 59 | + .extbase-debugger-floating{position:relative;z-index:99990} |
| 60 | + .extbase-debugger-top{background:#444;font-size:12px;font-family:monospace;color:#f1f1f1;padding:6px 15px} |
| 61 | + .extbase-debugger-center{padding:0 15px;margin:15px 0;background-image:repeating-linear-gradient(to bottom,transparent 0,transparent 20px,#252525 20px,#252525 40px)} |
| 62 | + .extbase-debugger-center,.extbase-debugger-center .extbase-debug-string,.extbase-debugger-center a,.extbase-debugger-center p,.extbase-debugger-center pre,.extbase-debugger-center strong{font-size:12px;font-weight:400;font-family:monospace;line-height:20px;color:#f1f1f1} |
| 63 | + .extbase-debugger-center pre{background-color:transparent;margin:0;padding:0;border:0;word-wrap:break-word;color:#999} |
| 64 | + .extbase-debugger-center .extbase-debug-string{color:#ce9178;white-space:normal} |
| 65 | + .extbase-debugger-center .extbase-debug-type{color:#569CD6;padding-right:4px} |
| 66 | + .extbase-debugger-center .extbase-debug-unregistered{background-color:#dce1e8} |
| 67 | + .extbase-debugger-center .extbase-debug-filtered,.extbase-debugger-center .extbase-debug-proxy,.extbase-debugger-center .extbase-debug-ptype,.extbase-debugger-center .extbase-debug-visibility,.extbase-debugger-center .extbase-debug-uninitialized,.extbase-debugger-center .extbase-debug-scope{color:#fff;font-size:10px;line-height:12px;padding:2px 4px;margin-right:2px;position:relative;top:-1px} |
| 68 | + .extbase-debugger-center .extbase-debug-scope{background-color:#497AA2} |
| 69 | + .extbase-debugger-center .extbase-debug-ptype{background-color:#698747} |
| 70 | + .extbase-debugger-center .extbase-debug-visibility{background-color:#6c0787} |
| 71 | + .extbase-debugger-center .extbase-debug-uninitialized{background-color:#698747} |
| 72 | + .extbase-debugger-center .extbase-debug-dirty{background-color:#FFFFB6} |
| 73 | + .extbase-debugger-center .extbase-debug-filtered{background-color:#4F4F4F} |
| 74 | + .extbase-debugger-center .extbase-debug-seeabove{text-decoration:none;font-style:italic} |
| 75 | + .extbase-debugger-center .extbase-debug-property{color:#f1f1f1} |
| 76 | + .extbase-debugger-center .extbase-debug-closure{color:#9BA223;} |
| 77 | + </style>'; |
| 78 | + } |
| 79 | +} |
0 commit comments