- Added new constants P_NAME_REQUEST_ID and

P_NAME_REQUEST_DATA_FORMATTER tp logLib
- Added new properties _requestId and _requestDataFormatter to LogLib
- Added new public method setConfigs to LogLib
- Added possibility to choose the request id when the LogLib is loaded
- Added possibility to format the request data using the
_requestDataFormatter parameter
- Now LogLib always stores the request id + log level
- Added possibility to change the execution time parameter
- Adapted application/core/JOB_Controller.php code
- Adapted application/core/JQW_Controller.php code
This commit is contained in:
Paolo
2020-09-11 17:43:39 +02:00
parent 01a8ec69de
commit fbd4ded720
4 changed files with 89 additions and 45 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');
+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;
}
}