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