michaelmoussa/zend-psr-log

View on GitHub
src/LoggerFactory.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
/**
 * Zend PSR Log
 *
 * @link      http://github.com/michaelmoussa/zend-psr-log
 * @license   http://opensource.org/licenses/MIT The MIT License
 */
namespace ZendPsrLog;

use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
 * Factory for creating ZendPsrLog logger instances.
 */
class LoggerFactory implements FactoryInterface
{
    /**
     * The key immediately under "log" in the config to use for this logger.
     *
     * @var string
     */
    protected $configKey;

    /**
     * Constructor
     *
     * @param string $configKey
     */
    public function __construct($configKey = null)
    {
        if ($configKey !== null) {
            $this->configKey = $configKey;
        }
    }

    /**
     * Creates the ZendPsrLog logger.
     *
     * @param ServiceLocatorInterface $serviceLocator
     * @return Logger
     */
    public function createService(ServiceLocatorInterface $serviceLocator)
    {
        $config = $serviceLocator->get('Config');

        if (!empty($this->configKey)) {
            $loggerConfig = isset($config['log'][$this->configKey]) ? $config['log'][$this->configKey] : [];
        } else {
            $loggerConfig = isset($config['log']) ? $config['log'] : [];
        }

        return new Logger($loggerConfig);
    }
}