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; | |
} | |
} |
Example Usage
include 'TinyLog.php';
$log_path = __DIR__ . '\logs';
$tiny_log = new TinyLog();
$tiny_log->setLogStorePath($log_path);
for ($i = 0; $i <= 10; $i++) {
$log_message = array(
"name" => "Tom $i",
"lname" => "Cat $i"
);
$tiny_log->debug("Something", $log_message);
$tiny_log->info("Something", $log_message);
$tiny_log->error("Something", $log_message);
}
<?php
session_start();
include 'TinyLog.php';
include 'FileReader.php';
$log_path = __DIR__ . '/logs';
$tiny_log = new TinyLog\TinyLog();
$fileReader = new TinyLog\FileReader();
$tiny_log->setLogStorePath($log_path);
if (isset($_SESSION['log_files']) === false) {
$log_files = $tiny_log->readLogs(\TinyLog\TinyLog::TYPE_ERROR);
} else {
$log_files = $_SESSION['log_files'];
}
$log_files = $tiny_log->readLogs(\TinyLog\TinyLog::TYPE_ERROR);
//var_dump($log_files);
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css"
integrity_no="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
<title>LOG PREVIEW</title>
</head>
<body>
<div class="container">
<div class="col">
<table class="table">
<thead>
<tr>
<th>LOG TYPE</th>
<th>FILES</th>
</tr>
</thead>
<tbody>
<?php foreach ($log_files as $key => $files): ?>
<tr>
<td><?= strtoupper($key); ?></td>
<td>
<ul>
<?php foreach ($files as $file): ?>
<li>
<strong><?= $file ?></strong>
<pre class="json-read"><?= $fileReader->getLogFileContent($file); ?></pre>
</li>
<?php endforeach; ?>
</ul>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
</div>
</div>
<style>
.json-read {
height: 400px;
overflow: auto;
color: green;
background-color: #212121;
}
</style>
<!-- Optional JavaScript -->
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity_no="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js"
integrity_no="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ"
crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js"
integrity_no="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm"
crossorigin="anonymous"></script>
</body>
</html>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Improved for log analytics.