-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adicionado suporte de logs e tratamento de erros no hawk.so
- Loading branch information
1 parent
02ec5c8
commit 2e554ba
Showing
6 changed files
with
101 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,3 +34,6 @@ SELENIUM_HOST=localhost:4444 | |
|
||
# Configurações de Debug | ||
DEBUG=true | ||
|
||
# Configurações do Hawk.so | ||
HAWK_TOKEN= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters