Merge branch 'logging'

This commit is contained in:
Andreas Österreicher
2020-09-15 13:43:57 +02:00
8 changed files with 243 additions and 71 deletions
+5 -3
View File
@@ -3,7 +3,8 @@
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
* This is the super class for all those controllers that can only be called from command line
* It provides also an helper to display the possible calls
*/
abstract class CLI_Controller extends FHC_Controller
{
@@ -15,9 +16,9 @@ abstract class CLI_Controller extends FHC_Controller
/**
* Constructor
*/
public function __construct()
public function __construct()
{
parent::__construct();
parent::__construct();
// Checks if the controller is called from command line
$this->_isAllowed();
@@ -103,3 +104,4 @@ abstract class CLI_Controller extends FHC_Controller
}
}
}
+28 -19
View File
@@ -3,26 +3,34 @@
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
* This is the super class for a job.
* All the controllers that extends this class can only be called from command line.
* Provides utility methods to log into database
*/
abstract class JOB_Controller extends CLI_Controller
{
/**
* Constructor
*/
public function __construct()
public function __construct()
{
parent::__construct();
parent::__construct();
// Loads LogLib with different debug trace levels to get data of the job that extends this class
// It also specify parameters to set database fields
$this->load->library('LogLib', array(
'classIndex' => 5,
'functionIndex' => 5,
'lineIndex' => 4,
'dbLogType' => 'job', // required
'dbExecuteUser' => 'Cronjob system'
));
$this->load->library('LogLib',
array(
'classIndex' => 5,
'functionIndex' => 5,
'lineIndex' => 4,
'dbLogType' => 'job', // required
'dbExecuteUser' => 'Cronjob system',
'requestId' => 'JOB',
'requestDataFormatter' => function($data) {
return json_encode($data);
}
)
);
}
//------------------------------------------------------------------------------------------------------------------
@@ -33,7 +41,7 @@ abstract class JOB_Controller extends CLI_Controller
*/
protected function logInfo($response, $parameters = null)
{
$this->_log(LogLib::INFO, 'Cronjob info', $response, $parameters);
$this->_log(LogLib::INFO, $response, $parameters);
}
/**
@@ -41,7 +49,7 @@ abstract class JOB_Controller extends CLI_Controller
*/
protected function logDebug($response, $parameters = null)
{
$this->_log(LogLib::DEBUG, 'Cronjob debug', $response, $parameters);
$this->_log(LogLib::DEBUG, $response, $parameters);
}
/**
@@ -49,7 +57,7 @@ abstract class JOB_Controller extends CLI_Controller
*/
protected function logWarning($response, $parameters = null)
{
$this->_log(LogLib::WARNING, 'Cronjob warning', $response, $parameters);
$this->_log(LogLib::WARNING, $response, $parameters);
}
/**
@@ -57,7 +65,7 @@ abstract class JOB_Controller extends CLI_Controller
*/
protected function logError($response, $parameters = null)
{
$this->_log(LogLib::ERROR, 'Cronjob error', $response, $parameters);
$this->_log(LogLib::ERROR, $response, $parameters);
}
//------------------------------------------------------------------------------------------------------------------
@@ -66,7 +74,7 @@ abstract class JOB_Controller extends CLI_Controller
/**
* Writes a log to database
*/
private function _log($level, $requestId, $response, $parameters)
private function _log($level, $response, $parameters)
{
$data = new stdClass();
@@ -76,17 +84,18 @@ abstract class JOB_Controller extends CLI_Controller
switch($level)
{
case LogLib::INFO:
$this->loglib->logInfoDB($requestId, json_encode(success($data, LogLib::INFO)));
$this->loglib->logInfoDB($data);
break;
case LogLib::DEBUG:
$this->loglib->logDebugDB($requestId, json_encode(success($data, LogLib::DEBUG)));
$this->loglib->logDebugDB($data);
break;
case LogLib::WARNING:
$this->loglib->logWarningDB($requestId, json_encode(error($data, LogLib::WARNING)));
$this->loglib->logWarningDB($data);
break;
case LogLib::ERROR:
$this->loglib->logErrorDB($requestId, json_encode(error($data, LogLib::ERROR)));
$this->loglib->logErrorDB($data);
break;
}
}
}
+11 -11
View File
@@ -5,9 +5,10 @@ if (!defined("BASEPATH")) exit("No direct script access allowed");
/**
* Job Queue Worker
*
* This controller acts as interface of the JobsQueueLib that contains all the needed functionalities to operate with
* the Jobs Queue System
* This is an abstract class that provide basic functionalities, it has to be extended to broaden its logic
* This controller acts as interface of the JobsQueueLib that contains
* all the needed functionalities to operate with the Jobs Queue System
* This is an abstract class that provide basic functionalities,
* it has to be extended to broaden its logic
*/
abstract class JQW_Controller extends JOB_Controller
{
@@ -18,14 +19,13 @@ abstract class JQW_Controller extends JOB_Controller
{
parent::__construct();
// Loads LogLib with different parameters
$this->load->library('LogLib', array(
'classIndex' => 5,
'functionIndex' => 5,
'lineIndex' => 4,
'dbLogType' => 'job', // required
'dbExecuteUser' => 'Jobs queue system'
));
// Changes the needed configs for LogLib
$this->loglib->setConfigs(
array(
'dbExecuteUser' => 'Jobs queue system',
'requestId' => 'JQW'
)
);
// Loads JobsQueueLib library
$this->load->library('JobsQueueLib');
+5 -2
View File
@@ -93,6 +93,7 @@ class FilterWidgetLib
const OP_NOT_SET = 'nset';
// Filter options values
const OPT_MINUTES = 'minutes';
const OPT_HOURS = 'hours';
const OPT_DAYS = 'days';
const OPT_MONTHS = 'months';
@@ -887,7 +888,8 @@ class FilterWidgetLib
&& isset($filterDefinition->option)
&& ($filterDefinition->option == self::OPT_HOURS
|| $filterDefinition->option == self::OPT_DAYS
|| $filterDefinition->option == self::OPT_MONTHS))
|| $filterDefinition->option == self::OPT_MONTHS
|| $filterDefinition->option == self::OPT_MINUTES))
{
$condition = '< (NOW() - \''.$filterDefinition->condition.' '.$filterDefinition->option.'\'::interval)';
}
@@ -903,7 +905,8 @@ class FilterWidgetLib
&& isset($filterDefinition->option)
&& ($filterDefinition->option == self::OPT_HOURS
|| $filterDefinition->option == self::OPT_DAYS
|| $filterDefinition->option == self::OPT_MONTHS))
|| $filterDefinition->option == self::OPT_MONTHS
|| $filterDefinition->option == self::OPT_MINUTES))
{
$condition = '> (NOW() - \''.$filterDefinition->condition.' '.$filterDefinition->option.'\'::interval)';
}
+45 -12
View File
@@ -42,6 +42,8 @@ class LogLib
const P_NAME_LINE_INDEX = 'lineIndex';
const P_NAME_DB_LOG_TYPE = 'dbLogType';
const P_NAME_DB_EXECUTE_USER = 'dbExecuteUser';
const P_NAME_REQUEST_ID = 'requestId';
const P_NAME_REQUEST_DATA_FORMATTER = 'requestDataFormatter';
// Properties used to retrieve caller data
private $_classIndex;
@@ -52,6 +54,9 @@ class LogLib
private $_dbLogType;
private $_dbExecuteUser;
private $_requestId; // is it possible to specify a request id when loading this library
private $_requestDataFormatter; // is possible to provide a function to format request data
/**
* Set properties to a default value or overwrites them with the given parameters
*/
@@ -63,7 +68,18 @@ class LogLib
$this->_lineIndex = self::LINE_INDEX;
$this->_dbLogType = null;
$this->_dbExecuteUser = self::DB_EXECUTE_USER;
$this->_requestId = null;
$this->_requestDataFormatter = null;
// If parameters are given then overwrite the default values
if (!isEmptyArray($params)) $this->setConfigs($params);
}
/**
* Store configuration parameters for this lib
*/
public function setConfigs($params)
{
// If parameters are given then overwrite the default values
if (!isEmptyArray($params))
{
@@ -72,6 +88,8 @@ class LogLib
if (isset($params[self::P_NAME_LINE_INDEX])) $this->_lineIndex = $params[self::P_NAME_LINE_INDEX];
if (isset($params[self::P_NAME_DB_LOG_TYPE])) $this->_dbLogType = $params[self::P_NAME_DB_LOG_TYPE];
if (isset($params[self::P_NAME_DB_EXECUTE_USER])) $this->_dbExecuteUser = $params[self::P_NAME_DB_EXECUTE_USER];
if (isset($params[self::P_NAME_REQUEST_ID])) $this->_requestId = $params[self::P_NAME_REQUEST_ID];
if (isset($params[self::P_NAME_REQUEST_DATA_FORMATTER])) $this->_requestDataFormatter = $params[self::P_NAME_REQUEST_DATA_FORMATTER];
}
}
@@ -108,33 +126,33 @@ class LogLib
/**
* Writes an info log to database
*/
public function logInfoDB($requestId, $data)
public function logInfoDB($data, $requestId = null)
{
$this->_logDB(self::INFO, $requestId, $data);
$this->_logDB(self::INFO, $data, $requestId);
}
/**
* Writes a debug log to database
*/
public function logDebugDB($requestId, $data)
public function logDebugDB($data, $requestId = null)
{
$this->_logDB(self::DEBUG, $requestId, $data);
$this->_logDB(self::DEBUG, $data, $requestId);
}
/**
* Writes an warning log to database
*/
public function logWarningDB($requestId, $data)
public function logWarningDB($data, $requestId = null)
{
$this->_logDB(self::WARNING, $requestId, $data);
$this->_logDB(self::WARNING, $data, $requestId);
}
/**
* Writes an error log to database
*/
public function logErrorDB($requestId, $data)
public function logErrorDB($data, $requestId = null)
{
$this->_logDB(self::ERROR, $requestId, $data);
$this->_logDB(self::ERROR, $data, $requestId);
}
// --------------------------------------------------------------------------------------------------------------
@@ -151,8 +169,15 @@ class LogLib
/**
* Writes logs to database
*/
private function _logDB($level, $requestId, $data)
private function _logDB($level, $data, $requestId, $executionTime = null)
{
// If there isn't a valid request id provided during the loading of this library or when any log to db method is called
// NOTE: this message will be displayed only to the developer AND stops the execution
if (isEmptyString($this->_requestId) && isEmptyString($requestId))
{
show_error('To log to database you need to specify or give the "'.self::P_NAME_REQUEST_ID.'" parameter when the LogLib is loaded or when log'.ucfirst($level).'DB is called');
}
// If the _dbLogType parameter was not given when this library was loaded
// NOTE: this message will be displayed only to the developer AND stops the execution
if ($this->_dbLogType == null)
@@ -175,14 +200,21 @@ class LogLib
// Get caller data
$callerData = $this->_getCaller();
// If a request data formatter was defined then use it
$request_data = $data;
if ($this->_requestDataFormatter != null && is_callable($this->_requestDataFormatter))
{
$request_data = call_user_func_array($this->_requestDataFormatter, array($data));
}
// Writes a log to database
$ci->WebservicelogModel->insert(array(
'webservicetyp_kurzbz' => $this->_dbLogType,
'request_id' => $requestId,
'request_id' => (!isEmptyString($requestId) ? $requestId : $this->_requestId).' - '.$level,
'beschreibung' => $this->_getDatabaseDescription($callerData),
'request_data' => $data,
'request_data' => $request_data,
'execute_user' => $this->_dbExecuteUser,
'execute_time' => 'NOW()' // current time
'execute_time' => $executionTime == null ? 'NOW()' : $executionTime // default current time, if not otherwise specified
));
}
}
@@ -251,3 +283,4 @@ class LogLib
return $formatted;
}
}
@@ -25,7 +25,7 @@
),
'formatRow' => function($datasetRaw) {
$datasetRaw->ExecutionTime = date_format(date_create($datasetRaw->ExecutionTime), 'd.m.Y H:i:s');
$datasetRaw->ExecutionTime = date_format(date_create($datasetRaw->ExecutionTime), 'd.m.Y H:i:s:u');
return $datasetRaw;
},
@@ -33,22 +33,22 @@
$mark = '';
if ($datasetRaw->RequestId == 'Cronjob error')
if (strpos($datasetRaw->RequestId, 'error') != false)
{
$mark = 'text-red';
}
if ($datasetRaw->RequestId == 'Cronjob info')
if (strpos($datasetRaw->RequestId, 'info') != false)
{
$mark = 'text-green';
}
if ($datasetRaw->RequestId == 'Cronjob warning')
if (strpos($datasetRaw->RequestId, 'warning') != false)
{
$mark = 'text-orange';
}
if ($datasetRaw->RequestId == 'Cronjob debug')
if (strpos($datasetRaw->RequestId, 'debug') != false)
{
$mark = 'text-info';
}
@@ -63,3 +63,4 @@
echo $this->widgetlib->widget('FilterWidget', $filterWidgetArray);
?>
+2
View File
@@ -824,6 +824,8 @@ var FHC_FilterWidget = {
html += "</span>";
html += "<span>";
html += " <select class='" + classOption + "' " + disabled + ">";
html += " <option value='minutes' " + (appliedFilter.option == "minutes" ? "selected" : "") + ">Minutes</option>";
html += " <option value='hours' " + (appliedFilter.option == "hours" ? "selected" : "") + ">Hours</option>";
html += " <option value='days' " + (appliedFilter.option == "days" ? "selected" : "") + ">Days</option>";
html += " <option value='months' " + (appliedFilter.option == "months" ? "selected" : "") + ">Months</option>";
html += " </select>";
+141 -19
View File
@@ -438,13 +438,13 @@ $filters = array(
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'last7days',
'description' => '{Last 7 days logs}',
'filter_kurzbz' => 'last1min',
'description' => '{Last minute logs}',
'sort' => 1,
'default_filter' => true,
'filter' => '
{
"name": "All logs from the last 7 days",
"name": "All logs from the last minute",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
@@ -456,8 +456,8 @@ $filters = array(
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "7",
"option": "days"
"condition": "1",
"option": "minutes"
}
]
}
@@ -467,13 +467,13 @@ $filters = array(
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'jobs14days',
'description' => '{Last 14 days jobs logs}',
'filter_kurzbz' => 'jobs24hours',
'description' => '{Last 24 hours jobs logs}',
'sort' => 2,
'default_filter' => false,
'filter' => '
{
"name": "All jobs logs from the last 14 days",
"name": "All jobs logs from the last 24 hours",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
@@ -487,11 +487,133 @@ $filters = array(
"operation": "contains",
"condition": "job"
},
{
"name": "RequestId",
"operation": "contains",
"condition": "JOB"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "14",
"option": "days"
"condition": "24",
"option": "hours"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'jobs48hours',
'description' => '{Last 48 hours jobs logs}',
'sort' => 2,
'default_filter' => false,
'filter' => '
{
"name": "All jobs logs from the last 48 hours",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "job"
},
{
"name": "RequestId",
"operation": "contains",
"condition": "JOB"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "48",
"option": "hours"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'jqws24hours',
'description' => '{Last 24 hours JQWs logs}',
'sort' => 2,
'default_filter' => false,
'filter' => '
{
"name": "All Job Queue Workers logs from the last 24 hours",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "job"
},
{
"name": "RequestId",
"operation": "contains",
"condition": "JQW"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "24",
"option": "hours"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'jqws48hours',
'description' => '{Last 48 hours JQWs logs}',
'sort' => 2,
'default_filter' => false,
'filter' => '
{
"name": "All Job Queue Workers logs from the last 48 hours",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "job"
},
{
"name": "RequestId",
"operation": "contains",
"condition": "JQW"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "48",
"option": "hours"
}
]
}
@@ -535,13 +657,13 @@ $filters = array(
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'content3days',
'description' => '{Last 3 days content logs}',
'filter_kurzbz' => 'content3minutes',
'description' => '{Last 3 minutes content logs}',
'sort' => 4,
'default_filter' => false,
'filter' => '
{
"name": "All content logs from the last 3 days",
"name": "All content logs from the last 3 minutes",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
@@ -559,7 +681,7 @@ $filters = array(
"name": "ExecutionTime",
"operation": "lt",
"condition": "3",
"option": "days"
"option": "minutes"
}
]
}
@@ -569,13 +691,13 @@ $filters = array(
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'wienerlinien7days',
'description' => '{Last 7 days wiener linien logs}',
'filter_kurzbz' => 'wienerlinien24hours',
'description' => '{Last 24 hours Wiener Linien logs}',
'sort' => 5,
'default_filter' => false,
'filter' => '
{
"name": "All wiener linien logs from the last 7 days",
"name": "All Wiener Linien logs from the last 24 hours",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
@@ -592,8 +714,8 @@ $filters = array(
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "7",
"option": "days"
"condition": "24",
"option": "hours"
}
]
}