MongoDB
 sql >> Baza danych >  >> NoSQL >> MongoDB

Jak skonfigurować Monolog do przechowywania dzienników w MongoDB za pomocą Symfony2 i Doctrine?

Pełna konfiguracja

mongodb_server: "mongodb://localhost:27017"
mongodb_username: "vagrant"
mongodb_password: "password"
mongodb_database: "testdb"
# Doctrine2 MongoDB Bundle
# http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html
doctrine_mongodb:
    default_database: %mongodb_database%
    connections:
    default:
        server: %mongodb_server%
        options:
            password: %mongodb_password%
            username: %mongodb_username%
            db: %mongodb_database%
            connect: true
    log:
        server: %mongodb_server%
        options:
            password: %mongodb_password%
            username: %mongodb_username%
            db: %mongodb_database%
            connect: true
    document_managers:
    default:
        auto_mapping: true
    log:
        auto_mapping: false
        logging: false
mongolog:
    class: Doctrine\MongoDB\Connection
    factory_service: doctrine_mongodb.odm.log_connection
    factory_method: getMongoClient
monolog:
    handlers:
    main:
        type:   stream
        path:   "%kernel.logs_dir%/%kernel.environment%.log"
        level:  debug
    console:
        type:   console
        bubble: false
        verbosity_levels:
            VERBOSITY_VERBOSE: INFO
            VERBOSITY_VERY_VERBOSE: DEBUG
        channels: ["!doctrine"]
    console_very_verbose:
        type:   console
        bubble: false
        verbosity_levels:
            VERBOSITY_VERBOSE: NOTICE
            VERBOSITY_VERY_VERBOSE: NOTICE
            VERBOSITY_DEBUG: DEBUG
        channels: ["doctrine"]
    mongo:
        type:   mongo
        level:  notice # change as desired
        mongo:
            id: mongolog
            database: %mongodb_database%
            collection: logs
monolog:
    handlers:
    main:
        type:         fingers_crossed
        action_level: error
        handler:      mongo
    nested:
        type:  stream
        path:  "%kernel.logs_dir%/%kernel.environment%.log"
        level: debug
    console:
        type:  console
    mongo:
        type: mongo
        level: notice
        mongo:
            id: mongolog
            database: %mongodb_database%
            collection: logs

Teraz uruchommy powiadomienie PHP i sprawdźmy, czy będzie prawidłowo przechowywane w MongoDB :-)

<?php trigger_error('hello world!', E_USER_NOTICE);

Dodawanie nagłówków żądań HTTP do rekordu Monolog

kernel.listener.exception_listener:
    class: AppBundle\EventListener\ExceptionListener
    arguments:
        - @logger
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }
<?php

namespace AppBundle\EventListener;

use Monolog\Handler\MongoDBHandler;
use Symfony\Bridge\Monolog\Logger;
use Symfony\Component\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;

/**
 * Class ExceptionListener
 * @package AppBundle\EventListener
 * @author Francesco Casula <[email protected]>
 */
class ExceptionListener extends ExceptionHandler
{
    /**
     * @var Logger
     */
    private $logger;

    /**
     * @param Logger $logger
     */
    public function __construct(Logger $logger)
    {
        $this->logger = $logger;
    }

    /**
     * @return Logger
     */
    public function getLogger()
    {
        return $this->logger;
    }

    /**
     * @param GetResponseForExceptionEvent $event
     */
    public function onKernelException(GetResponseForExceptionEvent $event)
    {
        foreach ($this->getLogger()->getHandlers() as $handler) {
            if ($handler instanceof MongoDBHandler) {
                $handler->pushProcessor(function (array $record) use ($event) {
                    $record['extra']['headers'] = $event->getRequest()->headers->all();
                    return $record;
                });

                break;
            }
        }
    }
}



  1. Redis
  2.   
  3. MongoDB
  4.   
  5. Memcached
  6.   
  7. HBase
  8.   
  9. CouchDB
  1. MongoError write EPIPE” w instalacji Parse-Server

  2. Jak wstawić do kolekcji MongoDB z pozycją

  3. Schemat nie został zarejestrowany dla modelu. Mongodb>Mongusta

  4. Jak ustawić klucz podstawowy w MongoDB?

  5. Mongo Map-Reduce To Mimic count(distinct(...)) group by w SQL