Merge branch 'feature-4190/Cronjobs_log_in_database'

This commit is contained in:
Andreas Österreicher
2019-10-02 14:29:53 +02:00
15 changed files with 655 additions and 39 deletions
@@ -0,0 +1,44 @@
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
/**
* Overview on cronjob logs
*/
class LogsViewer extends Auth_Controller
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct(
array(
'index' => 'admin:r'
)
);
// Loads WidgetLib
$this->load->library('WidgetLib');
// Loads phrases system
$this->loadPhrases(
array(
'global',
'ui',
'filter'
)
);
}
// -----------------------------------------------------------------------------------------------------------------
// Public methods
/**
* Main page of the InfoCenter tool
*/
public function index()
{
$this->load->view('system/logs/logsViewer.php');
}
}
+3 -1
View File
@@ -1,9 +1,11 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* REST_Controller takes care about authentication and it loads the AuthLib
*/
class APIv1_Controller extends REST_Controller
abstract class APIv1_Controller extends REST_Controller
{
private $_requiredPermissions;
+5 -2
View File
@@ -1,8 +1,11 @@
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Auth_Controller extends FHC_Controller
/**
*
*/
abstract class Auth_Controller extends FHC_Controller
{
/**
* Extends this controller if authentication is required
+4 -1
View File
@@ -2,7 +2,10 @@
if (!defined('BASEPATH')) exit('No direct script access allowed');
class CLI_Controller extends FHC_Controller
/**
*
*/
abstract class CLI_Controller extends FHC_Controller
{
const INFO_FORMAT = '%s %s %s %s'; // Info message format
const REQUIRED_PARAM_FORMAT = ' %s'; // Info message required method parameter format
+5
View File
@@ -1,5 +1,10 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
class DB_Model extends CI_Model
{
// Default schema used by the models
+5 -2
View File
@@ -1,8 +1,11 @@
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
if (!defined('BASEPATH')) exit('No direct script access allowed');
class FHC_Controller extends CI_Controller
/**
*
*/
abstract class FHC_Controller extends CI_Controller
{
const FHC_CONTROLLER_ID = 'fhc_controller_id'; // name of the parameter used to identify uniquely a call to a controller
+6 -1
View File
@@ -1,6 +1,11 @@
<?php
class FS_Model extends CI_Model
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
abstract class FS_Model extends CI_Model
{
protected $filepath; // Path of the file
+92
View File
@@ -0,0 +1,92 @@
<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
*
*/
abstract class JOB_Controller extends CLI_Controller
{
/**
* Constructor
*/
public function __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'
));
}
//------------------------------------------------------------------------------------------------------------------
// Protected methods
/**
* Writes a cronjob info log
*/
protected function logInfo($response, $parameters = null)
{
$this->_log(LogLib::INFO, 'Cronjob info', $response, $parameters);
}
/**
* Writes a cronjob debug log
*/
protected function logDebug($response, $parameters = null)
{
$this->_log(LogLib::DEBUG, 'Cronjob debug', $response, $parameters);
}
/**
* Writes a cronjob warning log
*/
protected function logWarning($response, $parameters = null)
{
$this->_log(LogLib::WARNING, 'Cronjob warning', $response, $parameters);
}
/**
* Writes a cronjob error log
*/
protected function logError($response, $parameters = null)
{
$this->_log(LogLib::ERROR, 'Cronjob error', $response, $parameters);
}
//------------------------------------------------------------------------------------------------------------------
// Private methods
/**
* Writes a log to database
*/
private function _log($level, $requestId, $response, $parameters)
{
$data = new stdClass();
$data->response = $response;
if ($parameters != null) $data->parameters = $parameters;
switch($level)
{
case LogLib::INFO:
$this->loglib->logInfoDB($requestId, json_encode(success($data, LogLib::INFO)));
break;
case LogLib::DEBUG:
$this->loglib->logDebugDB($requestId, json_encode(success($data, LogLib::DEBUG)));
break;
case LogLib::WARNING:
$this->loglib->logWarningDB($requestId, json_encode(error($data, LogLib::WARNING)));
break;
case LogLib::ERROR:
$this->loglib->logErrorDB($requestId, json_encode(error($data, LogLib::ERROR)));
break;
}
}
}
+180 -30
View File
@@ -1,102 +1,252 @@
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');
if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
* Library usefull for logging!
* This library can log using CodeIgniter log system (file system) or to database
*/
class LogLib
{
const DEBUG = 'debug';
const ERROR = 'error';
// Log levels
const INFO = 'info';
const DEBUG = 'debug';
const WARNING = 'warning';
const ERROR = 'error';
// Default debug trace levels
const CLASS_INDEX = 3;
const FUNCTION_INDEX = 3;
const LINE_INDEX = 2;
const DB_EXECUTE_USER = 'LogLib'; // Default execute user
// Caller data names
const CLASS_NAME = 'className';
const FUNCTION_NAME = 'functionName';
const CODE_LINE = 'codeLine';
// To format the log message prefix when logging to file system
const CALLER_PREFIX = '[';
const CALLER_POSTFIX = ']';
const CLASS_POSTFIX = '->';
const LINE_SEPARATOR = ':';
// --------------------------------------------------------------------------------------------------------------
// Public methods
// CodeIgniter configuration log entry name and log debug value
const CI_LOG_THRESHOLD_NAME = 'log_threshold';
const CI_LOG_THRESHOLD_DEBUG = 2;
// LogLib parameters names
const P_NAME_CLASS_INDEX = 'classIndex';
const P_NAME_FUNCTION_INDEX = 'functionIndex';
const P_NAME_LINE_INDEX = 'lineIndex';
const P_NAME_DB_LOG_TYPE = 'dbLogType';
const P_NAME_DB_EXECUTE_USER = 'dbExecuteUser';
// Properties used to retrieve caller data
private $_classIndex;
private $_functionIndex;
private $_lineIndex;
// Properties used when logging to database
private $_dbLogType;
private $_dbExecuteUser;
/**
* logDebug
* Set properties to a default value or overwrites them with the given parameters
*/
public function __construct($params = null)
{
// Properties default values
$this->_classIndex = self::CLASS_INDEX;
$this->_functionIndex = self::FUNCTION_INDEX;
$this->_lineIndex = self::LINE_INDEX;
$this->_dbLogType = null;
$this->_dbExecuteUser = self::DB_EXECUTE_USER;
// If parameters are given then overwrite the default values
if (!isEmptyArray($params))
{
if (isset($params[self::P_NAME_CLASS_INDEX])) $this->_classIndex = $params[self::P_NAME_CLASS_INDEX];
if (isset($params[self::P_NAME_FUNCTION_INDEX])) $this->_functionIndex = $params[self::P_NAME_FUNCTION_INDEX];
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];
}
}
// --------------------------------------------------------------------------------------------------------------
// Public methods based on CodeIgniter log system
/**
* Writes a debug log to CodeIgniter log
*/
public function logDebug($message)
{
$this->_log(LogLib::DEBUG, $message);
$this->_log(self::DEBUG, $message);
}
/**
* logInfo
* Writes an info log to CodeIgniter log
*/
public function logInfo($message)
{
$this->_log(LogLib::INFO, $message);
$this->_log(self::INFO, $message);
}
/**
* logError
* Writes an error log to CodeIgniter log
*/
public function logError($message)
{
$this->_log(LogLib::ERROR, $message);
$this->_log(self::ERROR, $message);
}
// --------------------------------------------------------------------------------------------------------------
// Public methods based on database
/**
* Writes an info log to database
*/
public function logInfoDB($requestId, $data)
{
$this->_logDB(self::INFO, $requestId, $data);
}
/**
* Writes a debug log to database
*/
public function logDebugDB($requestId, $data)
{
$this->_logDB(self::DEBUG, $requestId, $data);
}
/**
* Writes an warning log to database
*/
public function logWarningDB($requestId, $data)
{
$this->_logDB(self::WARNING, $requestId, $data);
}
/**
* Writes an error log to database
*/
public function logErrorDB($requestId, $data)
{
$this->_logDB(self::ERROR, $requestId, $data);
}
// --------------------------------------------------------------------------------------------------------------
// Private methods
/**
* log
* Writes using CodeIgniter log system (file system)
*/
private function _log($level, $message)
{
log_message($level, $this->_getCaller().$message);
log_message($level, $this->_getPrefix($this->_getCaller()).$message);
}
/**
* _getCaller
* Writes logs to database
*/
private function _logDB($level, $requestId, $data)
{
// 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)
{
show_error('To log to database you need to specify the "'.self::P_NAME_DB_LOG_TYPE.'" parameter when the LogLib is loaded');
}
$ci =& get_instance(); // get code igniter instance
// If only debug log is enabed then is possible to write a debug log, otherwise...
if ($level == self::DEBUG && $ci->config->item(self::CI_LOG_THRESHOLD_NAME) != self::CI_LOG_THRESHOLD_DEBUG)
{
// ...do nothing
}
else
{
// Loads WebservicelogModel
$ci->load->model('system/Webservicelog_model', 'WebservicelogModel');
// Get caller data
$callerData = $this->_getCaller();
// Writes a log to database
$ci->WebservicelogModel->insert(array(
'webservicetyp_kurzbz' => $this->_dbLogType,
'request_id' => $requestId,
'beschreibung' => $this->_getDatabaseDescription($callerData),
'request_data' => $data,
'execute_user' => $this->_dbExecuteUser,
'execute_time' => 'NOW()' // current time
));
}
}
/**
* Retrieves caller's data
*/
private function _getCaller()
{
$classIndex = 3;
$functionIndex = 3;
$lineIndex = 2;
$class = '';
$function = '';
$line = '';
$backtrace_arr = debug_backtrace();
if (isset($backtrace_arr[$classIndex]['class']) && $backtrace_arr[$classIndex]['class'] != '')
if (isset($backtrace_arr[$this->_classIndex]['class']) && $backtrace_arr[$this->_classIndex]['class'] != '')
{
$class = $backtrace_arr[$classIndex]['class'];
$class = $backtrace_arr[$this->_classIndex]['class'];
}
if (isset($backtrace_arr[$functionIndex]['function']) && $backtrace_arr[$functionIndex]['function'] != '')
if (isset($backtrace_arr[$this->_functionIndex]['function']) && $backtrace_arr[$this->_functionIndex]['function'] != '')
{
$function = $backtrace_arr[$functionIndex]['function'];
$function = $backtrace_arr[$this->_functionIndex]['function'];
}
if (isset($backtrace_arr[$lineIndex]['line']) && $backtrace_arr[$lineIndex]['line'] != '')
if (isset($backtrace_arr[$this->_lineIndex]['line']) && $backtrace_arr[$this->_lineIndex]['line'] != '')
{
$line = $backtrace_arr[$lineIndex]['line'];
$line = $backtrace_arr[$this->_lineIndex]['line'];
}
return $this->_format($class, $function, $line);
return array(
self::CLASS_NAME => $class,
self::FUNCTION_NAME => $function,
self::CODE_LINE => $line
);
}
/**
* format
* Formats the log message prefix (file system based)
*/
private function _format($class, $function, $line)
private function _getPrefix($callerData)
{
$formatted = LogLib::CALLER_PREFIX;
$formatted = self::CALLER_PREFIX;
if (!is_null($class) && $class != '')
if (!isEmptyString($callerData[self::CLASS_NAME]))
{
$formatted .= $class.LogLib::CLASS_POSTFIX;
$formatted .= $callerData[self::CLASS_NAME].self::CLASS_POSTFIX;
}
$formatted .= $function.LogLib::LINE_SEPARATOR.$line.LogLib::CALLER_POSTFIX.' ';
$formatted .= $callerData[self::FUNCTION_NAME].self::LINE_SEPARATOR.$callerData[self::CODE_LINE].self::CALLER_POSTFIX.' ';
return $formatted;
}
/**
* Formats the database description for a log
*/
private function _getDatabaseDescription($callerData)
{
$formatted = $callerData[self::FUNCTION_NAME].self::LINE_SEPARATOR.$callerData[self::CODE_LINE];
if (!isEmptyString($callerData[self::CLASS_NAME]))
{
$formatted = $callerData[self::CLASS_NAME].self::CLASS_POSTFIX.$formatted;
}
return $formatted;
}
@@ -1,13 +1,14 @@
<?php
class Webservicelog_model extends DB_Model
{
/**
* Constructor
*/
public function __construct()
{
parent::__construct();
$this->dbTable = 'system.tbl_webservicelog';
$this->pk = 'webservicelog_id';
}
@@ -0,0 +1,47 @@
<?php
$this->load->view(
'templates/FHC-Header',
array(
'title' => 'Logs viewer',
'jquery' => true,
'jqueryui' => true,
'bootstrap' => true,
'fontawesome' => true,
'sbadmintemplate' => true,
'tablesorter' => true,
'ajaxlib' => true,
'filterwidget' => true,
'navigationwidget' => true,
'phrases' => array(
'global' => array('mailAnXversandt'),
'ui' => array('bitteEintragWaehlen')
),
'customCSSs' => 'public/css/sbadmin2/tablesort_bootstrap.css',
'customJSs' => array('public/js/bootstrapper.js')
)
);
?>
<body>
<div id="wrapper">
<?php echo $this->widgetlib->widget('NavigationWidget'); ?>
<div id="page-wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-lg-12">
<h3 class="page-header">
JobsViewer
</h3>
</div>
</div>
<div>
<?php $this->load->view('system/logs/logsViewerData.php'); ?>
</div>
</div>
</div>
</div>
</body>
<?php $this->load->view('templates/FHC-Footer'); ?>
@@ -0,0 +1,66 @@
<?php
$filterWidgetArray = array(
'query' => '
SELECT wsl.webservicelog_id AS "LogId",
wsl.request_id AS "RequestId",
wsl.execute_time AS "ExecutionTime",
wsl.execute_user AS "ExecutedBy",
wsl.beschreibung AS "Description",
wsl.request_data AS "Data",
wsl.webservicetyp_kurzbz AS "WebserviceType"
FROM system.tbl_webservicelog wsl
ORDER BY wsl.execute_time DESC
',
'requiredPermissions' => 'admin',
'datasetRepresentation' => 'tablesorter',
'reloadDataset' => ($this->input->get('reloadDataset') == 'true' ? true : false),
'columnsAliases' => array(
'Log id',
'Request id',
'Execution time',
'Executed by',
'Producer',
'Data',
'Webservice type'
),
'formatRow' => function($datasetRaw) {
$datasetRaw->ExecutionTime = date_format(date_create($datasetRaw->ExecutionTime), 'd.m.Y H:i:s');
return $datasetRaw;
},
'markRow' => function($datasetRaw) {
$mark = '';
if ($datasetRaw->RequestId == 'Cronjob error')
{
$mark = 'text-red';
}
if ($datasetRaw->RequestId == 'Cronjob info')
{
$mark = 'text-green';
}
if ($datasetRaw->RequestId == 'Cronjob warning')
{
$mark = 'text-orange';
}
if ($datasetRaw->RequestId == 'Cronjob debug')
{
$mark = 'text-info';
}
return $mark;
}
);
$filterWidgetArray['app'] = 'core';
$filterWidgetArray['datasetName'] = 'logs';
$filterWidgetArray['filter_id'] = $this->input->get('filter_id');
echo $this->widgetlib->widget('FilterWidget', $filterWidgetArray);
?>
+16
View File
@@ -113,3 +113,19 @@
#applyFilter, #saveCustomFilterButton {
width: 100px;
}
.text-red {
color: red;
}
.text-orange {
color: orange;
}
.text-blue {
color: blue;
}
.text-green {
color: green;
}
+14
View File
@@ -3096,6 +3096,20 @@ if(!$result = @$db->db_query("SELECT projektphase_id FROM campus.tbl_zeitaufzeic
echo '<br>campus.tbl_zeitaufzeichnung: Spalte projektphase_id hinzugefuegt';
}
// Add new webservice type in system.tbl_webservicetyp
if ($result = @$db->db_query("SELECT 1 FROM system.tbl_webservicetyp WHERE webservicetyp_kurzbz = 'job';"))
{
if ($db->db_num_rows($result) == 0)
{
$qry = "INSERT INTO system.tbl_webservicetyp(webservicetyp_kurzbz, beschreibung) VALUES('job', 'Cronjob');";
if (!$db->db_query($qry))
echo '<strong>system.tbl_webservicetyp '.$db->db_last_error().'</strong><br>';
else
echo ' system.tbl_webservicetyp: Added webservice type "job"<br>';
}
}
// *** Pruefung und hinzufuegen der neuen Attribute und Tabellen
echo '<H2>Pruefe Tabellen und Attribute!</H2>';
+166 -1
View File
@@ -353,7 +353,7 @@ $filters = array(
{"name": "fakultaet"},
{"name": "datum"},
{"name": "uhrzeit"},
{"name": "anmeldefrist"},
{"name": "anmeldefrist"},
{"name": "oeffentlich"},
{"name": "studiengaenge"},
{"name": "freie_plaetze"},
@@ -460,6 +460,171 @@ $filters = array(
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'last7days',
'description' => '{Last 7 days logs}',
'sort' => 1,
'default_filter' => true,
'filter' => '
{
"name": "All logs from the last 7 days",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "7",
"option": "days"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'jobs14days',
'description' => '{Last 14 days jobs logs}',
'sort' => 2,
'default_filter' => false,
'filter' => '
{
"name": "All jobs logs from the last 14 days",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "job"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "14",
"option": "days"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'repots14days',
'description' => '{Last 14 days reports logs}',
'sort' => 3,
'default_filter' => false,
'filter' => '
{
"name": "All reports logs from the last 14 days",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "reports"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "14",
"option": "days"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'content3days',
'description' => '{Last 3 days content logs}',
'sort' => 4,
'default_filter' => false,
'filter' => '
{
"name": "All content logs from the last 3 days",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "content"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "3",
"option": "days"
}
]
}
',
'oe_kurzbz' => null,
),
array(
'app' => 'core',
'dataset_name' => 'logs',
'filter_kurzbz' => 'wienerlinien7days',
'description' => '{Last 7 days wiener linien logs}',
'sort' => 5,
'default_filter' => false,
'filter' => '
{
"name": "All wiener linien logs from the last 7 days",
"columns": [
{"name": "RequestId"},
{"name": "ExecutionTime"},
{"name": "ExecutedBy"},
{"name": "Description"},
{"name": "Data"}
],
"filters": [
{
"name": "WebserviceType",
"operation": "contains",
"condition": "wienerlinien"
},
{
"name": "ExecutionTime",
"operation": "lt",
"condition": "7",
"option": "days"
}
]
}
',
'oe_kurzbz' => null,
)
);