Add new exception log format
authorTim Düsterhus <duesterhus@woltlab.com>
Tue, 29 Sep 2015 20:04:30 +0000 (22:04 +0200)
committerTim Düsterhus <duesterhus@woltlab.com>
Tue, 1 Dec 2015 21:15:48 +0000 (22:15 +0100)
wcfsetup/install/files/lib/acp/page/ExceptionLogViewPage.class.php
wcfsetup/install/files/lib/core.functions.php

index 8c15b577f693a3c335114ad3e1779812ac4d4a91..03f8e279128615d6f8f8261bc70664ca17b24309 100644 (file)
@@ -134,6 +134,7 @@ class ExceptionLogViewPage extends MultipleLinkPage {
                $this->calculateNumberOfPages();
                
                $i = 0;
+               // TODO: This needs to be adapted for WCF 2.2
                $exceptionRegex = new Regex('(?P<date>[MTWFS][a-z]{2}, \d{1,2} [JFMASOND][a-z]{2} \d{4} \d{2}:\d{2}:\d{2} [+-]\d{4})
 Message: (?P<message>.*?)
 File: (?P<file>.*?) \((?P<line>\d+)\)
index 5d586439fbfd7902b75a953f06871366ad2011ac..4649cabdd9c77c102d324510369b2ffe7315ebc7 100644 (file)
@@ -48,8 +48,55 @@ namespace wcf\functions\exception {
        use wcf\util\FileUtil;
        use wcf\util\StringUtil;
        
+       function logThrowable($e) {
+               $logFile = WCF_DIR . 'log/' . gmdate('Y-m-d', TIME_NOW) . '.txt';
+               touch($logFile);
+               
+               // don't forget to update ExceptionLogViewPage, when changing the log file format
+               $message = gmdate('r', TIME_NOW)."\n".
+                       'Message: '.str_replace("\n", ' ', $e->getMessage())."\n".
+                       'PHP version: '.phpversion()."\n".
+                       'WCF version: '.WCF_VERSION."\n".
+                       'Request URI: '.(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '')."\n".
+                       'Referrer: '.(isset($_SERVER['HTTP_REFERER']) ? str_replace("\n", ' ', $_SERVER['HTTP_REFERER']) : '')."\n".
+                       'User Agent: '.(isset($_SERVER['HTTP_USER_AGENT']) ? str_replace("\n", ' ', $_SERVER['HTTP_USER_AGENT']) : '')."\n".
+                       'Peak Memory Usage: '.memory_get_peak_usage().'/'.FileUtil::getMemoryLimit()."\n";
+               do {
+                       $message .= "======\n".
+                       'Error Class: '.get_class($e)."\n".
+                       'Error Message: '.str_replace("\n", ' ', $e->getMessage())."\n".
+                       'Error Code: '.intval($e->getCode())."\n".
+                       'File: '.str_replace("\n", ' ', $e->getFile()).' ('.$e->getLine().')'."\n".
+                       'Extra Information: '.($e instanceof IExtraInformationException ? base64_encode(serialize($e->getExtraInformation())) : '-')."\n".
+                       'Stack Trace: '.base64_encode(serialize(array_map(function ($item) {
+                               $item['args'] = array_map(function ($item) {
+                                       switch (gettype($item)) {
+                                               case 'object':
+                                                       return get_class($item);
+                                               case 'array':
+                                                       return array_map(function () {
+                                                               return '[redacted]';
+                                                       }, $item);
+                                               default:
+                                                       return $item;
+                                       }
+                               }, $item['args']);
+                               
+                               return $item;
+                       }, sanitizeStacktrace($e, true))))."\n";
+               }
+               while ($e = $e->getPrevious());
+               
+               // calculate Exception-ID
+               $exceptionID = sha1($message);
+               $entry = "<<<<<<<<".$exceptionID."<<<<\n".$message."<<<<\n";
+               
+               file_put_contents($logFile, $entry, FILE_APPEND);
+               return $exceptionID;
+       }
+       
        function printThrowable($e) {
-               $exceptionID = '123456'; // TODO
+               $exceptionID = logThrowable($e); // TODO
        ?><!DOCTYPE html>
        <html>
                <head>