Merge branch '130-logger-fix' into 'development'

Implemented a proper error_logger.

Closes #130

See merge request fuzeworks/core!66
This commit is contained in:
Abel Hoogeveen 2018-11-16 22:40:03 +00:00
commit 8bbbcbfc81
4 changed files with 96 additions and 18 deletions

View File

@ -36,8 +36,8 @@
return array(
'fuzeworks_error_reporting' => true,
'tracy_error_reporting' => true,
'php_error_reporting' => false,
'log_to_file' => false,
'log_errors_to_file' => true,
'log_last_request_to_file' => false,
'logger_template' => 'logger_default',
);

View File

@ -36,7 +36,6 @@
namespace FuzeWorks;
use FuzeWorks\Exception\ConfiguratorException;
use FuzeWorks\Exception\Exception;
/**
@ -66,11 +65,18 @@ class Logger {
private static $print_to_screen = false;
/**
* whether to output the log to a file after FuzeWorks has run.
* whether to output the log of the last entire request to a file after FuzeWorks has run.
*
* @var bool
*/
private static $log_to_file = false;
private static $log_last_request = false;
/**
* Whether to output the log of all errors to a file after FuzeWorks has run
*
* @var bool
*/
private static $log_errors_to_file = false;
/**
* The template to use when parsing the debug log
@ -117,10 +123,6 @@ class Logger {
set_error_handler(array('\FuzeWorks\Logger', 'errorHandler'), E_ALL);
set_Exception_handler(array('\FuzeWorks\Logger', 'exceptionHandler'));
}
elseif ($cfg_error->tracy_error_reporting == true && self::$useTracy === true)
{
// Register with tracy
}
// @codeCoverageIgnoreEnd
// Set PHP error reporting
@ -131,7 +133,8 @@ class Logger {
// Set the environment variables
self::$debug = (ENVIRONMENT === 'DEVELOPMENT');
self::$log_to_file = $cfg_error->log_to_file;
self::$log_last_request = $cfg_error->log_last_request_to_file;
self::$log_errors_to_file = $cfg_error->log_errors_to_file;
self::$logger_template = $cfg_error->logger_template;
self::newLevel('Logger Initiated');
@ -159,10 +162,13 @@ class Logger {
self::logToScreen();
}
if (self::$log_to_file == true)
if (self::$log_last_request == true)
{
self::logToFile();
self::logLastRequest();
}
if (self::$log_errors_to_file == true)
self::logErrorsToFile();
}
/**
@ -273,18 +279,38 @@ class Logger {
* Output the entire log to a file. Used for debugging problems with your code.
* @codeCoverageIgnore
*/
public static function logToFile()
public static function logLastRequest()
{
ob_start(function () {});
$logs = self::$Logs;
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.logger_cli.php');
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.logger_file.php');
$contents = ob_get_clean();
$file = Core::$logDir . DS . 'log_latest.php';
$file = Core::$logDir . DS . 'fwlog_request.log';
if (is_writable(dirname($file))) {
file_put_contents($file, '<?php ' . $contents);
file_put_contents($file, $contents);
}
}
/**
* Output all erros to a file. Used for tracking all errors in FuzeWorks and associated code
* @codeCoverageIgnore
*/
public static function logErrorsToFile()
{
ob_start(function() {});
$logs = [];
foreach (self::$Logs as $log)
{
if ($log['type'] === 'ERROR')
$logs[] = $log;
}
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.logger_file.php');
$contents = ob_get_clean();
$file = Core::$logDir . DS . 'fwlog_errors.log';
if (is_writable(dirname($file)))
file_put_contents($file, $contents, FILE_APPEND | LOCK_EX);
}
/* =========================================LOGGING METHODS============================================================== */
/**

View File

@ -0,0 +1,52 @@
<?php
/**
* FuzeWorks Framework Core.
*
* The FuzeWorks PHP FrameWork
*
* Copyright (C) 2013-2018 TechFuze
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* @author TechFuze
* @copyright Copyright (c) 2013 - 2018, Techfuze. (http://techfuze.net)
* @license https://opensource.org/licenses/MIT MIT License
*
* @link http://techfuze.net/fuzeworks
* @since Version 1.2.0
*
* @version Version 1.2.0
*/
$mask = "|%5s |%-95s |\n";
$id = 1;
if (!empty($logs))
printf($mask, $id, ' REQUEST ' . date('Y-m-d H:i') . '-'.substr(sha1(uniqid()), 0, 8).'');
foreach ($logs as $log) {
$id++;
$string = '';
$string .= '[ERROR]' . ' - ';
$string .= $log['message'];
}
if (!empty($logs))
printf($mask. $id);

View File

@ -129,10 +129,10 @@ class configuratorTest extends CoreTestAbstract
// Create a log and write off to file
Logger::log('Test log for the file');
Logger::logToFile();
Logger::logLastRequest();
// Assert if exist
$this->assertTrue($fs->hasChild('log_latest.php'));
$this->assertTrue($fs->hasChild('fwlog_request.log'));
}
/**