Skip to content

Commit

Permalink
Updated
Browse files Browse the repository at this point in the history
  • Loading branch information
hungnguyenhp committed Jun 24, 2022
1 parent bf17c2b commit e3869e9
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 0 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"ext-mbstring": "*",
"nguyenanhung/codeigniter-basic-helper": "^1.0"
},
"require-dev": {
"nguyenanhung/my-debug": "^3.0 || ^2.0 || ^1.0"
},
"autoload": {
"classmap": [
"hungng/"
Expand Down
87 changes: 87 additions & 0 deletions hungng/HungNG_CI_Base_Controllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ public function __construct()
parent::__construct();
}

/**
* Function renderOutput
*
* @param $response
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 09/03/2021 38:04
*/
protected function renderOutput($response)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
}
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response))->_display();
exit;
}

/**
* Function jsonResponse
*
Expand Down Expand Up @@ -210,5 +232,70 @@ protected function errorSignatureResponse($validSignature = array())

return $response;
}

/**
* Function log
*
* @param $name
* @param $message
* @param $context
* @param $inputLevel
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 25/06/2022 32:45
*/
protected function log($name = '', $message = '', $context = array(), $inputLevel = 'info')
{
try {
if (class_exists('Monolog\Logger') && class_exists('Monolog\Formatter\LineFormatter') && class_exists('Monolog\Handler\StreamHandler')) {
$useLevel = strtolower($inputLevel);
switch ($useLevel) {
case 'debug':
$keyLevel = Monolog\Logger::DEBUG;
break;
case 'notice':
$keyLevel = Monolog\Logger::NOTICE;
break;
case 'warning':
$keyLevel = Monolog\Logger::WARNING;
break;
case 'error':
$keyLevel = Monolog\Logger::ERROR;
break;
case 'critical':
$keyLevel = Monolog\Logger::CRITICAL;
break;
case 'alert':
$keyLevel = Monolog\Logger::ALERT;
break;
case 'emergency':
$keyLevel = Monolog\Logger::EMERGENCY;
break;
default:
$keyLevel = Monolog\Logger::INFO;
}
$fileName = __DIR__ . '/../../storage/logs/Log-' . date('Y-m-d') . '.log';
$formatter = new Monolog\Formatter\LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", "Y-m-d H:i:s u");
$stream = new Monolog\Handler\StreamHandler($fileName, $keyLevel, true, 0777);
$stream->setFormatter($formatter);
$logger = new Monolog\Logger(trim($name));
$logger->pushHandler($stream);
if (is_array($context)) {
$logger->$useLevel($message, $context);
} else {
$logger->$useLevel($message . json_encode($context));
}
} else {
log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context));
}
} catch (InvalidArgumentException $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
} catch (Exception $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
}
}
}
}
87 changes: 87 additions & 0 deletions hungng/HungNG_CI_Base_Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,28 @@ public function __construct()
parent::__construct();
}

/**
* Function renderOutput
*
* @param $response
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 09/03/2021 38:04
*/
protected function renderOutput($response)
{
$method = $this->input->method(true);
$ip = getIPAddress();
$userAgent = $this->input->user_agent(true);
$message = 'Received ' . $method . ' Request from IP: ' . $ip . ' - With User Agent: ' . $userAgent;
if (method_exists($this, 'log')) {
$this->log('RequestAPI', $message, $response);
}
$this->output->set_status_header()->set_content_type('application/json', 'utf-8')->set_output(json_encode($response))->_display();
exit;
}

/**
* Function jsonResponse
*
Expand Down Expand Up @@ -210,5 +232,70 @@ protected function errorSignatureResponse($validSignature = array())

return $response;
}

/**
* Function log
*
* @param $name
* @param $message
* @param $context
* @param $inputLevel
*
* @author : 713uk13m <[email protected]>
* @copyright: 713uk13m <[email protected]>
* @time : 25/06/2022 32:45
*/
protected function log($name = '', $message = '', $context = array(), $inputLevel = 'info')
{
try {
if (class_exists('Monolog\Logger') && class_exists('Monolog\Formatter\LineFormatter') && class_exists('Monolog\Handler\StreamHandler')) {
$useLevel = strtolower($inputLevel);
switch ($useLevel) {
case 'debug':
$keyLevel = Monolog\Logger::DEBUG;
break;
case 'notice':
$keyLevel = Monolog\Logger::NOTICE;
break;
case 'warning':
$keyLevel = Monolog\Logger::WARNING;
break;
case 'error':
$keyLevel = Monolog\Logger::ERROR;
break;
case 'critical':
$keyLevel = Monolog\Logger::CRITICAL;
break;
case 'alert':
$keyLevel = Monolog\Logger::ALERT;
break;
case 'emergency':
$keyLevel = Monolog\Logger::EMERGENCY;
break;
default:
$keyLevel = Monolog\Logger::INFO;
}
$fileName = __DIR__ . '/../../storage/logs/Log-' . date('Y-m-d') . '.log';
$formatter = new Monolog\Formatter\LineFormatter("[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n", "Y-m-d H:i:s u");
$stream = new Monolog\Handler\StreamHandler($fileName, $keyLevel, true, 0777);
$stream->setFormatter($formatter);
$logger = new Monolog\Logger(trim($name));
$logger->pushHandler($stream);
if (is_array($context)) {
$logger->$useLevel($message, $context);
} else {
$logger->$useLevel($message . json_encode($context));
}
} else {
log_message($inputLevel, $name . " | Error Message: " . $message . ' | Context: ' . json_encode($context));
}
} catch (InvalidArgumentException $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
} catch (Exception $exception) {
log_message('error', $exception->getMessage());
log_message('error', $exception->getTraceAsString());
}
}
}
}
2 changes: 2 additions & 0 deletions hungng/HungNG_Custom_Based_model.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ public function page_limit($size = 500, $page = 0)

return $this->db->limit($size, $start);
}

return $this->db;
}

/**
Expand Down

0 comments on commit e3869e9

Please sign in to comment.