dongshukou0240 2018-07-17 17:38
浏览 136

laravel 5.6中的自定义日志记录

I am having the following problem, I need to create a custom logger for my application, the code is as follows:

config/logging.php

    'channels' => [
    'stack' => [
        'driver' => 'stack',
        'channels' => ['slack', 'logentries', 'custom'],
    ],
    'logentries' => [
        'driver'  => 'monolog',
        'level' => 'info',
        'handler' => Monolog\Handler\SyslogUdpHandler::class,
        'handler_with' => [
            'host' => 'us.data.logs.insight.rapid7.com',
            'port' => '14211',
        ],
    ],
    'custom' => [
        'driver' => 'custom',
        'via' => App\Logging\MongoLogger::class,
        'level' => 'debug',
        'formatter' => 'default',
    ],
    'single' => [
        'driver' => 'single',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
    ],
    'daily' => [
        'driver' => 'daily',
        'path' => storage_path('logs/laravel.log'),
        'level' => 'debug',
        'days' => 7,
    ],
    'slack' => [
        'driver' => 'slack',
        'url' => env('LOG_SLACK_WEBHOOK_URL'),
        'username' => 'Laravel Log',
        'emoji' => ':boom:',
        'level' => 'critical',
    ],
    'syslog' => [
        'driver' => 'syslog',
        'level' => 'debug',
    ],
    'errorlog' => [
        'driver' => 'errorlog',
        'level' => 'debug',
    ],
],

AfterMiddleWare.php

        $record = array(
        'tipo'           => 'debug',
        'url'            => Request::url(),
        'message'        => (string)'acesso_sistema',
        'datahora'       => \Carbon\Carbon::now(),
        'session_id'     => Session::getId(),
        'level'          => 'access_log',
        'so'             => BrowserDetect::platformName(),
        'mobile'         => BrowserDetect::isMobile(),
        'tempo_execucao' => (!empty($context['tempo_execucao'])) ? $context['tempo_execucao'] : 0,
        'dia_mes_ano'    => date('d/m/Y'),
        'usuario'        => $this->getUsuario(),
        'cliente'        => Session::get('cliente'),
        'ip_origem'      => Request::ip()
    );

    if(empty($record['datetime'])) {
        $record['datetime'] = $ts;
    }

    Log::debug(json_encode($record));

    return $response;

And the custom logger

MongoLogger.php

<?php

namespace App\Logging;

use Monolog\Handler\MongoDBHandler;
use MongoDB\Client;
use Monolog\Logger;


class MongoLogger
{

    /**
     * Customize the given logger instance.
     *
     * @param  \Illuminate\Log\Logger  $logger
     * @return void
     */
    public function __invoke($config)
    {
        die(print_r($config));
        $log = new Logger('debug');
        try {
            $log->pushHandler(new MongoDBHandler(new Client('mongodb://' . config('database.connections.mongodb.host') . ':' . config('database.connections.mongodb.port')), config('database.connections.mongodb.database'), 'logs', 'DEBUG'));

        } catch (\MongoConnectionException $e) {

            throw new \Exception('Cannot connect to Mongo - please check your server');

        }
        return $log;
    }
}

I want the following to pass an additional parameter to the mongologger which would be the collection where I will insert the data;

For example

Log::debug(json_encode($record) , 'MYCOLLECTION'`

but I'm not able to pass this parameter, how can I do this?

thanks for helping

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 我想在一个软件里添加一个优惠弹窗,应该怎么写代码
    • ¥15 fluent的在模拟压强时使用希望得到一些建议
    • ¥15 STM32驱动继电器
    • ¥15 Windows server update services
    • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
    • ¥15 模糊pid与pid仿真结果几乎一样
    • ¥15 java的GUI的运用
    • ¥15 Web.config连不上数据库
    • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
    • ¥15 怎么配置广告联盟瀑布流