<commit_before><?php
namespace NewRelic;

use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use NewRelic\Service\ManagerFactory;

class Module implements
    ConfigProviderInterface,
    ServiceProviderInterface,
    AutoloaderProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'NewRelicManager' => new ManagerFactory,
                'NewRelicLogWriter' => function($sm) {
                    $writer = new \NewRelic\Log\Writer\NewRelic;

                    return $writer;
                },
                'logger' => function($sm) {
                    $logger = new \Zend\Log\Logger();

                    $writer = $sm->get('NewRelicLogWriter');
                    $logger->addWriter($writer);

                    return $logger;
                },
            ),
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function onBootstrap(MvcEvent $e)
    {
        $application = $e->getApplication();

        /* @var $manager \NewRelic\Manager */
        $manager = $application->getServiceManager()->get('NewRelicManager');

        if (!$manager->extensionLoaded()) {
            return;
        }

        $applicationName = $manager->getApplicationName();
        if ($applicationName) {
            $params = array($applicationName);

            $applicationLicense = $manager->getApplicationLicense();
            if ($applicationLicense) {
                $params['license'] = $applicationLicense;
            }
            
            call_user_func_array('newrelic_set_appname', $params);
        }

        if ($manager->getBrowserTimingEnabled()) {
            ini_set(
                'newrelic.browser_monitoring.auto_instrument',
                $manager->getBrowserTimingAutoInstrument()
            );

            if (!$manager->getBrowserTimingAutoInstrument()) {
                $application->getEventManager()->attach('finish', array($this, 'addBrowserTiming'), 100);
            }
        }
    }


    public function addBrowserTiming($e)
    {
        $response = $e->getResponse();
        $content = $response->getBody();

        $browserTimingHeader = newrelic_get_browser_timing_header();
        $browserTimingFooter = newrelic_get_browser_timing_footer();

        $content = str_replace('<head>', '<head>' . $browserTimingHeader, $content);
        $content = str_replace('</body>', $browserTimingFooter . '</body>', $content);

        $response->setContent($content);
    }
}<commit_msg>Bug fix in log writer<commit_after><?php
namespace NewRelic;

use Zend\Mvc\MvcEvent;
use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\ModuleManager\Feature\ConfigProviderInterface;
use Zend\ModuleManager\Feature\ServiceProviderInterface;
use NewRelic\Service\ManagerFactory;

class Module implements
    ConfigProviderInterface,
    ServiceProviderInterface,
    AutoloaderProviderInterface
{
    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function getServiceConfig()
    {
        return array(
            'factories' => array(
                'NewRelicManager' => new ManagerFactory,
                'NewRelicLogWriter' => function($sm) {
                    $writer = new \NewRelic\Log\Writer\NewRelic;

                    $manager = $sm->get('NewRelicManager');
                    $writer->setManager($manager);

                    return $writer;
                },
                'logger' => function($sm) {
                    $logger = new \Zend\Log\Logger();

                    $writer = $sm->get('NewRelicLogWriter');
                    $logger->addWriter($writer);

                    return $logger;
                },
            ),
        );
    }

    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
                    __NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
                ),
            ),
        );
    }

    public function onBootstrap(MvcEvent $e)
    {
        $application = $e->getApplication();

        /* @var $manager \NewRelic\Manager */
        $manager = $application->getServiceManager()->get('NewRelicManager');

        if (!$manager->extensionLoaded()) {
            return;
        }

        $applicationName = $manager->getApplicationName();
        if ($applicationName) {
            $params = array($applicationName);

            $applicationLicense = $manager->getApplicationLicense();
            if ($applicationLicense) {
                $params['license'] = $applicationLicense;
            }
            
            call_user_func_array('newrelic_set_appname', $params);
        }

        if ($manager->getBrowserTimingEnabled()) {
            ini_set(
                'newrelic.browser_monitoring.auto_instrument',
                $manager->getBrowserTimingAutoInstrument()
            );

            if (!$manager->getBrowserTimingAutoInstrument()) {
                $application->getEventManager()->attach('finish', array($this, 'addBrowserTiming'), 100);
            }
        }
    }


    public function addBrowserTiming($e)
    {
        $response = $e->getResponse();
        $content = $response->getBody();

        $browserTimingHeader = newrelic_get_browser_timing_header();
        $browserTimingFooter = newrelic_get_browser_timing_footer();

        $content = str_replace('<head>', '<head>' . $browserTimingHeader, $content);
        $content = str_replace('</body>', $browserTimingFooter . '</body>', $content);

        $response->setContent($content);
    }
}
{'fraction_non_alphanumeric': 0.11763786908295579, 'fraction_numerical': 0.0009373535385096079, 'mean_word_length': 1.816542014958205, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1583357', 'n_tokens_mistral': 1727, 'n_tokens_neox': 1618, 'n_words': 261}