如何使用Symfony2和Doctrine配置独白以将logs存储到MongoDB中



是否可以使用Symfony 2.6和Doctrine 2?

完整配置

/app/parameters.yml

mongodb_server: "mongodb://localhost:27017"
mongodb_username: "vagrant"
mongodb_password: "password"
mongodb_database: "testdb"

/app/config.yml

# 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

/app/services.yml

mongolog:
    class: DoctrineMongoDBConnection
    factory_service: doctrine_mongodb.odm.log_connection
    factory_method: getMongoClient

/app/config_dev.yml

在此示例中,我决定将所有内容(debug级别)一如既往地存储在dev.log中,只是在Mongo上的错误,警告和通知。

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

/app/config_prod.yml

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

现在,让我们触发PHP通知,然后检查是否正确存储在MongoDB上: - )

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

将HTTP请求标头添加到单一记录

/app/services.yml

kernel.listener.exception_listener:
    class: AppBundleEventListenerExceptionListener
    arguments:
        - @logger
    tags:
        - { name: kernel.event_listener, event: kernel.exception, method: onKernelException }

AppBundleEventListenerExceptionListener

<?php
namespace AppBundleEventListener;
use MonologHandlerMongoDBHandler;
use SymfonyBridgeMonologLogger;
use SymfonyComponentDebugExceptionHandler;
use SymfonyComponentHttpKernelEventGetResponseForExceptionEvent;
/**
 * Class ExceptionListener
 * @package AppBundleEventListener
 * @author Francesco Casula <fra.casula@gmail.com>
 */
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;
            }
        }
    }
}

相关内容

  • 没有找到相关文章

最新更新