From 428729ebf7be2276e5ec5a0d325bbfa786c1d743 Mon Sep 17 00:00:00 2001 From: Sasha Rybkin Date: Wed, 20 Nov 2024 15:58:07 +0300 Subject: [PATCH] init --- Loger.php | 106 ++++++++++++++++++++++++++++++++++++++++++++++++++ composer.json | 21 ++++++++++ 2 files changed, 127 insertions(+) create mode 100644 Loger.php create mode 100644 composer.json diff --git a/Loger.php b/Loger.php new file mode 100644 index 0000000..45fab25 --- /dev/null +++ b/Loger.php @@ -0,0 +1,106 @@ +params['log']; + if ($type == 'error' || YII_DEBUG || $log) + { + $model = new Loger; + $model->maxFileSize = isset($config['maxFileSize']) ? $config['maxFileSize'] : 10; + $model->maxLogFiles = isset($config['maxLogFiles']) ? $config['maxLogFiles'] : 10000; + $model->enableRotation = isset($config['enableRotation']) ? (bool)$config['enableRotation'] : true; + $dir = Yii::getAlias($config['dir']); + $model->logFile = $dir . $prefixFile . (empty($prefixFile) ? $type : ucfirst($type) ) . '.log'; + $model->messages = [$result]; + $model->export(); + } + + if (isset($config['longRequestTime'], $result['time']) && $result['time'] > $config['longRequestTime']) + { + + $model = new Loger; + $model->maxFileSize = isset($config['maxFileSize']) ? $config['maxFileSize'] : 10; + $model->maxLogFiles = isset($config['maxLogFiles']) ? $config['maxLogFiles'] : 10000; + $model->enableRotation = isset($config['enableRotation']) ? (bool)$config['enableRotation'] : true; + $dir = Yii::getAlias($config['dir']); + $model->logFile = $dir . 'longRequestTime.log'; + $model->format = 'time'; + $model->messages = [$result['time']]; + $model->export(); + } + + } + + public function formatMessage($result) + { + + $output = ''; + $url = ''; + $post = ''; + $jwt = 'console'; + $HTTP_X_REAL_IP = ''; + if(!is_a(Yii::$app->request, 'yii\console\Request')) + { + $url = Yii::$app->request->url; + $post = json_encode(Yii::$app->request->post(), JSON_UNESCAPED_UNICODE); + $jwt = str_replace('Bearer ', '', Yii::$app->request->getHeaders()->get('Authorization')); + $HTTP_X_REAL_IP = Yii::$app->request->getHeaders()->get('HTTP_X_REAL_IP'); + } + switch ($this->format) + { + case 'time': + $output = sprintf( + "[%s]\t%s\t%s\t%s", + date('Y-m-d H:i:s'), + $url, + json_encode($result, JSON_UNESCAPED_UNICODE), + $post + ); + break; + case 'json': + $error = $result[0]; + if(is_a($error, 'Exception')) + { + $message = $error->getMessage() . ' in '.$error->getFile().':'.$error->getLine(); + $output = json_encode([ + 'date' => date('Y-m-d H:i:s'), + 'url' => $url, + 'message' => $message , + 'trace' => $error->getTraceAsString(), + 'graphql' => $post, + 'jwt' => $jwt, + 'HTTP_X_REAL_IP' => $HTTP_X_REAL_IP + ], JSON_UNESCAPED_UNICODE); + } + else + { + // $output = print_r($result, true); + } + + // var_dump($output); + break; + case 'result': + default : + $output = sprintf( + "[%s]\t%s\t%s\t%s", + date('Y-m-d H:i:s'), + $url, + $post, + json_encode($result, JSON_UNESCAPED_UNICODE) + ); + break; + } + return $output; + } +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..a315ce1 --- /dev/null +++ b/composer.json @@ -0,0 +1,21 @@ +{ + "name": "dominion/log", + "description": "Функционал для работы с log", + "type": "yii2-extension", + "keywords": ["yii2","extension"], + "license": "MIT", + "authors": [ + { + "name": "Rybkin Sasha", + "email": "ribkin@dominion.ru" + } + ], + "require": { + "yiisoft/yii2": "~2.0.0" + }, + "autoload": { + "psr-4": { + "dominion\\log\\": "" + } + } +} \ No newline at end of file