如何配置 Monolog 以使用 Symfony2 和 Doctrine 将日志存储到 MongoDB 中 [英] How to configure Monolog to store logs into MongoDB with Symfony2 and Doctrine

查看:39
本文介绍了如何配置 Monolog 以使用 Symfony2 和 Doctrine 将日志存储到 MongoDB 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以获得一个完整的示例,说明如何使用 Symfony 2.6 和 Doctrine 2 配置 Monolog 以将其日志存储到 MongoDB 中?

解决方案

完整配置

<块引用>

/app/parameters.yml

mongodb_server: "mongodb://localhost:27017"mongodb_username: "流浪者"mongodb_password: "密码"mongodb_database: "testdb"

<块引用>

/app/config.yml

# Doctrine2 MongoDB Bundle# http://symfony.com/doc/current/bundles/DoctrineMongoDBBundle/index.html学说_mongodb:默认数据库:%mongodb_database%连接:默认:服务器:%mongodb_server%选项:密码:%mongodb_password%用户名:%mongodb_username%数据库:%mongodb_database%连接:真日志:服务器:%mongodb_server%选项:密码:%mongodb_password%用户名:%mongodb_username%数据库:%mongodb_database%连接:真文档管理器:默认:自动映射:真日志:自动映射:假记录:假

<块引用>

/app/services.yml

mongolog:类:DoctrineMongoDBConnection工厂服务:doctrine_mongodb.odm.log_connectionfactory_method:getMongoClient

<块引用>

/app/config_dev.yml

在这个例子中,我决定像往常一样将所有内容(debug 级别)存储到 dev.log 中,并且只存储 mongo 上的错误、警告和通知.

 独白:处理程序:主要的:类型:流路径:%kernel.logs_dir%/%kernel.environment%.log"级别:调试安慰:类型:控制台泡沫:假详细级别:VERBOSITY_VERBOSE:信息VERBOSITY_VERY_VERBOSE:调试频道:[!教义"]console_very_verbose:类型:控制台泡沫:假详细级别:VERBOSITY_VERBOSE:注意VERBOSITY_VERY_VERBOSE:注意VERBOSITY_DEBUG:调试频道:[教义"]蒙戈:类型:蒙戈级别:注意 # 根据需要更改蒙戈:编号:mongolog数据库:%mongodb_database%收集:日志

<块引用>

/app/config_prod.yml

 独白:处理程序:主要的:类型:finger_crossedaction_level: 错误处理程序:mongo嵌套:类型:流路径:%kernel.logs_dir%/%kernel.environment%.log"级别:调试安慰:类型:控制台蒙戈:类型:蒙戈级别:通知蒙戈:编号:mongolog数据库:%mongodb_database%收集:日志

现在让我们触发一个 PHP 通知并检查它是否会正确存储在 MongoDB 上:-)

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

向 Monolog 记录添加 HTTP 请求头

<块引用>

/app/services.yml

kernel.listener.exception_listener:类:AppBundleEventListenerExceptionListener论据:- @logger标签:- { 名称:kernel.event_listener,事件:kernel.exception,方法:onKernelException }

<块引用>

AppBundleEventListenerExceptionListener

*/类 ExceptionListener 扩展了 ExceptionHandler{/*** @var 记录器*/私人 $logger;/*** @param 记录器 $logger*/公共函数 __construct(Logger $logger){$this->logger = $logger;}/*** @return 记录器*/公共函数 getLogger(){返回 $this->logger;}/*** @param GetResponseForExceptionEvent $event*/公共函数 onKernelException(GetResponseForExceptionEvent $event){foreach ($this->getLogger()->getHandlers() 作为 $handler) {if ($handler instanceof MongoDBHandler) {$handler->pushProcessor(function (array $record) use ($event) {$record['extra']['headers'] = $event->getRequest()->headers->all();返回$记录;});休息;}}}}

Would it be possible to get a full example of how is it possible to configure Monolog to store its logs into MongoDB using Symfony 2.6 and Doctrine 2?

解决方案

Full configuration

/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

In this example I decided to store everything (debug level) as always into the dev.log and just errors, warnings and notices on 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

Now let's trigger a PHP notice and check if it'll be stored on MongoDB properly :-)

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

Adding HTTP request headers to Monolog record

/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;
            }
        }
    }
}

这篇关于如何配置 Monolog 以使用 Symfony2 和 Doctrine 将日志存储到 MongoDB 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆