Last active
April 19, 2018 06:53
-
-
Save pranid/c6cebc440b1a04031865cf632988caa3 to your computer and use it in GitHub Desktop.
PHP Logger for analytics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TinyLog; | |
/** | |
* Tiny Log Generator | |
* @Author Praneeth Nidarshan | |
* @email [email protected] | |
* @version 2.5.1 | |
* | |
*/ | |
class TinyLog | |
{ | |
const LINE_BREAK = "\n"; | |
const TYPE_ERROR = 'error'; | |
const TYPE_INFO = 'info'; | |
const TYPE_DEBUG = 'debug'; | |
/** | |
* @var String | |
*/ | |
private $log_store_path; | |
/** | |
* @var String | |
*/ | |
private $log_message; | |
/** | |
* @var string | |
*/ | |
private $file_name; | |
/** | |
* @var string | |
*/ | |
private $log_folder; | |
/** | |
* @var \TinyLog\FileReader | |
*/ | |
private $fileReader; | |
/** | |
* TinyLog constructor. | |
*/ | |
public function __construct() | |
{ | |
$this->file_name = date('Ymd'); | |
$this->fileReader = new FileReader(); | |
} | |
/** | |
* @param $description | |
* @param array $data | |
*/ | |
public function debug($description, $data = array()) | |
{ | |
$this->log_folder = "debug"; | |
$this->setLogMessage(['description' => $description, 'data' => $data]); | |
$this->writeLog(); | |
} | |
/** | |
* @param array $log_message | |
*/ | |
private function setLogMessage($log_message) | |
{ | |
$this->log_message = ['id' => $this->getLogId(), 'timestamp' => $this->getTimeStamp(), 'log' => $log_message]; | |
} | |
/** | |
* @return false|int | |
*/ | |
private function getLogId() | |
{ | |
return strtotime($this->getTimeStamp()); | |
} | |
/** | |
* @return false|string | |
*/ | |
private function getTimeStamp() | |
{ | |
return date('Y-m-d H:i:s'); | |
} | |
/** | |
* | |
*/ | |
private function writeLog() | |
{ | |
try { | |
$file_name = $this->getLogStorePath() . '/' . $this->getLogFileName() . ".log"; | |
if (!file_exists($file_name)) { | |
@mkdir($this->getLogStorePath(), 0777, true); | |
} | |
$log_file = fopen($file_name, (file_exists($file_name)) ? 'a' : 'w'); | |
$txt = $this->getLogMessage() . self::LINE_BREAK; | |
fwrite($log_file, $txt); | |
fclose($log_file); | |
} catch (\Exception $e) { | |
error_log($e->getMessage(), "TINY LOG"); | |
} | |
} | |
/** | |
* @return String | |
*/ | |
private function getLogStorePath() | |
{ | |
return $this->log_store_path . "/" . $this->log_folder; | |
} | |
/** | |
* @return String | |
*/ | |
private function getLogFileName() | |
{ | |
return $this->file_name; | |
} | |
/** | |
* @return String | |
*/ | |
private function getLogMessage() | |
{ | |
return json_encode($this->log_message); | |
} | |
/** | |
* @param mixed $log_store_path | |
*/ | |
public function setLogStorePath($log_store_path) | |
{ | |
$this->log_store_path = $log_store_path; | |
} | |
/** | |
* @param $description | |
* @param array $data | |
*/ | |
public function info($description, $data = array()) | |
{ | |
$this->log_folder = "info"; | |
$this->setLogMessage(['description' => $description, 'data' => $data]); | |
$this->writeLog(); | |
} | |
/** | |
* @param $description | |
* @param array $data | |
*/ | |
public function error($description, $data = array()) | |
{ | |
$this->log_folder = "error"; | |
$this->setLogMessage(['description' => $description, 'data' => $data]); | |
$this->writeLog(); | |
} | |
/** | |
* @param null $type | |
* @return array | |
* @throws \Exception | |
*/ | |
public function readLogs($type = null) | |
{ | |
$path = $this->getLogStorePath(); | |
if (isset($path) === false) { | |
throw new \Exception("Log folder path is empty."); | |
} | |
$this->fileReader->setFolderPath($path); | |
$log_types = $this->fileReader->getLogTypes(); | |
$log_files = array(); | |
foreach ($log_types as $key => $log_folder) { | |
$this->fileReader->setFolderPath($log_folder); | |
if (isset($type) && $type == $key) { | |
$log_files[$key] = $this->fileReader->read(); | |
return $log_files; | |
} else if (isset($type) === false) { | |
$log_files[$key] = $this->fileReader->read(); | |
} | |
} | |
return $log_files; | |
} | |
} |
Author
pranid
commented
Apr 19, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment