Hi all. I'm getting a lot of troubles on a model because of it's logger.
The first thing I've noticed is that I need to initialize the logger inside the method onConstruct because if I use the inizitialize model method, many methods of the class give an error saying $this->logger is null.
Maybe because initialize is run only at the first invocation, but i'd tought that the protected attributes should be accessible once initialized.
That's my initialize:
public function initialize($params = null)
{
/** gestione dei log **/
if(!is_dir(LOG_PATH . "confirm/"))
if(!mkdir(LOG_PATH . "confirm/",0755))
die("create directory ". LOG_PATH . "confirm/"." failed");
$formatter = new \Phalcon\Logger\Formatter\Line("[".date("Y-m-d H:i:s")."][" . HOSTNAME . "][%type%] %message%");
$this->logger = new \Phalcon\Logger\Adapter\File(LOG_PATH . "confirm/" . date('Y-m-d') . ".log");
$this->logger->setFormatter($formatter);
}
public function onConstruct($params = null)
{
if(!isset($this->logger)){
/** gestione dei log **/
if(!is_dir(LOG_PATH . "confirm/"))
if(!mkdir(LOG_PATH . "confirm/",0755))
die("create directory ". LOG_PATH . "confirm/"." failed");
$formatter = new \Phalcon\Logger\Formatter\Line("[".date("Y-m-d H:i:s")."][" . HOSTNAME . "][%type%] %message%");
$this->logger = new \Phalcon\Logger\Adapter\File(LOG_PATH . "confirm/" . date('Y-m-d') . ".log");
$this->logger->setFormatter($formatter);
}
//$this->logger->debug('Avvio Confirm');
}
There is one more crazy thing. This model do a lot of logging and it's used very often. I've a controller that retrieve data between 2 dates and uses (with a relation) also the model with the logger. If I extract data for more than two days I receive this error from the model constructor:
Can't open log file at '/var/www/mysite/log/confirm/2017-12-05.log'<br><pre>
#0 /var/www/mysite/lib/models/Confirm.php(512): Phalcon\Logger\Adapter\File->__construct('/var/www/...')
#1 [internal function]: App\Models\Confirm->onConstruct(NULL)
#2 [internal function]: Phalcon\Mvc\Model->__construct(NULL, Object(Phalcon\Di\FactoryDefault), Object(Phalcon\Mvc\Model\Manager))
At first sight it seems a permission issue, so I got crazy finding what was wrong with them, but everything is ok on the filesystem. If I select a smaller range of dates for the controller it works, so I think it depends on the number of invocations of the model and maybe the system reach the maximum number of files opened. But in that case it should give another error message, isn’t it? And last but not least, if there’s an issue with the number of log files openable… how can i use a logger with big numbers such this case?