-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract logging logic outside Util class (#60)
- Loading branch information
Showing
7 changed files
with
369 additions
and
46 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Tpay\OriginApi\Utilities; | ||
|
||
class FileLogger | ||
{ | ||
/** @var null|string */ | ||
private $logFilePath; | ||
|
||
/** @param null|string $logFilePath */ | ||
public function __construct($logFilePath) | ||
{ | ||
$this->logFilePath = $logFilePath; | ||
} | ||
|
||
public function log($level, $message, array $context = []) | ||
{ | ||
$this->info($message); | ||
} | ||
|
||
/** | ||
* @param string $message | ||
* | ||
* @throws TException | ||
*/ | ||
public function info($message) | ||
{ | ||
$content = json_decode($message, true); | ||
$logText = PHP_EOL.'==========================='; | ||
$logText .= PHP_EOL.$content['title']; | ||
$logText .= PHP_EOL.'==========================='; | ||
$logText .= PHP_EOL.$content['date']; | ||
$logText .= PHP_EOL.'ip: '.$content['ip']; | ||
$logText .= PHP_EOL; | ||
$logText .= $content['message']; | ||
$logText .= PHP_EOL; | ||
|
||
$this->checkLogFile(); | ||
file_put_contents($this->getLogPath(), $logText, FILE_APPEND); | ||
} | ||
|
||
/** @return string */ | ||
private function getLogPath() | ||
{ | ||
$logFileName = sprintf('log_%s.log', date('Y-m-d')); | ||
|
||
if (null !== $this->logFilePath) { | ||
$logPath = $this->logFilePath.$logFileName; | ||
} else { | ||
$logPath = sprintf('%s/../Logs/%s', __DIR__, $logFileName); | ||
} | ||
|
||
return $logPath; | ||
} | ||
|
||
/** @throws TException */ | ||
private function checkLogFile() | ||
{ | ||
$logFilePath = $this->getLogPath(); | ||
|
||
if (!file_exists($logFilePath)) { | ||
file_put_contents($logFilePath, '<?php exit; ?> '.PHP_EOL); | ||
chmod($logFilePath, 0644); | ||
} | ||
|
||
if (!file_exists($logFilePath) || !is_writable($logFilePath)) { | ||
throw new TException('Unable to create or write the log file'); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
|
||
namespace Tpay\OriginApi\Utilities; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Psr\Log\NullLogger; | ||
|
||
class Logger | ||
{ | ||
/** @var LoggerInterface */ | ||
private static $logger; | ||
|
||
/** @var string */ | ||
private static $customLogPath; | ||
|
||
public static function disableLogging() | ||
{ | ||
self::$logger = new NullLogger(); | ||
} | ||
|
||
/** | ||
* @param mixed $logger | ||
* | ||
* @throws TException | ||
*/ | ||
public static function setLogger($logger) | ||
{ | ||
if (false === assert($logger instanceof LoggerInterface)) { | ||
throw new TException(sprintf('%s is not instance of LoggerInterface', get_class($logger))); | ||
} | ||
|
||
self::$logger = $logger; | ||
} | ||
|
||
/** @param string $logPath */ | ||
public static function setLogPath($logPath) | ||
{ | ||
self::$customLogPath = $logPath; | ||
} | ||
|
||
/** @return FileLogger|LoggerInterface */ | ||
public static function getLogger() | ||
{ | ||
if (null === self::$logger) { | ||
self::$logger = new FileLogger(self::$customLogPath); | ||
} | ||
|
||
return self::$logger; | ||
} | ||
|
||
/** | ||
* @param string $title | ||
* @param string $text | ||
* @param string $logLevel | ||
*/ | ||
public static function log($title, $text, $logLevel = 'info') | ||
{ | ||
$ip = (isset($_SERVER['REMOTE_ADDR'])) ? $_SERVER['REMOTE_ADDR'] : 'Empty server REMOTE_ADDR'; | ||
$content = [ | ||
'ip' => $ip, | ||
'title' => $title, | ||
'date' => date('Y-m-d H:i:s'), | ||
'message' => $text, | ||
'logLevel' => $logLevel, | ||
]; | ||
|
||
self::getLogger()->log($logLevel, json_encode($content)); | ||
} | ||
|
||
/** @param string $text */ | ||
public static function logLine($text) | ||
{ | ||
self::$logger->info((string) $text); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace Tpay\OriginApi\Utilities\phpseclib\Crypt; | ||
|
||
use RuntimeException; | ||
|
||
if (class_exists('phpseclib3\Crypt\RSA')) { | ||
abstract class RSA extends \phpseclib3\Crypt\RSA {} | ||
} elseif (class_exists('phpseclib\Crypt\RSA')) { | ||
class RSA extends \phpseclib\Crypt\RSA {} | ||
} else { | ||
throw new RuntimeException('Cannot find supported phpseclib3/phpseclib library'); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
namespace Tpay\OriginApi\Utilities\phpseclib\File; | ||
|
||
use RuntimeException; | ||
|
||
if (class_exists('phpseclib3\File\X509')) { | ||
class X509 extends \phpseclib3\File\X509 | ||
{ | ||
public function withSettings($publicKey, $hash, $signature) | ||
{ | ||
return $publicKey->withHash($hash)->withPadding($signature); | ||
} | ||
} | ||
} elseif (class_exists('phpseclib\File\X509')) { | ||
class X509 extends \phpseclib\File\X509 | ||
{ | ||
public function withSettings($publicKey, $hash, $signature) | ||
{ | ||
$publicKey->setHash($hash); | ||
$publicKey->setSignatureMode($signature); | ||
|
||
return $publicKey; | ||
} | ||
} | ||
} else { | ||
throw new RuntimeException('Cannot find supported phpseclib/phpseclib library'); | ||
} |
Oops, something went wrong.