Compare commits

...

6 Commits

@ -7,7 +7,6 @@ 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;
@ -33,10 +32,10 @@ class DiscordHandler extends AbstractProcessingHandler
/** /**
* @param string $webhook * @param string $webhook
* @param int $level * @param mixed $level
* @param bool $bubble * @param bool $bubble
*/ */
public function __construct(string $webhook, \Monolog\Level $level = \Monolog\Level::ERROR, bool $bubble = true) public function __construct(string $webhook, $level = 400, bool $bubble = true)
{ {
$this->webhook = $webhook; $this->webhook = $webhook;
$this->client = new Client(); $this->client = new Client();
@ -45,18 +44,35 @@ class DiscordHandler extends AbstractProcessingHandler
} }
/** /**
* @param LogRecord $record * @param mixed $record
* @return void * @return void
* @throws GuzzleException * @throws GuzzleException
*/ */
protected function write(LogRecord $record): void protected 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
* @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->toArray()); $response = $this->send($record);
} catch (ClientException $exception) { } catch (ClientException $exception) {
$response = $exception->getResponse(); $response = $exception->getResponse();
@ -67,7 +83,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->toArray()); $this->send($record);
} }
$this->rateLimitRemaining = (int)$response->getHeaderLine('X-RateLimit-Remaining'); $this->rateLimitRemaining = (int)$response->getHeaderLine('X-RateLimit-Remaining');

@ -32,7 +32,7 @@ class RabbitMQHandler extends AbstractProcessingHandler
/** /**
* @throws Exception * @throws Exception
*/ */
public function __construct($exchange = 'logs', $routingKey = 'log', $level = \Monolog\Level::ERROR, $bubble = true) public function __construct($exchange = 'logs', $routingKey = 'log', $level = 400, $bubble = true)
{ {
parent::__construct($level, $bubble); parent::__construct($level, $bubble);
@ -54,14 +54,30 @@ class RabbitMQHandler extends AbstractProcessingHandler
} }
/** /**
* @param LogRecord $record * @param mixed $record
* @return void * @return void
*/ */
public function write(LogRecord $record): void public function write($record): void
{ {
$data = json_encode($record->toArray()); 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
*/
protected function recordHandler(array $record)
{
$data = json_encode($record);
$msg = new AMQPMessage($data, [ $msg = new AMQPMessage($data, [
'delivery_mode' => AMQPMessage::DELIVERY_MODE_PERSISTENT 'delivery_mode' => 2
]); ]);
$this->channel->basic_publish($msg, $this->exchange, $this->routingKey); $this->channel->basic_publish($msg, $this->exchange, $this->routingKey);

@ -69,6 +69,8 @@ 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,
@ -147,6 +149,8 @@ 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,4 +15,5 @@ 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