mirror of
https://github.com/standardebooks/web.git
synced 2025-07-15 19:06:49 -04:00
Replace Logger static class with Log class
This commit is contained in:
parent
f7558eab3a
commit
934545c191
6 changed files with 73 additions and 69 deletions
39
lib/Log.php
Normal file
39
lib/Log.php
Normal file
|
@ -0,0 +1,39 @@
|
|||
<?
|
||||
use function Safe\fopen;
|
||||
use function Safe\fwrite;
|
||||
use function Safe\fclose;
|
||||
use function Safe\error_log;
|
||||
use function Safe\gmdate;
|
||||
|
||||
class Log{
|
||||
private $RequestId = null;
|
||||
private $LogFilePath = null;
|
||||
|
||||
public function __construct(?string $logFilePath){
|
||||
// Get a semi-random ID to identify this request within the log.
|
||||
$this->RequestId = substr(sha1(time() . rand()), 0, 8);
|
||||
$this->LogFilePath = $logFilePath;
|
||||
}
|
||||
|
||||
public function Write(string $text): void{
|
||||
if($this->LogFilePath === null){
|
||||
self::WriteErrorLogEntry($text);
|
||||
}
|
||||
else{
|
||||
try{
|
||||
$fp = fopen($this->LogFilePath, 'a+');
|
||||
}
|
||||
catch(\Exception $ex){
|
||||
self::WriteErrorLogEntry('Couldn\'t open log file: ' . $this->LogFilePath . '. Exception: ' . vds($ex));
|
||||
return;
|
||||
}
|
||||
|
||||
fwrite($fp, gmdate('Y-m-d H:i:s') . "\t" . $this->RequestId . "\t" . $text . "\n");
|
||||
fclose($fp);
|
||||
}
|
||||
}
|
||||
|
||||
public static function WriteErrorLogEntry(string $text): void{
|
||||
error_log($text);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue