We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

about

I have a question, use Phalcon\Logger\Adapter\File , the log increase to a quantity, whether it will generate another log file? i don't try. thanks



7.9k

it will append to end of file but you can rotate log file each hour/day/month etc



3.4k
Accepted
answer
edited Nov '14

You can use a logic like this

<?php
        $loggerPath = "../app/logs/sql.log";

        // size greater than 3 MB
        if (filesize($loggerPath) > 3000000) {

            // increment older log files
            for ($i = 10; $i > 0; $i--) {

                if (file_exists('../app/logs/sql.' . $i . '.log')) {
                    rename('../app/logs/sql.' . $i . '.log', '../app/logs/sql.' . ($i + 1) . '.log');
                }
            }

            // delete eleventh file if it exists
            if (file_exists('../app/logs/sql.11.log')) {
                unlink('../app/logs/sql.11.log');
            }

            rename($loggerPath, '../app/logs/sql.1.log');
        }

        $logger = new FileLogger("../app/logs/sql.log", array('mode' => 'a'));

This is only a quick test implementation ;-) It will keep 10 files with max 3 MB size.

If this is on a Linux system, the proper way to rotate the logs would be to use the log rotate daemon.

I thought Phalcon\Logger\Adapter\File own mechanism . do not written the logic of file size.. thanks



3.4k

@YangHuiFor - Do you still need something like this? I'm actually developing a logger that does this (ie: emulates log rotate daemon) with some colour coding so you can easily tail -f and identify errors much easier.

I'll be releasing it on GitHub soon, but will announce it on the forums anyways.