How to log something to Zend Developer Tools toolb

2019-02-10 23:29发布

It's posible to log to Zend Developer Tools toolbar some variable in ZF2 controller?

3条回答
看我几分像从前
2楼-- · 2019-02-10 23:40

Well, I have created a Collector to add personal information on ZendDevelopersTool. You need info from a Controller, and that's I never had tried to do. :)

I gonna show you how I created the Collector, and maybe you can find some way to take info from the Controller.

In your MyModule\module.config.php

/* ZendDeveloperTools Configuration */
'invokables' => array(
    'MyModule\ConfigCollector'   => 'MyModule\Collector\ConfigCollector',
),

'view_manager' => array(
    'template_map' => array(
        'zend-developer-tools/toolbar/mymodule-configs' => __DIR__ . '/../view/zend-developer-tools/toolbar/mymodule-configs.phtml',
    ),
),

'zenddevelopertools' => array(
    'profiler' => array(
        'collectors' => array(
            'mymodule_configs' => 'MyModule\ConfigCollector',
        ),
    ),
    'toolbar' => array(
        'entries' => array(
            'mymodule_configs' => 'zend-developer-tools/toolbar/mymodule-configs',
        ),
    ),
),

On MyModule\Module.php, you need to load your invokables, I'm doing this by adding the following method:

public function getServiceConfig()
{
    return include __DIR__ . '/config/module.config.php';
}

Add a .phtml file on the on your views:

MyModule/view/zend-developer-tools/toolbar/mymodule-configs.phtml

And add, for example:

<?php /* @var $collector \DoctrineORMModule\Collector\MappingCollector */ ?>
<div class="zdt-toolbar-entry">
    <div class="zdt-toolbar-preview">
        <img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABoAAAAaCAYAAACpSkzOAAAHMUlEQVRIiYWWW2wU1xnHf2dm71fv2uv1GszaFLDTEIJcMJe0AiJuJYYEBbWkJKKq0qql9Kmq+tCq6kVtQ+OqUVDJW9qXNlKE1DQRAQLiJlqT1OFmOxjWXOwFX9b22rs7s9eZOX1Y45CUku/lHM0c/X/zfeecb/6CLw4b4ANqAD/gmH1eBjRgBsgBxqNExCPeOYF6hGiZ1xTf0hhfuCbesjBeEwp5LSlFenJSHx8bSSZvDV64N3T7qJTyFpACil8E8gJ1s19WUBR16eKly/ev37zl6xaKbyanYVYqlEtVHYfTh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ento">
        <span class="zdt-toolbar-info">
            Configurações Gerais
        </span>
    </div>
    <div class="zdt-toolbar-detail">
        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Environment</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getEnvironment(); ?>
            </span>
        </span>

        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Default Locale</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getDefaultLocale(); ?>
            </span>
        </span>

        <span class="zdt-toolbar-info">
            <span class="zdt-detail-label">Time Zone</span>
            <span class="zdt-detail-value">
                <?php echo $this->collector->getTimeZone(); ?>
            </span>
        </span>

    </div>
</div>

Now, the final part! We need to create a Collector, that gonna grab all teh information we wanna display:

<?php
namespace MyModule\Collector;

use ZendDeveloperTools\Collector;

use Zend\Mvc\MvcEvent;

/**
 * Collector to be used in ZendDeveloperTools to record and display personal information
 *
 * @license MIT
 * @author  Vinicius Garcia <vinigar[...].com>
 */
class ConfigCollector extends \ZendDeveloperTools\Collector\AbstractCollector
{

    /**
     * {@inheritDoc}
     */
    public function getName()
    {
        return 'mymodule_configs';
    }

    /**
     * {@inheritDoc}
     */
    public function getPriority()
    {
        return 10;
    }

    /**
     * {@inheritDoc}
     */
    public function collect(MvcEvent $mvcEvent)
    {
        $date = new \DateTime();

        $this->data = array(
            'environment' => ucfirst(getenv('APPLICATION_ENV') ?: 'production'),
            'timezone' => $date->getTimezone()->getName(),
            'defaultlocale' => \Locale::getDefault(),
        );
    }

    /**
     * Returns the environment
     *
     * @return string
     */
    public function getEnvironment()
    {
        return $this->data['environment'];
    }

    /**
     * Returns the timezone
     *
     * @return string
     */
    public function getTimeZone()
    {
        return $this->data['timezone'];
    }

    /**
     * Returns the default locale
     *
     * @return string
     */
    public function getDefaultLocale()
    {
        return $this->data['defaultlocale'];
    }
}

This Class have methods, like getDefaultLocale(), and we use them on zend-developer-tools/toolbar/mymodule-configs.phtml.

It should look like this:

My Configs on ZendDeveloperTool

You can try to adapt this piece of code, to fit your needs. And please, if you find out how to grab info from your controller, share with us!

Edit

In the ConfigCollector class, pay attention to the method collect(MvcEvent $mvcEvent).

As you can see, you have a MvcEvent object there. Maybe you can pass your personal info using it.

查看更多
家丑人穷心不美
3楼-- · 2019-02-10 23:40

You can also use my package that alow log any data to bar and use Tracy/Debugger to highlight and add tree to your dumped data.

renat-magadiev/zf3-bar-logger

so you can simply write:

<?php

\BarLogger\Collector\DebugCollector::barDump(['testing dump' => ['asd', 'asdad', 'fdgfdsg', 'gfgsd']], 'testing dump');

?>

or simply using global function in shortcuts:

<?php

barDump(['testing dump' => ['asd', 'asdad', 'fdgfdsg', 'gfgsd']], 'testing dump');

?>

preview

查看更多
该账号已被封号
4楼-- · 2019-02-10 23:53

More briefly, it's possible : ) I'm not proud about the way I did it, but - well known excuse - it works. And if you have any suggestions, I'll be not glad but happy to hear them.

How to create a custom collector @vinigarcia87 explained above.

To log something in Controller(and in any other place) and then grab that data in the collector I made a new class in my module with a static field and static getter/setter, which I use as a proxy. Here is some code:

namespace MyModule\Log;

class MyLogger {
    private static $messages = array();

    public static function log($message)
    {
        self::$messages[] = $message;
    }
    public static function getMessages()
    {
        return self::$messages;
    }
}

In a controller's action:

MyModule\Log\Logger::log('Informational message');

And in collector collect method:

public function collect(MvcEvent $mvcEvent)
{
    $this->data = array(
        'messages'     => MyModule\Log\Logger::getMessages(),
    );
}
public function getMessages()
{
    return $this->data['messages'];
}

This messages are now accessible in toolbar entry .pthml file this way: <?php \Zend\Debug\Debug::dump($this->collector->getMessages()); ?>

Your opinions...

查看更多
登录 后发表回答