Logger Adapter and can't open log message

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?

Im guessing this is about that in one request file is being saved and operation is not ended yet and in another request again save happens.



2.4k

it would, but if it's like what you are saying, logger adapter inside heavy usage methods should often give back these kind of errors. I mean.. my controller runs a cicle on this model collection insidea several hundreds elements foreach. not so huge, it's a report.

how can i use a logger with big numbers such this case?

You simply avoid using filesystem for logs (i.e. use another Logger adapter, such as Memcached or send it to a different server with higher IOPS). I/O is slowest operation you can imagine in entire app lifecycle.



2.4k
edited 2d ago

how can i use a logger with big numbers such this case?

You simply avoid using filesystem for logs (i.e. use another Logger adapter, such as Memcached or send it to a different server with higher IOPS). I/O is slowest operation you can imagine in entire app lifecycle.

Of course Memcached or DB are faster. No doubt about it. But I think that system logs should not give such problems in large model collections. I mean that writing a system log is quite normal, what it's not normal is that the adapter opens a lot of file without closing them. The right behaviour should be to open and close the log file only in the writing methods like info() or error(). In my case I receive this error without writing any line of log, just for the initialization inside every single object of the find result collection.