Created
February 19, 2019 06:50
-
-
Save fhdalikhan/8d3b84c5f3dd9310c9ece3192494ed71 to your computer and use it in GitHub Desktop.
A PHP logging helper class
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 application\classes\LogHelper; | |
class LogHelper | |
{ | |
protected $logFile; | |
protected $deeplogFile; | |
public function __construct() | |
{ | |
$this->logFile = __DIR__ . DIRECTORY_SEPARATOR . 'logs/fb-api-logs.txt'; | |
$this->deeplogFile = __DIR__ . DIRECTORY_SEPARATOR . 'logs/all-logs.txt'; | |
if (! file_exists($this->logFile) ) { | |
touch($this->logFile); | |
} | |
} | |
public function log($message, $severity = 'error') | |
{ | |
$handle = fopen($this->logFile, 'a+'); | |
$message = sprintf('[%s] %s: %s %s', date('Y-m-d H:i:s'), $severity, $message, PHP_EOL); | |
fwrite($handle, $message); | |
fclose($handle); | |
return $this; | |
} | |
/** | |
* @param $logMessage mixed|array | |
* @param $type string | |
* @param $severity string | |
* @return self | |
**/ | |
public function deepLog($logMessage, $type = '', $severity = 'error') | |
{ | |
$handle = fopen($this->deeplogFile, 'a+'); | |
if ( is_array($logMessage) ) { | |
$logMessage = print_r($logMessage, true); | |
} | |
$message = sprintf('[%s] %s [%s] : %s URI: (%s) QS: (%s) {%s} %s', date('Y-m-d H:i:s'), $type, $severity, $logMessage, $_SERVER['REQUEST_URI'], $_SERVER['QUERY_STRING'], $_SERVER['REMOTE_ADDR'], PHP_EOL . PHP_EOL); | |
fwrite($handle, $message); | |
fclose($handle); | |
return $this; | |
} | |
} | |
// usage | |
// $logHelper = new LogHelper(); | |
// $logHelper->log('Bad request' . ' ' . __FILE__ . ':' . __LINE__); | |
// $logHelper->deepLog($_GET, $_SERVER['REQUEST_METHOD'], 'info'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment