2015-02-08 16:29:39 +00:00
|
|
|
<?php
|
2015-08-29 15:43:13 +00:00
|
|
|
/**
|
2018-10-07 21:02:18 +00:00
|
|
|
* FuzeWorks Framework Core.
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* The FuzeWorks PHP FrameWork
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2019-01-10 22:59:00 +00:00
|
|
|
* Copyright (C) 2013-2019 TechFuze
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* 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:
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* The above copyright notice and this permission notice shall be included in all
|
|
|
|
* copies or substantial portions of the Software.
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* 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.
|
2015-08-29 15:43:13 +00:00
|
|
|
*
|
2016-05-07 17:22:09 +00:00
|
|
|
* @author TechFuze
|
2019-01-10 22:59:00 +00:00
|
|
|
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
|
2018-10-07 21:02:18 +00:00
|
|
|
* @license https://opensource.org/licenses/MIT MIT License
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2016-07-23 15:06:19 +00:00
|
|
|
* @link http://techfuze.net/fuzeworks
|
2016-05-07 17:22:09 +00:00
|
|
|
* @since Version 0.0.1
|
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* @version Version 1.2.0
|
2015-08-29 15:43:13 +00:00
|
|
|
*/
|
2015-02-08 16:29:39 +00:00
|
|
|
|
2015-04-29 15:18:33 +00:00
|
|
|
namespace FuzeWorks;
|
2016-07-11 10:44:13 +00:00
|
|
|
|
2019-01-21 19:44:53 +00:00
|
|
|
use FuzeWorks\Exception\ConfigException;
|
2019-01-16 22:07:09 +00:00
|
|
|
use FuzeWorks\Exception\EventException;
|
2017-12-21 23:06:01 +00:00
|
|
|
use FuzeWorks\Exception\Exception;
|
2016-07-11 10:44:13 +00:00
|
|
|
|
2015-04-29 15:49:33 +00:00
|
|
|
/**
|
2016-05-07 17:22:09 +00:00
|
|
|
* Logger Class.
|
2015-04-29 15:49:33 +00:00
|
|
|
*
|
|
|
|
* The main tool to handle errors and exceptions. Provides some tools for debugging and tracking where errors take place
|
2015-08-29 15:43:13 +00:00
|
|
|
* All fatal errors get catched by this class and get displayed if configured to do so.
|
2016-05-12 10:17:43 +00:00
|
|
|
* Also provides utilities to benchmark the application.
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2018-10-07 21:02:18 +00:00
|
|
|
* @author TechFuze <contact@techfuze.net>
|
2019-01-10 22:59:00 +00:00
|
|
|
* @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net)
|
2015-04-29 15:49:33 +00:00
|
|
|
*/
|
2016-05-12 10:17:43 +00:00
|
|
|
class Logger {
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* All log entries, unsorted.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static $logs = [];
|
2016-05-07 17:22:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* whether to output the log after FuzeWorks has run.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $print_to_screen = false;
|
|
|
|
|
2019-03-04 20:18:08 +00:00
|
|
|
/**
|
|
|
|
* Whether the Logger has been enabled or not
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
private static $isEnabled = false;
|
|
|
|
|
2016-06-02 13:19:31 +00:00
|
|
|
/**
|
2018-11-16 22:34:32 +00:00
|
|
|
* whether to output the log of the last entire request to a file after FuzeWorks has run.
|
2016-06-02 13:19:31 +00:00
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
2018-11-16 22:34:32 +00:00
|
|
|
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;
|
2016-06-02 13:19:31 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The template to use when parsing the debug log
|
|
|
|
*
|
|
|
|
* @var string Template name
|
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
private static $logger_template = 'logger_cli';
|
2016-06-02 13:19:31 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* whether to output the log after FuzeWorks has run, regardless of conditions.
|
|
|
|
*
|
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
public static $debug = false;
|
|
|
|
|
2016-05-12 10:17:43 +00:00
|
|
|
/**
|
|
|
|
* List of all benchmark markpoints.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static $markPoints = [];
|
2016-06-08 12:57:41 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Initiates the Logger.
|
|
|
|
*
|
|
|
|
* Registers the error and exception handler, when required to do so by configuration
|
2019-01-21 19:44:53 +00:00
|
|
|
* @throws ConfigException
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2017-12-21 21:18:23 +00:00
|
|
|
public function __construct()
|
|
|
|
{
|
2018-10-07 21:02:18 +00:00
|
|
|
// Get the config file
|
|
|
|
$cfg_error = Factory::getInstance()->config->getConfig('error');
|
|
|
|
|
2016-07-23 14:32:11 +00:00
|
|
|
// Register the error handler, Untestable
|
|
|
|
// @codeCoverageIgnoreStart
|
2019-01-21 19:34:45 +00:00
|
|
|
if ($cfg_error->get('fuzeworks_error_reporting') == true)
|
2018-10-07 21:02:18 +00:00
|
|
|
{
|
2019-01-16 22:07:09 +00:00
|
|
|
self::enableHandlers();
|
2018-10-07 21:02:18 +00:00
|
|
|
}
|
2016-07-23 14:32:11 +00:00
|
|
|
// @codeCoverageIgnoreEnd
|
2016-07-23 00:53:01 +00:00
|
|
|
|
2018-10-07 21:02:18 +00:00
|
|
|
// Set PHP error reporting
|
2019-01-21 19:44:53 +00:00
|
|
|
if ($cfg_error->get('php_error_reporting'))
|
2018-10-07 21:02:18 +00:00
|
|
|
error_reporting(true);
|
2019-01-16 22:07:09 +00:00
|
|
|
else
|
|
|
|
error_reporting(false);
|
2016-07-23 00:53:01 +00:00
|
|
|
|
2018-10-07 21:02:18 +00:00
|
|
|
// Set the environment variables
|
2019-01-21 19:44:53 +00:00
|
|
|
self::$log_last_request = $cfg_error->get('log_last_request_to_file');
|
|
|
|
self::$log_errors_to_file = $cfg_error->get('log_errors_to_file');
|
2016-05-07 17:22:09 +00:00
|
|
|
self::newLevel('Logger Initiated');
|
2019-01-16 22:07:09 +00:00
|
|
|
}
|
2016-07-22 12:14:22 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
/**
|
|
|
|
* Enable error to screen logging.
|
|
|
|
*/
|
|
|
|
public static function enable()
|
|
|
|
{
|
2019-03-04 20:18:08 +00:00
|
|
|
self::$isEnabled = true;
|
2019-01-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable error to screen logging.
|
|
|
|
*/
|
|
|
|
public static function disable()
|
|
|
|
{
|
2019-03-04 20:18:08 +00:00
|
|
|
self::$isEnabled = false;
|
2019-01-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns whether screen logging is enabled.
|
|
|
|
*/
|
|
|
|
public static function isEnabled(): bool
|
|
|
|
{
|
2019-03-04 20:18:08 +00:00
|
|
|
return self::$isEnabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable outputting the debugger after the request has been processed
|
|
|
|
*/
|
|
|
|
public static function enableScreenLog()
|
|
|
|
{
|
|
|
|
if (!Core::isProduction())
|
|
|
|
self::$print_to_screen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable outputting the debugger after the request has been processed
|
|
|
|
*/
|
|
|
|
public static function disableScreenLog()
|
|
|
|
{
|
|
|
|
self::$print_to_screen = false;
|
2019-01-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Enable FuzeWorks error handling
|
|
|
|
*
|
|
|
|
* Registers errorHandler() and exceptionHandler() as the respective handlers for PHP
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public static function enableHandlers()
|
|
|
|
{
|
2019-03-04 20:18:08 +00:00
|
|
|
Core::addErrorHandler(['\FuzeWorks\Logger', 'errorHandler'], Priority::NORMAL);
|
|
|
|
Core::addExceptionHandler(['\FuzeWorks\Logger', 'exceptionHandler'], Priority::NORMAL);
|
2019-01-16 22:07:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Disable FuzeWorks error handling
|
|
|
|
*
|
|
|
|
* Unregisters errorHandler() and exceptionHandler() as the respective handlers for PHP
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public static function disableHandlers()
|
|
|
|
{
|
2019-03-04 20:18:08 +00:00
|
|
|
Core::removeErrorHandler(['\FuzeWorks\Logger', 'errorHandler'], Priority::NORMAL);
|
|
|
|
Core::removeExceptionHandler(['\FuzeWorks\Logger', 'exceptionHandler'], Priority::NORMAL);
|
2016-06-08 12:57:41 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Function to be run upon FuzeWorks shutdown.
|
|
|
|
*
|
2016-07-23 14:32:11 +00:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-06-08 12:57:41 +00:00
|
|
|
* Logs data to screen when requested to do so
|
2019-01-21 19:44:53 +00:00
|
|
|
* @throws EventException
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function shutdown()
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-06-08 12:57:41 +00:00
|
|
|
// And finally stop the Logging
|
|
|
|
self::stopLevel();
|
|
|
|
|
|
|
|
if (self::$debug === true || self::$print_to_screen) {
|
|
|
|
self::log('Parsing debug log');
|
|
|
|
self::logToScreen();
|
|
|
|
}
|
|
|
|
|
2018-11-16 22:34:32 +00:00
|
|
|
if (self::$log_last_request == true)
|
|
|
|
self::logLastRequest();
|
|
|
|
|
|
|
|
if (self::$log_errors_to_file == true)
|
|
|
|
self::logErrorsToFile();
|
2016-06-08 12:57:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to be run upon FuzeWorks shutdown.
|
|
|
|
*
|
2016-07-23 14:32:11 +00:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-06-08 12:57:41 +00:00
|
|
|
* Logs a fatal error and outputs the log when configured or requested to do so
|
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function shutdownError()
|
2016-06-08 12:57:41 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$error = error_get_last();
|
|
|
|
if ($error !== null) {
|
2019-01-16 22:07:09 +00:00
|
|
|
// Log it!
|
|
|
|
$thisType = self::getType($error['type']);
|
2019-01-17 13:14:31 +00:00
|
|
|
$LOG = array('type' => (!is_null($thisType) ? $thisType : 'ERROR'),
|
2019-01-16 22:07:09 +00:00
|
|
|
'message' => $error['message'],
|
|
|
|
'logFile' => $error['file'],
|
|
|
|
'logLine' => $error['line'],
|
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
|
|
|
self::$logs[] = $LOG;
|
2017-04-05 20:37:54 +00:00
|
|
|
|
|
|
|
if ($thisType == 'ERROR')
|
|
|
|
{
|
2019-01-16 22:07:09 +00:00
|
|
|
self::haltExecution($LOG);
|
2017-04-05 20:37:54 +00:00
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* System that redirects the errors to the appropriate logging method.
|
|
|
|
*
|
|
|
|
* @param int $type Error-type, Pre defined PHP Constant
|
|
|
|
* @param string error. The error itself
|
|
|
|
* @param string File. The absolute path of the file
|
|
|
|
* @param int Line. The line on which the error occured.
|
|
|
|
* @param array context. Some of the error's relevant variables
|
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function errorHandler($type = E_USER_NOTICE, $error = 'Undefined Error', $errFile = null, $errLine = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
// Check type
|
|
|
|
$thisType = self::getType($type);
|
|
|
|
$LOG = array('type' => (!is_null($thisType) ? $thisType : 'ERROR'),
|
|
|
|
'message' => (!is_null($error) ? $error : ''),
|
|
|
|
'logFile' => (!is_null($errFile) ? $errFile : ''),
|
|
|
|
'logLine' => (!is_null($errLine) ? $errLine : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Exception handler
|
|
|
|
* Will be triggered when an uncaught exception occures. This function shows the error-message, and shuts down the script.
|
|
|
|
* Please note that most of the user-defined exceptions will be caught in the router, and handled with the error-controller.
|
|
|
|
*
|
|
|
|
* @param Exception $exception The occured exception.
|
2019-02-09 19:25:49 +00:00
|
|
|
* @param bool $haltExecution. Defaults to true
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-02-09 19:25:49 +00:00
|
|
|
public static function exceptionHandler($exception, bool $haltExecution = true)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2019-01-16 22:07:09 +00:00
|
|
|
$LOG = array('type' => 'EXCEPTION',
|
|
|
|
'message' => $exception->getMessage(),
|
|
|
|
'logFile' => $exception->getFile(),
|
|
|
|
'logLine' => $exception->getLine(),
|
|
|
|
'context' => $exception->getTraceAsString(),
|
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
|
|
|
self::$logs[] = $LOG;
|
|
|
|
|
2017-07-14 14:11:35 +00:00
|
|
|
// And return a 500 because this error was fatal
|
2019-02-09 19:25:49 +00:00
|
|
|
if ($haltExecution)
|
|
|
|
self::haltExecution($LOG);
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2016-06-02 13:19:31 +00:00
|
|
|
/**
|
|
|
|
* Set the template that FuzeWorks should use to parse debug logs
|
|
|
|
*
|
2016-07-23 14:32:11 +00:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-06-02 13:19:31 +00:00
|
|
|
* @var string Name of the template file
|
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function setLoggerTemplate($templateName)
|
2016-06-02 13:19:31 +00:00
|
|
|
{
|
|
|
|
self::$logger_template = $templateName;
|
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Output the entire log to the screen. Used for debugging problems with your code.
|
2016-07-23 14:32:11 +00:00
|
|
|
* @codeCoverageIgnore
|
2019-01-21 19:44:53 +00:00
|
|
|
* @throws EventException
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2017-12-21 21:18:23 +00:00
|
|
|
public static function logToScreen()
|
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
// Send a screenLogEvent, allows for new screen log designs
|
|
|
|
$event = Events::fireEvent('screenLogEvent');
|
|
|
|
if ($event->isCancelled()) {
|
2019-01-16 22:07:09 +00:00
|
|
|
return;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
$logs = self::$logs;
|
2017-12-21 21:18:23 +00:00
|
|
|
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.' . self::$logger_template . '.php');
|
2016-06-02 13:19:31 +00:00
|
|
|
}
|
2015-10-11 18:14:49 +00:00
|
|
|
|
2016-07-23 14:32:11 +00:00
|
|
|
/**
|
|
|
|
* Output the entire log to a file. Used for debugging problems with your code.
|
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
2018-11-16 22:34:32 +00:00
|
|
|
public static function logLastRequest()
|
2016-06-02 13:19:31 +00:00
|
|
|
{
|
2016-07-23 14:32:11 +00:00
|
|
|
ob_start(function () {});
|
2019-01-16 22:07:09 +00:00
|
|
|
$logs = self::$logs;
|
2018-11-16 22:34:32 +00:00
|
|
|
require(dirname(__DIR__) . DS . 'Layout' . DS . 'layout.logger_file.php');
|
2016-07-23 14:32:11 +00:00
|
|
|
$contents = ob_get_clean();
|
2018-11-16 22:34:32 +00:00
|
|
|
$file = Core::$logDir . DS . 'fwlog_request.log';
|
2019-07-22 17:48:13 +00:00
|
|
|
if (is_writable(dirname($file)))
|
2018-11-16 22:34:32 +00:00
|
|
|
file_put_contents($file, $contents);
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2018-11-16 22:34:32 +00:00
|
|
|
/**
|
2019-01-16 22:07:09 +00:00
|
|
|
* Output all errors to a file. Used for tracking all errors in FuzeWorks and associated code
|
2018-11-16 22:34:32 +00:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*/
|
|
|
|
public static function logErrorsToFile()
|
|
|
|
{
|
|
|
|
ob_start(function() {});
|
|
|
|
$logs = [];
|
2019-01-16 22:07:09 +00:00
|
|
|
foreach (self::$logs as $log)
|
2018-11-16 22:34:32 +00:00
|
|
|
{
|
2019-07-22 17:48:13 +00:00
|
|
|
if ($log['type'] === 'ERROR' || $log['type'] === 'EXCEPTION')
|
2018-11-16 22:34:32 +00:00
|
|
|
$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)))
|
2019-07-22 17:48:13 +00:00
|
|
|
file_put_contents($file, $contents, FILE_APPEND);
|
2018-11-16 22:34:32 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:17:43 +00:00
|
|
|
/* =========================================LOGGING METHODS============================================================== */
|
|
|
|
|
|
|
|
/**
|
2019-01-16 22:07:09 +00:00
|
|
|
* Set a benchmark mark point.
|
2016-05-12 10:17:43 +00:00
|
|
|
*
|
|
|
|
* Multiple calls to this function can be made so that several
|
|
|
|
* execution points can be timed.
|
|
|
|
*
|
|
|
|
* @param string $name Marker name
|
|
|
|
* @return void
|
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function mark($name)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-07-23 14:32:11 +00:00
|
|
|
$LOG = array('type' => 'BMARK',
|
|
|
|
'message' => (!is_null($name) ? $name : ''),
|
|
|
|
'logFile' => '',
|
|
|
|
'logLine' => '',
|
|
|
|
'context' => '',
|
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-12 10:17:43 +00:00
|
|
|
}
|
2016-05-07 17:22:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a information log entry.
|
|
|
|
*
|
|
|
|
* @param string $msg The information to be logged
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function log($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
self::logInfo($msg, $mod, $file, $line);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a information log entry.
|
|
|
|
*
|
|
|
|
* @param string $msg The information to be logged
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function logInfo($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$LOG = array('type' => 'INFO',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:17:43 +00:00
|
|
|
/**
|
|
|
|
* Create a information log entry.
|
|
|
|
*
|
|
|
|
* @param string $msg The information to be logged
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-12 10:17:43 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function logDebug($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-12 10:17:43 +00:00
|
|
|
$LOG = array('type' => 'DEBUG',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-12 10:17:43 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Create a error log entry.
|
|
|
|
*
|
|
|
|
* @param string $msg The information to be logged
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function logError($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$LOG = array('type' => 'ERROR',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a warning log entry.
|
|
|
|
*
|
|
|
|
* @param string $msg The information to be logged
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function logWarning($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$LOG = array('type' => 'WARNING',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
2015-08-29 15:43:13 +00:00
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Create a new Level log entry. Used to categorise logs.
|
|
|
|
*
|
|
|
|
* @param string $msg The name of the new level
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function newLevel($msg, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$LOG = array('type' => 'LEVEL_START',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a stop Level log entry. Used to close log categories.
|
|
|
|
*
|
|
|
|
* @param string $msg The name of the new level
|
|
|
|
* @param string $mod The name of the module
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param string $file The file where the log occurred
|
|
|
|
* @param int $line The line where the log occurred
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2017-12-21 23:06:01 +00:00
|
|
|
public static function stopLevel($msg = null, $mod = null, $file = null, $line = null)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$LOG = array('type' => 'LEVEL_STOP',
|
|
|
|
'message' => (!is_null($msg) ? $msg : ''),
|
|
|
|
'logFile' => (!is_null($file) ? $file : ''),
|
|
|
|
'logLine' => (!is_null($line) ? $line : ''),
|
|
|
|
'context' => (!is_null($mod) ? $mod : ''),
|
2016-05-12 10:17:43 +00:00
|
|
|
'runtime' => round(self::getRelativeTime(), 4),);
|
2016-05-07 17:22:09 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
self::$logs[] = $LOG;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
2016-05-12 10:17:43 +00:00
|
|
|
/* =========================================OTHER METHODS============================================================== */
|
2016-05-07 17:22:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a string representation of an error
|
|
|
|
* Turns a PHP error-constant (or integer) into a string representation.
|
|
|
|
*
|
2019-01-16 22:07:09 +00:00
|
|
|
* @param int $type PHP-constant errorType (e.g. E_NOTICE).
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
|
|
|
* @return string String representation
|
|
|
|
*/
|
2017-12-21 21:18:23 +00:00
|
|
|
public static function getType($type): string
|
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
switch ($type) {
|
|
|
|
case E_PARSE:
|
|
|
|
case E_CORE_ERROR:
|
|
|
|
case E_COMPILE_ERROR:
|
|
|
|
case E_USER_ERROR:
|
2020-02-20 11:43:40 +00:00
|
|
|
case E_STRICT:
|
|
|
|
case E_RECOVERABLE_ERROR:
|
|
|
|
case E_ERROR:
|
2016-05-07 17:22:09 +00:00
|
|
|
return 'ERROR';
|
2020-02-20 11:43:40 +00:00
|
|
|
case E_NOTICE:
|
|
|
|
case E_CORE_WARNING:
|
|
|
|
case E_COMPILE_WARNING:
|
2016-05-07 17:22:09 +00:00
|
|
|
case E_USER_WARNING:
|
|
|
|
case E_USER_NOTICE:
|
|
|
|
case E_USER_DEPRECATED:
|
|
|
|
case E_DEPRECATED:
|
2020-02-20 11:43:40 +00:00
|
|
|
case E_WARNING:
|
2016-05-07 17:22:09 +00:00
|
|
|
return 'WARNING';
|
|
|
|
}
|
|
|
|
|
2016-05-12 10:17:43 +00:00
|
|
|
return $type = 'Unknown error: ' . $type;
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-01-16 22:07:09 +00:00
|
|
|
* Halts the Execution of FuzeWorks
|
2016-05-07 17:22:09 +00:00
|
|
|
*
|
2019-01-16 22:07:09 +00:00
|
|
|
* Will die a message if not intercepted by haltExecutionEvent.
|
|
|
|
* @param array $log
|
|
|
|
* @codeCoverageIgnore
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-01-16 22:07:09 +00:00
|
|
|
public static function haltExecution(array $log)
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2019-01-16 22:07:09 +00:00
|
|
|
self::logError("Halting execution...");
|
|
|
|
try {
|
|
|
|
$event = Events::fireEvent("haltExecutionEvent", $log);
|
|
|
|
} catch (EventException $e) {
|
|
|
|
self::logError("Can't fire haltExecutionEvent: '".$e->getMessage()."'");
|
|
|
|
die(PHP_EOL . "FuzeWorks execution halted. See error log for more information");
|
2016-05-07 17:22:09 +00:00
|
|
|
}
|
2019-01-16 22:07:09 +00:00
|
|
|
if ($event->isCancelled() == true)
|
|
|
|
return;
|
2015-05-03 20:50:36 +00:00
|
|
|
|
2019-01-16 22:07:09 +00:00
|
|
|
die(PHP_EOL . "FuzeWorks execution halted. See error log for more information");
|
2016-07-23 14:32:11 +00:00
|
|
|
}
|
|
|
|
|
2016-05-07 17:22:09 +00:00
|
|
|
/**
|
|
|
|
* Get the relative time since the framework started.
|
|
|
|
*
|
|
|
|
* Used for debugging timings in FuzeWorks
|
|
|
|
*
|
2019-02-09 19:25:49 +00:00
|
|
|
* @return float Time passed since FuzeWorks init
|
2016-05-07 17:22:09 +00:00
|
|
|
*/
|
2019-02-09 19:25:49 +00:00
|
|
|
private static function getRelativeTime(): float
|
2017-12-21 21:18:23 +00:00
|
|
|
{
|
2016-05-07 17:22:09 +00:00
|
|
|
$startTime = STARTTIME;
|
|
|
|
$time = microtime(true) - $startTime;
|
|
|
|
|
|
|
|
return $time;
|
|
|
|
}
|
2016-07-23 14:32:11 +00:00
|
|
|
}
|