| 1 |
<?php |
| 2 |
|
| 3 |
namespace SuperFrete_API\Helpers; |
| 4 |
|
| 5 |
if (!defined('ABSPATH')) { |
| 6 |
exit; // Segurança para evitar acesso direto |
| 7 |
} |
| 8 |
|
| 9 |
class Logger { |
| 10 |
|
| 11 |
private static $log_file; |
| 12 |
|
| 13 |
/** |
| 14 |
* Inicializa a classe e define o caminho do log. |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
if (!self::$log_file) { |
| 18 |
$upload_dir = wp_upload_dir(); |
| 19 |
self::$log_file = trailingslashit($upload_dir['basedir']) . 'superfrete.log'; |
| 20 |
|
| 21 |
// Garante que a pasta de logs exista |
| 22 |
$log_dir = dirname(self::$log_file); |
| 23 |
|
| 24 |
if (!file_exists($log_dir)) { |
| 25 |
mkdir($log_dir, 0755, true); |
| 26 |
} |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
/** |
| 31 |
* Verifica se o modo debug está ativado. |
| 32 |
* Respeita WP_DEBUG ou a opção superfrete_debug_mode. |
| 33 |
* |
| 34 |
* @return bool |
| 35 |
*/ |
| 36 |
public static function is_debug_enabled() { |
| 37 |
// Permite ativar via opção do WordPress (para controle independente) |
| 38 |
$debug_option = get_option('superfrete_debug_mode', 'auto'); |
| 39 |
|
| 40 |
if ($debug_option === 'enabled') { |
| 41 |
return true; |
| 42 |
} |
| 43 |
|
| 44 |
if ($debug_option === 'disabled') { |
| 45 |
return false; |
| 46 |
} |
| 47 |
|
| 48 |
// Modo 'auto': respeita WP_DEBUG |
| 49 |
return defined('WP_DEBUG') && WP_DEBUG; |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Registra uma mensagem no log. |
| 54 |
* |
| 55 |
* @param string|array $message Mensagem a ser registrada (pode ser string ou array para JSON). |
| 56 |
* @param string $level Nível do log (DEBUG, INFO, WARNING, ERROR). |
| 57 |
*/ |
| 58 |
public static function log($message, $level = 'ERROR') { |
| 59 |
// DEBUG logs só são registrados se debug estiver habilitado |
| 60 |
if (strtoupper($level) === 'DEBUG' && !self::is_debug_enabled()) { |
| 61 |
return; |
| 62 |
} |
| 63 |
|
| 64 |
self::init(); |
| 65 |
|
| 66 |
// Se for um array (como uma resposta da API), converte para JSON formatado |
| 67 |
if (is_array($message)) { |
| 68 |
$message = wp_json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE); |
| 69 |
} else { |
| 70 |
// Remove caracteres Unicode escapados de strings normais |
| 71 |
$message = json_decode('"' . $message . '"'); |
| 72 |
} |
| 73 |
|
| 74 |
$log_entry = sprintf("[%s] [%s] %s\n", date("Y-m-d H:i:s"), strtoupper($level), $message); |
| 75 |
file_put_contents(self::$log_file, $log_entry, FILE_APPEND); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Registra uma mensagem de debug. |
| 80 |
* Só será registrada se WP_DEBUG estiver ativo ou superfrete_debug_mode = 'enabled'. |
| 81 |
* |
| 82 |
* @param string|array $message Mensagem a ser registrada. |
| 83 |
* @param string $context Contexto opcional para prefixar a mensagem. |
| 84 |
*/ |
| 85 |
public static function debug($message, $context = '') { |
| 86 |
if ($context) { |
| 87 |
$message = "[$context] $message"; |
| 88 |
} |
| 89 |
self::log($message, 'DEBUG'); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Obtém o conteúdo do log. |
| 94 |
* |
| 95 |
* @return string Conteúdo do log ou mensagem de erro se não existir. |
| 96 |
*/ |
| 97 |
public static function get_log() { |
| 98 |
self::init(); |
| 99 |
return file_exists(self::$log_file) ? file_get_contents(self::$log_file) : 'Nenhum log encontrado.'; |
| 100 |
} |
| 101 |
|
| 102 |
/** |
| 103 |
* Limpa o log. |
| 104 |
*/ |
| 105 |
public static function clear_log() { |
| 106 |
self::init(); |
| 107 |
file_put_contents(self::$log_file, ''); |
| 108 |
} |
| 109 |
} |