Mani Rai
Mani Rai

Reputation: 705

Zend\Log Cannot Create Log File

I am learning ZF2 and trying to set up a restful web services architecture using ZF2. I am having a problem creating log file.

Here's my module configuration:

'service_manager' => array(
    'factories' => array(
        'loggingService' => function(\Zend\ServiceManager\ServiceManager $serviceManager) {
            $logger = new \Zend\Log\Logger();
            $writer = new \Zend\Log\Writer\Stream('/home/mani/logs/forum.log');
            $logger->addWriter($writer);
            return $logger;
        }
    )
)

Here I created a logger as a service and trying to inject logginService using constructor in controllers.

public function createService(ServiceLocatorInterface $serviceLocator) {

    $coreServiceLocator = $serviceLocator->getServiceLocator();
    $loggingService = $coreServiceLocator->get('loggingService');

    return new QuestionController($loggingService);
}

I think it will work, but ZF2 cannot create a log file showing me this message:

fopen(/home/mani/logs/forum.log): failed to open stream: No such file or directory

So my confusion is can ZF2 create log file or do we have to create it manually?

Upvotes: 1

Views: 997

Answers (2)

Gennadiy Litvinyuk
Gennadiy Litvinyuk

Reputation: 1558

Please check whether /home/mani/logs exists, because in other case it would not be created and the same error will be thrown. Also give 777 to the dorectory.

Upvotes: 0

GianArb
GianArb

Reputation: 1674

In my opinion you have a problem of permission, try to change the path with /tmp/app.log for example.

Thanks

Upvotes: 0

Related Questions