Skip to content

Commit

Permalink
Merge pull request #240 from TransbankDevelopers/fix/save-in-cache-on…
Browse files Browse the repository at this point in the history
…ly-log-file-name

fix: save in cache only log file name
  • Loading branch information
mastudillot authored Jun 3, 2024
2 parents 0dfc8e7 + e5734a8 commit b35f6a9
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 20 deletions.
82 changes: 64 additions & 18 deletions plugin/shared/Helpers/PluginLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
use Monolog\Formatter\LineFormatter;
use Transbank\Plugin\Model\LogConfig;

final class PluginLogger implements ILogger {
final class PluginLogger implements ILogger
{

const CACHE_LOG_NAME = 'transbank_log_file_name';

private $logger;
private $config;

Expand All @@ -18,32 +22,52 @@ final class PluginLogger implements ILogger {
* output format : "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"
* @param Throwable $e
*/
public function __construct(LogConfig $config) {
public function __construct(LogConfig $config)
{
$this->config = $config;
$logDir = $this->config->getLogDir();
$cacheLogName = 'transbank_log_name';
$logFile = get_transient($cacheLogName);
if (!$logFile) {
$uniqueId = uniqid('', true);
$logFile = "{$logDir}/log_transbank_{$uniqueId}.log";
$expireTime = strtotime('tomorrow') - time();
set_transient($cacheLogName, $logFile, $expireTime);
}

$logFilePath = $this->getLogFilePath();
$this->initializeLogger($logFilePath);
}

private function initializeLogger(string $logFilePath)
{
$dateFormat = "Y-m-d H:i:s";
$output = "%datetime% > %level_name% > %message% %context% %extra%\n";
$formatter = new LineFormatter($output, $dateFormat);
$stream = new RotatingFileHandler($logFile,
100, Logger::DEBUG);

$stream = new RotatingFileHandler(
$logFilePath,
100,
Logger::DEBUG
);
$stream->setFormatter($formatter);

$this->logger = new Logger('transbank');
$this->logger->pushHandler($stream);
}

public function getLogger(){
private function getLogFilePath(): string
{
$logFileName = $this->getLogFileNameFromCache();

if (!$logFileName) {
$logFileName = $this->getLogFileName();
$expireTime = strtotime('tomorrow') - time();
$this->saveLogFileNameInCache($logFileName, $expireTime);
}

$logDir = $this->getLogDir();
return $logDir . $logFileName;
}

public function getLogger()
{
return $this->logger;
}

public function getConfig(){
public function getConfig()
{
return $this->config;
}

Expand All @@ -64,7 +88,7 @@ public function logError($msg)

public function getInfo()
{
$files = glob($this->config->getLogDir().'/*.log');
$files = glob($this->config->getLogDir() . '/*.log');
if (!$files) {
return [
'dir' => $this->config->getLogDir(),
Expand All @@ -77,7 +101,7 @@ public function getInfo()
arsort($files);

$logs = [];
foreach($files as $key=>$value) {
foreach ($files as $key => $value) {
$logs[] = [
"filename" => basename($key),
"modified" => $value
Expand All @@ -97,7 +121,7 @@ public function getLogDetail($filename, $replaceNewline = false)
if ($filename == '') {
return [];
}
$fle = $this->config->getLogDir().'/'.$filename;
$fle = $this->config->getLogDir() . '/' . $filename;
$content = file_get_contents($fle);
if ($replaceNewline && $content !== false) {
$content = str_replace("\n", '#@#', $content);
Expand All @@ -122,4 +146,26 @@ private function formatBytes($path)
}
return $bytes;
}

private function getLogDir(): string
{
$logDir = $this->config->getLogDir();
return trailingslashit($logDir);
}

private function getLogFileName(): string
{
$uniqueId = uniqid('', true);
return 'log_transbank_' . $uniqueId . '.log';
}

private function getLogFileNameFromCache()
{
return get_transient(self::CACHE_LOG_NAME);
}

private function saveLogFileNameInCache(string $logFileName, int $expireTime)
{
set_transient(self::CACHE_LOG_NAME, $logFileName, $expireTime);
}
}
6 changes: 4 additions & 2 deletions plugin/templates/admin/log.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@
if (!$folderHasLogs) {
$options = "<option value='' selected>No hay archivos log</option>";
}

foreach ($resume['logs'] as $index) {
$options .= "<option value='{$index['filename']}'>{$index['filename']}</option>";

if($index['filename'] == basename($lastLog['filename'])) {
$options .= "<option value='{$index['filename']}' selected>{$index['filename']}</option>";
continue;
}

$options .= "<option value='{$index['filename']}'>{$index['filename']}</option>";
}

echo $options;
Expand Down

0 comments on commit b35f6a9

Please sign in to comment.