PluginProbe
SuperFrete / 2.1.5
SuperFrete v2.1.5
trunk 1.1.0 1.1.1 1.1.2 1.1.3 1.1.4 2.0.0 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 3.2.1 3.3.0 3.3.1 3.3.2 3.3.3 3.3.4
superfrete / api / Helpers / Logger.php

Logger.php in SuperFrete 2.1.5, at api/Helpers/Logger.php

69 lines 2.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 * Registra uma mensagem no log.
32 *
33 * @param string|array $message Mensagem a ser registrada (pode ser string ou array para JSON).
34 * @param string $level Nível do log (INFO, WARNING, ERROR).
35 */
36 public static function log($message, $level = 'ERROR') {
37 self::init();
38
39 // Se for um array (como uma resposta da API), converte para JSON formatado
40 if (is_array($message)) {
41 $message = wp_json_encode($message, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE);
42 } else {
43 // Remove caracteres Unicode escapados de strings normais
44 $message = json_decode('"' . $message . '"');
45 }
46
47 $log_entry = sprintf("[%s] [%s] %s\n", date("Y-m-d H:i:s"), strtoupper($level), $message);
48 file_put_contents(self::$log_file, $log_entry, FILE_APPEND);
49
50 }
51
52 /**
53 * Obtém o conteúdo do log.
54 *
55 * @return string Conteúdo do log ou mensagem de erro se não existir.
56 */
57 public static function get_log() {
58 self::init();
59 return file_exists(self::$log_file) ? file_get_contents(self::$log_file) : 'Nenhum log encontrado.';
60 }
61
62 /**
63 * Limpa o log.
64 */
65 public static function clear_log() {
66 self::init();
67 file_put_contents(self::$log_file, '');
68 }
69 }