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

log as service

i already post this problem but I click on "delete discussion".

The problem is to set the logger as service but with the possibility to use the $dispatcer (getController and getAction) referenced to the class where I get the service log.

Thank you



6.5k
Accepted
answer

This work :)

$di->set("logger", function () use ($config, $di)
        {
            $router = $di->get('router');
            $controller = $router->getControllerName();
            $action = $router->getActionName();
            $logger = new FileAdapter($config->application->logPath);
            $formatter = new LineFormatter("[%date%][Controller: ".$controller."->Action: ".$action."][%type%]{%message%}");
            $logger->setFormatter($formatter);
            return $logger;
        });