Compare commits

..

No commits in common. 'production' and '0.1.8' have entirely different histories.

@ -7,6 +7,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\ClientException; use GuzzleHttp\Exception\ClientException;
use GuzzleHttp\Exception\GuzzleException; use GuzzleHttp\Exception\GuzzleException;
use Monolog\Handler\AbstractProcessingHandler; use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Logger;
use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ResponseInterface;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
use Monolog\LogRecord; use Monolog\LogRecord;
@ -32,10 +33,10 @@ class DiscordHandler extends AbstractProcessingHandler
/** /**
* @param string $webhook * @param string $webhook
* @param mixed $level * @param int $level
* @param bool $bubble * @param bool $bubble
*/ */
public function __construct(string $webhook, $level = 400, bool $bubble = true) public function __construct(string $webhook, \Monolog\Level $level = \Monolog\Level::ERROR, bool $bubble = true)
{ {
$this->webhook = $webhook; $this->webhook = $webhook;
$this->client = new Client(); $this->client = new Client();
@ -44,35 +45,18 @@ class DiscordHandler extends AbstractProcessingHandler
} }
/** /**
* @param mixed $record * @param LogRecord $record
* @return void * @return void
* @throws GuzzleException * @throws GuzzleException
*/ */
protected function write($record): void protected function write(LogRecord $record): void
{
if (is_array($record)) {
// Implementação para Monolog 1.x
$this->recordHandler($record);
}elseif (class_exists(LogRecord::class) && $record instanceof LogRecord) {
// Implementação para Monolog 2.x
$arrayRecord = $record->toArray();
$this->recordHandler($arrayRecord);
}
}
/**
* @param array $record
* @return void
* @throws GuzzleException
*/
protected function recordHandler(array $record)
{ {
if ($this->rateLimitRemaining === 0 && $this->rateLimitReset !== null) { if ($this->rateLimitRemaining === 0 && $this->rateLimitReset !== null) {
$this->waitUntil($this->rateLimitReset); $this->waitUntil($this->rateLimitReset);
} }
try { try {
$response = $this->send($record); $response = $this->send($record->toArray());
} catch (ClientException $exception) { } catch (ClientException $exception) {
$response = $exception->getResponse(); $response = $exception->getResponse();
@ -83,7 +67,7 @@ class DiscordHandler extends AbstractProcessingHandler
$retryAfter = $response->getHeaderLine('Retry-After'); $retryAfter = $response->getHeaderLine('Retry-After');
$this->wait((int)$retryAfter); $this->wait((int)$retryAfter);
$this->send($record); $this->send($record->toArray());
} }
$this->rateLimitRemaining = (int)$response->getHeaderLine('X-RateLimit-Remaining'); $this->rateLimitRemaining = (int)$response->getHeaderLine('X-RateLimit-Remaining');

@ -4,7 +4,6 @@ namespace Ae3\LaravelLogsLayer\app\Handlers;
use Exception; use Exception;
use Monolog\Handler\AbstractProcessingHandler; use Monolog\Handler\AbstractProcessingHandler;
use Monolog\LogRecord;
use PhpAmqpLib\Channel\AbstractChannel; use PhpAmqpLib\Channel\AbstractChannel;
use PhpAmqpLib\Channel\AMQPChannel; use PhpAmqpLib\Channel\AMQPChannel;
use PhpAmqpLib\Connection\AMQPStreamConnection; use PhpAmqpLib\Connection\AMQPStreamConnection;
@ -32,7 +31,7 @@ class RabbitMQHandler extends AbstractProcessingHandler
/** /**
* @throws Exception * @throws Exception
*/ */
public function __construct($exchange = 'logs', $routingKey = 'log', $level = 400, $bubble = true) public function __construct($exchange = 'logs', $routingKey = 'log', $level = \Monolog\Level::ERROR, $bubble = true)
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
@ -47,37 +46,17 @@ class RabbitMQHandler extends AbstractProcessingHandler
$this->exchange = $exchange; $this->exchange = $exchange;
$this->routingKey = $routingKey; $this->routingKey = $routingKey;
$this->channel->exchange_declare($exchange, 'direct', false, true, false); $this->channel->exchange_declare($exchange, 'direct', false, true, false);
$queueName = config('logging.channels.rabbitmq.queue', 'logstash_queue');
$this->channel->queue_declare($queueName, false, true, false, false);
$this->channel->queue_bind($queueName, $this->exchange, $this->routingKey);
}
/**
* @param mixed $record
* @return void
*/
public function write($record): void
{
if (is_array($record)) {
// Implementação para Monolog 1.x
$this->recordHandler($record);
}elseif (class_exists(LogRecord::class) && $record instanceof LogRecord) {
// Implementação para Monolog 2.x
$arrayRecord = $record->toArray();
$this->recordHandler($arrayRecord);
}
} }
/** /**
* @param array $record * @param array $record
* @return void * @return void
*/ */
protected function recordHandler(array $record) public function write(array $record): void
{ {
$data = json_encode($record); $data = json_encode($record);
$msg = new AMQPMessage($data, [ $msg = new AMQPMessage($data, [
'delivery_mode' => 2 'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT
]); ]);
$this->channel->basic_publish($msg, $this->exchange, $this->routingKey); $this->channel->basic_publish($msg, $this->exchange, $this->routingKey);

@ -1,37 +0,0 @@
<?php
namespace Ae3\LaravelLogsLayer\app\Services;
use Ae3\LaravelLogsLayer\app\DataTransferObjects\ExceptionContextDTO;
use Throwable;
class RabbitMQLogService extends AbstractLogService
{
/**
* @inheritDoc
*/
protected function getLogChannel(): string
{
return 'rabbitmq';
}
/**
* @inheritDoc
*/
protected function buildLogData(string $caller, Throwable $exception, ExceptionContextDTO $contextDto): array
{
return [
'caller' => $caller,
'status_code' => $exception->getCode(),
'line' => $exception->getLine(),
'file' => $exception->getFile(),
'error_code' => $contextDto->code,
'custom_data' => $this->asPrettyJson($contextDto->custom_data),
'tags' => $contextDto->tags,
'exception' => get_class($exception),
'current_url' => $contextDto->current_url,
'current_user' => $this->asPrettyJson($contextDto->current_user),
'classes' => $this->asPrettyJson($this->getContextClasses($exception->getTrace(), $contextDto->root_namespace)),
];
}
}

@ -10,7 +10,6 @@ use Ae3\LaravelLogsLayer\app\Services\DailyLogService;
use Ae3\LaravelLogsLayer\app\Services\DiscordLogService; use Ae3\LaravelLogsLayer\app\Services\DiscordLogService;
use Ae3\LaravelLogsLayer\app\Services\EmailLogService; use Ae3\LaravelLogsLayer\app\Services\EmailLogService;
use Ae3\LaravelLogsLayer\app\Services\LogstashLogService; use Ae3\LaravelLogsLayer\app\Services\LogstashLogService;
use Ae3\LaravelLogsLayer\app\Services\RabbitMQLogService;
use Hashids\Hashids; use Hashids\Hashids;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Throwable; use Throwable;
@ -53,10 +52,6 @@ trait LogTrait
if ($defaultChannel === 'email' || in_array('email', $stackChannels, true)) { if ($defaultChannel === 'email' || in_array('email', $stackChannels, true)) {
$this->logServices[] = app(EmailLogService::class); $this->logServices[] = app(EmailLogService::class);
} }
if ($defaultChannel === 'rabbitmq' || in_array('rabbitmq', $stackChannels, true)) {
$this->logServices[] = app(RabbitMQLogService::class);
}
} }
/** /**
@ -69,8 +64,6 @@ trait LogTrait
{ {
$this->initializeLogServices(); $this->initializeLogServices();
$customData['server_ip'] = config('laravel-logs-layer.server_ip');
foreach ($this->logServices as $logService) { foreach ($this->logServices as $logService) {
$logService->$method($message, [ $logService->$method($message, [
'custom_data' => $customData, 'custom_data' => $customData,
@ -149,8 +142,6 @@ trait LogTrait
$this->initializeLogServices(); $this->initializeLogServices();
$customData['server_ip'] = config('laravel-logs-layer.server_ip');
foreach ($this->logServices as $logService) { foreach ($this->logServices as $logService) {
$logService->$log_level($caller, $exception, ExceptionContextDTO::fromArray([ $logService->$log_level($caller, $exception, ExceptionContextDTO::fromArray([
'code' => $errorCode, 'code' => $errorCode,

@ -15,5 +15,4 @@ return [
'backoff' => explode(',', env('LOG_QUEUE_BACKOFF', '15')) 'backoff' => explode(',', env('LOG_QUEUE_BACKOFF', '15'))
], ],
'sensitive_data' => env('LOGS_LAYER_SENSITIVE_DATA', 'password,password_confirmation,token,api_token,api_key,access_token,refresh_token,authorization_code,client_secret'), 'sensitive_data' => env('LOGS_LAYER_SENSITIVE_DATA', 'password,password_confirmation,token,api_token,api_key,access_token,refresh_token,authorization_code,client_secret'),
'server_ip' => env('SERVER_IP', '127.0.0.1')
]; ];

Loading…
Cancel
Save