Created
August 29, 2021 09:59
-
-
Save vmrfriz/1fc8a56d539cb05ec06ed35ac5ce2b45 to your computer and use it in GitHub Desktop.
PHP логгирование
This file contains hidden or 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
| <?php | |
| /** | |
| * Логгирование запросов с группировкой в файлы (по времени) | |
| * - запись в лог параметров запроса GET и POST | |
| * - функция writelog(string $text) для кастомных записей лога | |
| * | |
| * Изначально писалось для логгирования обмена 1С-Предприятие с 1С-Битрикс | |
| * | |
| * @author Valeriy Grechukha <v.mrfriz@gmail.com> | |
| */ | |
| /** @var string $logfile Путь к файлу лога */ | |
| $logfile = __DIR__ . '/request_' . date('Y-m-d_H:i:s').'.log'; | |
| // $logfile = $_SERVER['DOCUMENT_ROOT'] . '/logs/request_' . date('Y-m-d_H:i:s').'.log'; | |
| /** @var int $groupping_seconds Количество секунд, в пределах которых запросы группируются */ | |
| $groupping_seconds = ini_get('max_execution_time') + 5; | |
| if (function_exists('writelog') { | |
| exit('Function writelog() already exists.'); | |
| } | |
| function writelog(string $text) { | |
| global $logfile; | |
| $saved_file = file_put_contents($logfile, date('H:i:s | ') . $text . PHP_EOL, FILE_APPEND); | |
| if (($saved_file === false) || ($saved_file == -1)) { | |
| exit("Couldn't save log to file {$logfile}"); | |
| } | |
| } | |
| if (!file_exists(dirname($logfile))) { | |
| mkdir(dirname($logfile).'/', 775); | |
| } else { | |
| $files = glob(dirname($logfile) . '/*'); | |
| if ($files) { | |
| $files = array_combine($files, array_map("filemtime", $files)); | |
| arsort($files); | |
| $latest_file = key($files); | |
| $file_modified_time = filemtime($latest_file); | |
| if ($file_modified_time >= time() - $groupping_seconds) { | |
| $filename = basename($latest_file); | |
| } | |
| } | |
| } | |
| writelog( | |
| date('H:i:s') . ' | ' . $_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI'] . | |
| (empty($_POST) | |
| ? '' | |
| : (PHP_EOL . 'POST ' . var_export($_POST, true)) | |
| ) | |
| ); |
This file contains hidden or 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
| <?php | |
| /** | |
| * Логгирование ВСЕХ ошибок длительных скриптов | |
| * - Exceptions | |
| * - error_reporting E_ALL | |
| * - функция writelog(string $text) для кастомных записей лога | |
| * | |
| * Изначально писалось для кастомного импорта в 1С-Битрикс | |
| * | |
| * @author Valeriy Grechukha <v.mrfriz@gmail.com> | |
| */ | |
| /** @var string $logfile Путь к файлу лога */ | |
| $logfile = __DIR__ . '/run_' . date('Y-m-d_H-i-s') . '.log'; | |
| // $logfile = $_SERVER['DOCUMENT_ROOT'] . '/logs/run_' . date('Y-m-d_H:i:s').'.log'; | |
| if (!file_exists(dirname($logfile))) { | |
| if (!mkdir(dirname($logfile).'/', 775)) { | |
| exit('Couldn\'t create directory ' . dirname($logfile)); | |
| } | |
| } | |
| if (function_exists('writelog') { | |
| exit('Function writelog() already exists.'); | |
| } | |
| function writelog(string $text) { | |
| global $logfile; | |
| $saved_file = file_put_contents($logfile, date('H:i:s | ') . $text . PHP_EOL, FILE_APPEND); | |
| if (($saved_file === false) || ($saved_file == -1)) { | |
| exit("Couldn't save log to file {$logfile}"); | |
| } | |
| } | |
| set_error_handler(function (int $errno, string $errstr, string $errfile = null, int $errline = null, array $errcontext = null) { | |
| writelog('Error | ' . $errstr . ' in ' . $errfile . ':' . $errline); | |
| return true; | |
| }, E_ALL); | |
| set_exception_handler(function ($e) { | |
| writelog('Exception | ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine()); | |
| return true; | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment