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 用windows做服务的同志有吗
    • ¥60 求一个简单的网页(标签-安全|关键词-上传)
    • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图