laravel 5.6中的自定义日志记录

时间:2018-07-17 17:38:03

标签: php laravel logging customization

我遇到以下问题,我需要为我的应用程序创建一个自定义记录器,代码如下:

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;

还有自定义记录器

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

我希望以下内容将附加参数传递给mongologger,该参数将是我将在其中插入数据的集合;

例如

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

但是我无法传递此参数,我该怎么做?

感谢您的帮助

0 个答案:

没有答案