adicionado suporte de logs e tratamento de erros no hawk.so

This commit is contained in:
Renan Bernordi 2024-12-19 22:48:52 -03:00
parent 02ec5c80ea
commit 2e554ba6ae
6 changed files with 101 additions and 21 deletions

View file

@ -34,3 +34,6 @@ SELENIUM_HOST=localhost:4444
# Configurações de Debug # Configurações de Debug
DEBUG=true DEBUG=true
# Configurações do Hawk.so
HAWK_TOKEN=

View file

@ -3,7 +3,8 @@
"vlucas/phpdotenv": "^5.6.1", "vlucas/phpdotenv": "^5.6.1",
"aws/aws-sdk-php": "^3.0", "aws/aws-sdk-php": "^3.0",
"php-curl-class/php-curl-class": "^11.0", "php-curl-class/php-curl-class": "^11.0",
"php-webdriver/webdriver": "^1.15" "php-webdriver/webdriver": "^1.15",
"codex-team/hawk.php": "^2.2"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -31,11 +31,18 @@ define('SELENIUM_HOST', isset($_ENV['SELENIUM_HOST']) ? $_ENV['SELENIUM_HOST'] :
define('CACHE_DIR', __DIR__ . '/cache'); define('CACHE_DIR', __DIR__ . '/cache');
define('DEBUG', isset($_ENV['DEBUG']) ? filter_var($_ENV['DEBUG'], FILTER_VALIDATE_BOOLEAN) : false); define('DEBUG', isset($_ENV['DEBUG']) ? filter_var($_ENV['DEBUG'], FILTER_VALIDATE_BOOLEAN) : false);
// Configurações de Redis /**
* Configurações de Redis
*/
define('REDIS_HOST', isset($_ENV['REDIS_HOST']) ? $_ENV['REDIS_HOST'] : 'localhost'); define('REDIS_HOST', isset($_ENV['REDIS_HOST']) ? $_ENV['REDIS_HOST'] : 'localhost');
define('REDIS_PORT', isset($_ENV['REDIS_PORT']) ? $_ENV['REDIS_PORT'] : 6379); define('REDIS_PORT', isset($_ENV['REDIS_PORT']) ? $_ENV['REDIS_PORT'] : 6379);
define('REDIS_PREFIX', isset($_ENV['REDIS_PREFIX']) ? $_ENV['REDIS_PREFIX'] : 'marreta:'); define('REDIS_PREFIX', isset($_ENV['REDIS_PREFIX']) ? $_ENV['REDIS_PREFIX'] : 'marreta:');
/**
* Configurações do Hawk.so
*/
define('HAWK_TOKEN', isset($_ENV['HAWK_TOKEN']) ? $_ENV['HAWK_TOKEN'] : null);
/** /**
* Configurações de Cache S3 * Configurações de Cache S3
*/ */

73
app/inc/Logger.php Normal file
View file

@ -0,0 +1,73 @@
<?php
namespace Inc;
use Exception;
use \Hawk\Catcher;
class Logger
{
private static $instance = null;
private function __construct()
{
// Inicializa o Hawk apenas se houver um token configurado
if (!empty(HAWK_TOKEN)) {
Catcher::init([
'integrationToken' => HAWK_TOKEN,
]);
}
}
public static function getInstance(): Logger
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Registra um erro com contexto
*
* @param string $message Mensagem de erro
* @param array $context Dados adicionais de contexto
* @param string $type Tipo/categoria do erro
*/
public function error(string $message, array $context = [], string $type = 'WARNING'): void
{
// Se não houver token do Hawk configurado, não gera log
if (empty(HAWK_TOKEN)) {
return;
}
// Registra no Hawk
try {
Catcher::get()->sendException(new Exception($message), [
'type' => $type,
'context' => $context
]);
} catch (Exception $e) {
// Se o Hawk falhar, já temos o log em arquivo como backup
error_log("Falha ao enviar erro para o Hawk: " . $e->getMessage());
}
}
/**
* Registra um erro específico de URL
*
* @param string $url A URL que gerou o erro
* @param string $error_group Grupo/categoria do erro
* @param string $message_error Detalhes adicionais do erro
* @param string $type Tipo/categoria do erro
*/
public function log(string $url, string $error_group, string $message_error = '', string $type = 'WARNING'): void
{
$this->error($error_group, [
'url' => $url,
'timestamp' => time(),
'user_agent' => $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown',
'message_error' => $message_error
], $type);
}
}

View file

@ -15,6 +15,7 @@
require_once 'Rules.php'; require_once 'Rules.php';
require_once 'Cache.php'; require_once 'Cache.php';
require_once 'Logger.php';
use Curl\Curl; use Curl\Curl;
use Facebook\WebDriver\Remote\DesiredCapabilities; use Facebook\WebDriver\Remote\DesiredCapabilities;
@ -22,6 +23,7 @@ use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions; use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Firefox\FirefoxProfile; use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Chrome\ChromeOptions; use Facebook\WebDriver\Chrome\ChromeOptions;
use Inc\Logger;
class URLAnalyzer class URLAnalyzer
{ {
@ -88,19 +90,6 @@ class URLAnalyzer
]; ];
} }
/**
* Registra erros no arquivo de log
*
* @param string $url URL que gerou o erro
* @param string $error Mensagem de erro
*/
private function logError($url, $error)
{
$timestamp = date('Y-m-d H:i:s');
$logEntry = "[{$timestamp}] URL: {$url} - Error: {$error}" . PHP_EOL;
file_put_contents(__DIR__ . '/../logs/error.log', $logEntry, FILE_APPEND);
}
/** /**
* Método principal para análise de URLs * Método principal para análise de URLs
* *
@ -126,8 +115,8 @@ class URLAnalyzer
$host = preg_replace('/^www\./', '', $host); $host = preg_replace('/^www\./', '', $host);
if (in_array($host, BLOCKED_DOMAINS)) { if (in_array($host, BLOCKED_DOMAINS)) {
$error = 'Este domínio está bloqueado para extração.'; $error = 'BLOCKED_DOMAINS';
$this->logError($cleanUrl, $error); Logger::getInstance()->log($cleanUrl, $error);
throw new Exception($error); throw new Exception($error);
} }
@ -142,8 +131,9 @@ class URLAnalyzer
return $processedContent; return $processedContent;
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->logError($cleanUrl, "Selenium fetch error: " . $e->getMessage()); $error = 'SELENIUM_ERROR';
throw new Exception("Não foi possível obter o conteúdo via Selenium"); Logger::getInstance()->log($cleanUrl, 'SELENIUM_ERROR', $e->getMessage());
throw new Exception($error);
} }
} }
@ -156,7 +146,7 @@ class URLAnalyzer
return $processedContent; return $processedContent;
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->logError($cleanUrl, "Direct fetch error: " . $e->getMessage()); Logger::getInstance()->log($cleanUrl, 'DIRECT_FETCH_ERROR', $e->getMessage());
} }
// 6. Tenta buscar do Wayback Machine como fallback // 6. Tenta buscar do Wayback Machine como fallback
@ -168,9 +158,10 @@ class URLAnalyzer
return $processedContent; return $processedContent;
} }
} catch (Exception $e) { } catch (Exception $e) {
$this->logError($cleanUrl, "Wayback Machine error: " . $e->getMessage()); Logger::getInstance()->log($cleanUrl, 'WAYBACK_FETCH_ERROR', $e->getMessage());
} }
Logger::getInstance()->log($cleanUrl, 'GENERAL_FETCH_ERROR');
throw new Exception("Não foi possível obter o conteúdo da URL"); throw new Exception("Não foi possível obter o conteúdo da URL");
} }

View file

@ -90,6 +90,11 @@ if [ -n "${SELENIUM_HOST}" ]; then
echo "SELENIUM_HOST=${SELENIUM_HOST}" >> /app/.env echo "SELENIUM_HOST=${SELENIUM_HOST}" >> /app/.env
fi fi
# Configurações do Hawk.so
if [ -n "${HAWK_TOKEN}" ]; then
echo "HAWK_TOKEN=${HAWK_TOKEN}" >> /app/.env
fi
log_success "Variáveis de ambiente configuradas" log_success "Variáveis de ambiente configuradas"
# === Ajuste de Permissões === # === Ajuste de Permissões ===