Skip to content

Commit

Permalink
adicionado suporte de logs e tratamento de erros no hawk.so
Browse files Browse the repository at this point in the history
  • Loading branch information
altendorfme committed Dec 20, 2024
1 parent 02ec5c8 commit 2e554ba
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 21 deletions.
3 changes: 3 additions & 0 deletions app/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,6 @@ SELENIUM_HOST=localhost:4444

# Configurações de Debug
DEBUG=true

# Configurações do Hawk.so
HAWK_TOKEN=
3 changes: 2 additions & 1 deletion app/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"vlucas/phpdotenv": "^5.6.1",
"aws/aws-sdk-php": "^3.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": {
"psr-4": {
Expand Down
9 changes: 8 additions & 1 deletion app/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@
define('CACHE_DIR', __DIR__ . '/cache');
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_PORT', isset($_ENV['REDIS_PORT']) ? $_ENV['REDIS_PORT'] : 6379);
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
*/
Expand Down
73 changes: 73 additions & 0 deletions app/inc/Logger.php
Original file line number Diff line number Diff line change
@@ -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);
}
}
29 changes: 10 additions & 19 deletions app/inc/URLAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@

require_once 'Rules.php';
require_once 'Cache.php';
require_once 'Logger.php';

use Curl\Curl;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;
use Facebook\WebDriver\Firefox\FirefoxOptions;
use Facebook\WebDriver\Firefox\FirefoxProfile;
use Facebook\WebDriver\Chrome\ChromeOptions;
use Inc\Logger;

class URLAnalyzer
{
Expand Down Expand Up @@ -88,19 +90,6 @@ public function checkRedirects($url)
];
}

/**
* 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
*
Expand All @@ -126,8 +115,8 @@ public function analyze($url)
$host = preg_replace('/^www\./', '', $host);

if (in_array($host, BLOCKED_DOMAINS)) {
$error = 'Este domínio está bloqueado para extração.';
$this->logError($cleanUrl, $error);
$error = 'BLOCKED_DOMAINS';
Logger::getInstance()->log($cleanUrl, $error);
throw new Exception($error);
}

Expand All @@ -142,8 +131,9 @@ public function analyze($url)
return $processedContent;
}
} catch (Exception $e) {
$this->logError($cleanUrl, "Selenium fetch error: " . $e->getMessage());
throw new Exception("Não foi possível obter o conteúdo via Selenium");
$error = 'SELENIUM_ERROR';
Logger::getInstance()->log($cleanUrl, 'SELENIUM_ERROR', $e->getMessage());
throw new Exception($error);
}
}

Expand All @@ -156,7 +146,7 @@ public function analyze($url)
return $processedContent;
}
} 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
Expand All @@ -168,9 +158,10 @@ public function analyze($url)
return $processedContent;
}
} 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");
}

Expand Down
5 changes: 5 additions & 0 deletions docker-entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ if [ -n "${SELENIUM_HOST}" ]; then
echo "SELENIUM_HOST=${SELENIUM_HOST}" >> /app/.env
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"

# === Ajuste de Permissões ===
Expand Down

0 comments on commit 2e554ba

Please sign in to comment.