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

doulu4233
doulu4233 我决定与Session谢谢
大约 2 年之前 回复
douwei7203
douwei7203 好的,因为你正在使用现有的处理程序。就像我说的那样,你不能传递一个额外的参数,因为那会破坏合同。我建议你研究界面,laracasts.com可能有一些很好的视频。
大约 2 年之前 回复
douju1852
douju1852 链接?教程?所以我发布已经可以插入mongo只是想传递一个额外的参数
大约 2 年之前 回复
du6jws6975
du6jws6975 换句话说,您无法自定义Log::debug接收的内容,因为这会破坏合同。
大约 2 年之前 回复
doushai4890
doushai4890 Laravel提供了延长合同的合同。查看其他一个日志记录实现,看看它是如何工作的。我认为您必须实现LoggerInterface才能让您的类使用Logfacade。
大约 2 年之前 回复
dqthn68688
dqthn68688 怎么样?我可以做这个
大约 2 年之前 回复
donglinli2027
donglinli2027 如果要扩展现有日志记录功能,则必须遵循该界面。
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问