Skip to content

Commit

Permalink
Fix error with logger
Browse files Browse the repository at this point in the history
  • Loading branch information
nenes25 committed Mar 27, 2024
1 parent d73e05f commit 4738e5b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
V 0.4.1 2024-03-27 : #7 Error with logger
V 0.4.0 2024-03-14 : #1 Harmonize logs and move them to global var directory
#5 Unable to generate an upgrade if only 1 event
#6 Allow to generate file upgrade from CLI
Expand Down
21 changes: 16 additions & 5 deletions hhmodulesmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class HhModulesManager extends Module
];

/** @var LoggerInterface|null */
protected ?LoggerInterface $logger = null;
protected $logger = null;

public function __construct()
{
$this->name = 'hhmodulesmanager';
$this->tab = 'administration';
$this->version = '0.4.0';
$this->version = '0.4.1';
$this->author = 'hhennes';
$this->bootstrap = true;
parent::__construct();
Expand Down Expand Up @@ -264,7 +264,18 @@ protected function logEvent(string $name, string $type, ?string $key = null, arr
public function log(string $message, int $level = 3): void
{
try {
$this->getLogger()->log($level * 100, $message);
$logger = $this->getLogger();
if ($logger) {
$logger->log($level * 100, $message);
} else {
//In some context the logger might not be defined due to missing symfony context
//So we make a fallback
file_put_contents(
_PS_ROOT_DIR_ . '/var/logs/' . $this->name . '/' . $this->name . '.log',
'['.date('Y-m-d H:i:s') . '] - '.$this->name .'.'. $level . ' - ' . $message . "\n",
FILE_APPEND
);
}
} catch (Exception $e) {
file_put_contents(
dirname(__FILE__) . '/logs/exceptions.log',
Expand All @@ -277,11 +288,11 @@ public function log(string $message, int $level = 3): void
/**
* Get logger interface from service
*
* @return LoggerInterface
* @return LoggerInterface|null|false
*
* @throws Exception
*/
protected function getLogger(): LoggerInterface
protected function getLogger()
{
if (null === $this->logger) {
$this->logger = $this->get('hhennes.modulesmanager.logger');
Expand Down

0 comments on commit 4738e5b

Please sign in to comment.