Bether Loggig Class

This commit is contained in:
Václav Španinger 2020-07-28 09:02:46 +02:00
parent 6d44750804
commit 70ef954b64
9 changed files with 84 additions and 50 deletions

View File

@ -39,6 +39,9 @@ class Autoloader {
spl_autoload_register("Autoloader::ClassLoader");
Autoloader::setRoot('/var/www/dev.steelants.cz/vasek/home-update/');
// import configs
require_once '../config/config.php';
class ErrorHandler {
static function exception($exception){
error_log($exception);
@ -52,7 +55,8 @@ class ErrorHandler {
echo json_encode($message);
$apiLogManager = new LogManager('../logs/apache/'. date("Y-m-d").'.log');
$apiLogManager->write("[APACHE] ERROR\n" . json_encode($message, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[APACHE] ERROR\n" . json_encode($message, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
}
}
set_exception_handler("ErrorHandler::exception");
@ -63,11 +67,12 @@ $json = file_get_contents('php://input');
$obj = json_decode($json, true);
$apiLogManager = new LogManager('../logs/api/'. date("Y-m-d").'.log');
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API] headers\n" . json_encode($_SERVER, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] headers\n" . json_encode($_SERVER, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] POST body\n" . json_encode($_POST, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
//Debug
error_reporting(E_ALL);
@ -80,8 +85,7 @@ ini_set('session.cookie_path', str_replace("login", "", str_replace('https://' .
ini_set('session.cookie_secure', '1');
mb_internal_encoding ("UTF-8");
// import configs
require_once '../config/config.php';
Debugger::flag('dbconnect');
//D B Conector

View File

@ -3,5 +3,6 @@ class CronApi extends ApiController {
public function clean(){
$logKeeper = new LogMaintainer();
$logKeeper->purge(LOGTIMOUT);
$this->response(['Value' => 'OK']);
}
}

View File

@ -9,6 +9,7 @@ class EndpointsApi extends ApiController{
//Log
$logManager = new LogManager();
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager = new LogManager('../logs//api/'. date("Y-m-d").'.log');
//Token Checks
@ -42,12 +43,12 @@ class EndpointsApi extends ApiController{
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordTypes::INFO);
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
$logManager->write("[API] Registering Device", LogRecordType::INFO);
$logManager->write("[API] Registering Device", LogRecordTypes::INFO);
$this->response([
'state' => 'unsuccess',
'errorMSG' => "Device not registeret",
@ -86,7 +87,7 @@ class EndpointsApi extends ApiController{
'command'=>'null'
];
DeviceManager::editByToken($obj['token'], $data);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordType::INFO);
$logManager->write("[API] Device_ID " . $deviceId . " executing command " . $command, LogRecordTypes::INFO);
}
}
@ -103,7 +104,7 @@ class EndpointsApi extends ApiController{
}
$subDeviceLastReordValue[$key] = $value['value'];
RecordManager::create($deviceId, $key, round($value['value'],3));
$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordType::INFO);
$logManager->write("[API] Device_ID " . $deviceId . " writed value " . $key . ' ' . $value['value'], LogRecordTypes::INFO);
//notification
if ($key == 'door' || $key == 'water') {
@ -131,7 +132,7 @@ class EndpointsApi extends ApiController{
if ($notificationData != []) {
$subscribers = $notificationMng::getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordType::INFO);
$logManager->write("[NOTIFICATION] SENDING TO" . $subscriber['id'] . " ", LogRecordTypes::INFO);
$notificationMng::sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}
@ -159,7 +160,7 @@ class EndpointsApi extends ApiController{
$subDeviceLastReordValue[$subDeviceData['type']] = $subDeviceLastReord['value'];
if ($subDeviceLastReord['execuded'] == 0){
$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordType::INFO);
$logManager->write("[API] subDevice_ID ".$subDeviceId . " executed comand with value " . json_encode($subDeviceLastReordValue) ." executed " . $subDeviceLastReord['execuded'], LogRecordTypes::INFO);
RecordManager::setExecuted($subDeviceLastReord['record_id']);
}
}

View File

@ -6,25 +6,27 @@ class GoogleHomeApi{
$obj = json_decode($json, true);
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager->setLevel(LOGLEVEL);
header('Content-Type: application/json');
switch ($obj['inputs'][0]['intent']) {
case 'action.devices.SYNC':
GoogleHome::sync($obj['requestId']);
$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordType::INFO);
$apiLogManager->write("[Google Home] action.devices.SYNC", LogRecordTypes::INFO);
break;
case 'action.devices.QUERY':
GoogleHome::query($obj['requestId'], $obj['inputs'][0]['payload']);
$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordType::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[Google Home] action.devices.QUERY", LogRecordTypes::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
break;
case 'action.devices.EXECUTE':
GoogleHome::execute($obj['requestId'], $obj['inputs'][0]['payload']);
$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordType::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[Google Home] action.devices.EXECUTE", LogRecordTypes::INFO);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
break;
}
@ -35,8 +37,10 @@ class GoogleHomeApi{
$obj = json_decode($json, true);
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API] request body\n" . json_encode($obj, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->write("[API] GET body\n" . json_encode($_GET, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$get = [
"access_token"=>"2222255888",

View File

@ -12,6 +12,7 @@ class UpdatesApi {
public function default(){
header('Content-type: text/plain; charset=utf8', true);
$logManager = new LogManager('../logs/ota/'. date("Y-m-d").'.log');
$logManager->setLevel(LOGLEVEL);
//Filtrování IP adress
/* if (DEBUGMOD != 1) {
@ -21,7 +22,7 @@ class UpdatesApi {
'errorMSG' => "Using API from your IP insnt alowed!",
));
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::WARNING);
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordTypes::WARNING);
exit();
}
}*/
@ -29,10 +30,10 @@ class UpdatesApi {
$macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
$localBinary = "../updater/" . str_replace(':', '', $macAddress) . ".bin";
$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
$logManager->write("[Updater] url: " . $localBinary, LogRecordTypes::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordTypes::INFO);
if (file_exists($localBinary)) {
$logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordType::INFO);
$logManager->write("[Updater] version PHP: \n" . md5_file($localBinary), LogRecordTypes::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
$this->sendFile($localBinary);
//get device data
@ -40,8 +41,8 @@ class UpdatesApi {
$deviceName = $device['name'];
$deviceId = $device['device_id'];
//logfile write
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
$logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordType::INFO);
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordTypes::WARNING);
$logManager->write("[Device] version hash: \n" . md5_file($localBinary), LogRecordTypes::INFO);
//notification
$notificationMng = new NotificationManager;
$notificationData = [
@ -52,7 +53,7 @@ class UpdatesApi {
if ($notificationData != []) {
$subscribers = $notificationMng->getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordTypes::INFO);
$answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}

View File

@ -10,9 +10,10 @@ class UpdatesApi {
}
public function default(){
header('Content-type: text/plain; charset=utf8', true);
header('Content-type: text/plain; charset=utf8', true);
$logManager = new LogManager('../logs/ota/'. date("Y-m-d").'.log');
$logManager->write("[Updater] Client Connected", LogRecordType::WARNING);
$logManager->setLevel(LOGLEVEL);
$logManager->write("[Updater] Client Connected", LogRecordTypes::WARNING);
//Filtrování IP adress
@ -23,17 +24,17 @@ class UpdatesApi {
'errorMSG' => "Using API from your IP insnt alowed!",
));
header($_SERVER["SERVER_PROTOCOL"]." 401 Unauthorized");
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordType::INFO);
$logManager->write("[Updater] acces denied from " . $_SERVER['REMOTE_ADDR'], LogRecordTypes::INFO);
exit();
}
}
$macAddress = $_SERVER['HTTP_X_ESP8266_STA_MAC'];
$localBinary = "../updater/" . str_replace(':', '', $macAddress) . ".bin";
$logManager->write("[Updater] url: " . $localBinary, LogRecordType::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordType::INFO);
$logManager->write("[Updater] url: " . $localBinary, LogRecordTypes::INFO);
$logManager->write("[Updater] version: " . $_SERVER['HTTP_X_ESP8266_SKETCH_MD5'], LogRecordTypes::INFO);
if (file_exists($localBinary)) {
$logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordType::INFO);
$logManager->write("[Updater] version PHP: " . md5_file($localBinary), LogRecordTypes::INFO);
if ($_SERVER['HTTP_X_ESP8266_SKETCH_MD5'] != md5_file($localBinary)) {
$this->sendFile($localBinary);
//get device data
@ -41,8 +42,8 @@ class UpdatesApi {
$deviceName = $device['name'];
$deviceId = $device['device_id'];
//logfile write
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordType::WARNING);
$logManager->write("[Device] version hash: " . md5_file($localBinary), LogRecordType::INFO);
$logManager->write("[Device] device_ID " . $deviceId . " was just updated to new version", LogRecordTypes::WARNING);
$logManager->write("[Device] version hash: " . md5_file($localBinary), LogRecordTypes::INFO);
//notification
$notificationMng = new NotificationManager;
$notificationData = [
@ -53,7 +54,7 @@ class UpdatesApi {
if ($notificationData != []) {
$subscribers = $notificationMng->getSubscription();
foreach ($subscribers as $key => $subscriber) {
$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordType::INFO);
$logManager->write("[NOTIFICATION] SENDING TO " . $subscriber['id'] . " ", LogRecordTypes::INFO);
$answer = $notificationMng->sendSimpleNotification(SERVERKEY, $subscriber['token'], $notificationData);
}
}

View File

@ -55,7 +55,8 @@ class GoogleHome {
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
echo json_encode($response);
}
@ -147,7 +148,8 @@ class GoogleHome {
],
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->write("[API][$requestId] request response\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
$apiLogManager->setLevel(LOGLEVEL);
echo json_encode($response);
}
@ -218,7 +220,8 @@ static function execute($requestId, $payload){
];
$apiLogManager = new LogManager('../logs/google-home/'. date("Y-m-d").'.log');
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordType::INFO);
$apiLogManager->setLevel(LOGLEVEL);
$apiLogManager->write("[API][EXECUTE][$requestId]\n" . json_encode($response, JSON_PRETTY_PRINT), LogRecordTypes::INFO);
echo json_encode($response);
}

View File

@ -3,17 +3,14 @@
*
*/
class LogRecordType{
const WARNING = 'warning';
const ERROR = 'error';
const INFO = 'info';
}
class LogManager
{
private $logFile;
function __construct($fileName = "")
private $logLevel = 1;
public function __construct($fileName = "")
{
if ($fileName == ""){
$fileName = '../logs/'. date("Y-m-d").'.log';
@ -22,15 +19,22 @@ class LogManager
{
mkdir("../logs/");
}
$this->logFile = fopen($fileName, "a") or die("Unable to open file!");
}
function write($value, $type = LogRecordType::ERROR){
$record = "[".date("H:m:s")."][".$type."]" . $value . "\n";
fwrite($this->logFile, $record);
public function setLevel($type = LogRecordTypess::WARNING){
$logLevel = $type['level'];
}
function __destruct(){
public function write($value, $type = LogRecordTypess::ERROR){
if ($type['level'] <= $this->logLevel) {
$record = "[".date("H:m:s")."][".$type['identifier']."]" . $value . "\n";
fwrite($this->logFile, $record);
}
}
public function __destruct(){
if (isset($this->logFile)) {
fclose($this->logFile);
}

View File

@ -0,0 +1,15 @@
<?php
class LogRecordTypes{
const ERROR = [
'level' => 0,
'identifier' => 'error',
];
const WARNING = [
'level' => 1,
'identifier' =>'warning',
];
const INFO = [
'level' => 2,
'identifier' => 'info',
];
}