Yii2: Web service Request and Response Logger
I am trying to create a Request and Response Logger for a web service created in Yii 2.0.5 for debugging purpose.
Basically my motive is to track all the request, request data, response data and for this purpose I am using Yii Events. So far I have coded something like this:
UserController
use yiirestActiveController;
use yiibaseEvent;
use yiiwebResponse;
Event::on(ActiveController::className(), ActiveController::EVENT_AFTER_ACTION, ['appmodelsLogHandler', 'saveRequest'], ['request' => Yii::$app->request->getRawBody(), 'response' => Yii::$app->response->content]);
class UserController extends ActiveController
{
public $modelClass = 'appmodelsUser';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_XML;
return $behaviors;
}
// POST demo
public function actionDemo()
{
$data = array('status' => 200, 'message' => 'Success');
return $data;
}
}
In the above code if you have noticed then I have used the Class level Event handler (doc). Here I am trying to capture the Controllers EVENT_AFTER_ACTION event and passing the Request & Response object to my LogHandler's static method. However in my handler I am able to get the request's raw body but I am not able to get the response data which the actionDemo() is returning.
LogHandler
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord
{
public static function tableName()
{
return 'request_log';
}
public static function saveRequest($event)
{
// self::load($event);
// self::save();
var_dump($event);
}
}
How do I get the response data as well...
php yii2 yii2-basic-app
add a comment |
I am trying to create a Request and Response Logger for a web service created in Yii 2.0.5 for debugging purpose.
Basically my motive is to track all the request, request data, response data and for this purpose I am using Yii Events. So far I have coded something like this:
UserController
use yiirestActiveController;
use yiibaseEvent;
use yiiwebResponse;
Event::on(ActiveController::className(), ActiveController::EVENT_AFTER_ACTION, ['appmodelsLogHandler', 'saveRequest'], ['request' => Yii::$app->request->getRawBody(), 'response' => Yii::$app->response->content]);
class UserController extends ActiveController
{
public $modelClass = 'appmodelsUser';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_XML;
return $behaviors;
}
// POST demo
public function actionDemo()
{
$data = array('status' => 200, 'message' => 'Success');
return $data;
}
}
In the above code if you have noticed then I have used the Class level Event handler (doc). Here I am trying to capture the Controllers EVENT_AFTER_ACTION event and passing the Request & Response object to my LogHandler's static method. However in my handler I am able to get the request's raw body but I am not able to get the response data which the actionDemo() is returning.
LogHandler
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord
{
public static function tableName()
{
return 'request_log';
}
public static function saveRequest($event)
{
// self::load($event);
// self::save();
var_dump($event);
}
}
How do I get the response data as well...
php yii2 yii2-basic-app
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27
add a comment |
I am trying to create a Request and Response Logger for a web service created in Yii 2.0.5 for debugging purpose.
Basically my motive is to track all the request, request data, response data and for this purpose I am using Yii Events. So far I have coded something like this:
UserController
use yiirestActiveController;
use yiibaseEvent;
use yiiwebResponse;
Event::on(ActiveController::className(), ActiveController::EVENT_AFTER_ACTION, ['appmodelsLogHandler', 'saveRequest'], ['request' => Yii::$app->request->getRawBody(), 'response' => Yii::$app->response->content]);
class UserController extends ActiveController
{
public $modelClass = 'appmodelsUser';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_XML;
return $behaviors;
}
// POST demo
public function actionDemo()
{
$data = array('status' => 200, 'message' => 'Success');
return $data;
}
}
In the above code if you have noticed then I have used the Class level Event handler (doc). Here I am trying to capture the Controllers EVENT_AFTER_ACTION event and passing the Request & Response object to my LogHandler's static method. However in my handler I am able to get the request's raw body but I am not able to get the response data which the actionDemo() is returning.
LogHandler
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord
{
public static function tableName()
{
return 'request_log';
}
public static function saveRequest($event)
{
// self::load($event);
// self::save();
var_dump($event);
}
}
How do I get the response data as well...
php yii2 yii2-basic-app
I am trying to create a Request and Response Logger for a web service created in Yii 2.0.5 for debugging purpose.
Basically my motive is to track all the request, request data, response data and for this purpose I am using Yii Events. So far I have coded something like this:
UserController
use yiirestActiveController;
use yiibaseEvent;
use yiiwebResponse;
Event::on(ActiveController::className(), ActiveController::EVENT_AFTER_ACTION, ['appmodelsLogHandler', 'saveRequest'], ['request' => Yii::$app->request->getRawBody(), 'response' => Yii::$app->response->content]);
class UserController extends ActiveController
{
public $modelClass = 'appmodelsUser';
public function behaviors()
{
$behaviors = parent::behaviors();
$behaviors['contentNegotiator']['formats']['text/html'] = Response::FORMAT_XML;
return $behaviors;
}
// POST demo
public function actionDemo()
{
$data = array('status' => 200, 'message' => 'Success');
return $data;
}
}
In the above code if you have noticed then I have used the Class level Event handler (doc). Here I am trying to capture the Controllers EVENT_AFTER_ACTION event and passing the Request & Response object to my LogHandler's static method. However in my handler I am able to get the request's raw body but I am not able to get the response data which the actionDemo() is returning.
LogHandler
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord
{
public static function tableName()
{
return 'request_log';
}
public static function saveRequest($event)
{
// self::load($event);
// self::save();
var_dump($event);
}
}
How do I get the response data as well...
php yii2 yii2-basic-app
php yii2 yii2-basic-app
edited Aug 13 '15 at 14:33
Sagar Guhe
asked Aug 13 '15 at 14:06
Sagar GuheSagar Guhe
771729
771729
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27
add a comment |
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27
add a comment |
1 Answer
1
active
oldest
votes
According to the Yii guide here, the best way to create a logger component is to override the class yiilogTarget. Then to send the logs, you need to override the abstract method export() of this class
Since this class is accessed after the life cycle of the request, you will have the request and response data Yii::$app->request and Yii::$app->response and you can access them to create whatever message you want. This is an example of the LogHandler class (this can be edited to include whatever details you want about the request and response)
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord {
$requestPath;
$responseBody;
public static function tableName() {
return 'request_log';
}
}
And an example of a logger class is as follows:
namespace appcomponentsLogs;
use yiihelpersVarDumper;
use yiilogTarget;
use Yii;
class LoggerComponent extends Target {
public function export() {
$logMessage = new LogHandler();
$logMessage->requestPath = VarDumper::export(Yii::$app->request->absoluteUrl);
$logMessage->responseBody = VarDumper::export(Yii::$app->response->data);
$logMessage->save();
}
}
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31990534%2fyii2-web-service-request-and-response-logger%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
According to the Yii guide here, the best way to create a logger component is to override the class yiilogTarget. Then to send the logs, you need to override the abstract method export() of this class
Since this class is accessed after the life cycle of the request, you will have the request and response data Yii::$app->request and Yii::$app->response and you can access them to create whatever message you want. This is an example of the LogHandler class (this can be edited to include whatever details you want about the request and response)
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord {
$requestPath;
$responseBody;
public static function tableName() {
return 'request_log';
}
}
And an example of a logger class is as follows:
namespace appcomponentsLogs;
use yiihelpersVarDumper;
use yiilogTarget;
use Yii;
class LoggerComponent extends Target {
public function export() {
$logMessage = new LogHandler();
$logMessage->requestPath = VarDumper::export(Yii::$app->request->absoluteUrl);
$logMessage->responseBody = VarDumper::export(Yii::$app->response->data);
$logMessage->save();
}
}
add a comment |
According to the Yii guide here, the best way to create a logger component is to override the class yiilogTarget. Then to send the logs, you need to override the abstract method export() of this class
Since this class is accessed after the life cycle of the request, you will have the request and response data Yii::$app->request and Yii::$app->response and you can access them to create whatever message you want. This is an example of the LogHandler class (this can be edited to include whatever details you want about the request and response)
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord {
$requestPath;
$responseBody;
public static function tableName() {
return 'request_log';
}
}
And an example of a logger class is as follows:
namespace appcomponentsLogs;
use yiihelpersVarDumper;
use yiilogTarget;
use Yii;
class LoggerComponent extends Target {
public function export() {
$logMessage = new LogHandler();
$logMessage->requestPath = VarDumper::export(Yii::$app->request->absoluteUrl);
$logMessage->responseBody = VarDumper::export(Yii::$app->response->data);
$logMessage->save();
}
}
add a comment |
According to the Yii guide here, the best way to create a logger component is to override the class yiilogTarget. Then to send the logs, you need to override the abstract method export() of this class
Since this class is accessed after the life cycle of the request, you will have the request and response data Yii::$app->request and Yii::$app->response and you can access them to create whatever message you want. This is an example of the LogHandler class (this can be edited to include whatever details you want about the request and response)
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord {
$requestPath;
$responseBody;
public static function tableName() {
return 'request_log';
}
}
And an example of a logger class is as follows:
namespace appcomponentsLogs;
use yiihelpersVarDumper;
use yiilogTarget;
use Yii;
class LoggerComponent extends Target {
public function export() {
$logMessage = new LogHandler();
$logMessage->requestPath = VarDumper::export(Yii::$app->request->absoluteUrl);
$logMessage->responseBody = VarDumper::export(Yii::$app->response->data);
$logMessage->save();
}
}
According to the Yii guide here, the best way to create a logger component is to override the class yiilogTarget. Then to send the logs, you need to override the abstract method export() of this class
Since this class is accessed after the life cycle of the request, you will have the request and response data Yii::$app->request and Yii::$app->response and you can access them to create whatever message you want. This is an example of the LogHandler class (this can be edited to include whatever details you want about the request and response)
namespace appmodels;
use yiidbActiveRecord;
class LogHandler extends ActiveRecord {
$requestPath;
$responseBody;
public static function tableName() {
return 'request_log';
}
}
And an example of a logger class is as follows:
namespace appcomponentsLogs;
use yiihelpersVarDumper;
use yiilogTarget;
use Yii;
class LoggerComponent extends Target {
public function export() {
$logMessage = new LogHandler();
$logMessage->requestPath = VarDumper::export(Yii::$app->request->absoluteUrl);
$logMessage->responseBody = VarDumper::export(Yii::$app->response->data);
$logMessage->save();
}
}
edited Nov 19 '18 at 16:12
answered Nov 19 '18 at 12:21
mratebmrateb
583524
583524
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f31990534%2fyii2-web-service-request-and-response-logger%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Hi... I'm basically talking for my own shop, but have you looked at yii2-audit? It's free, based on the debug component and very thourough
– Blizz
Aug 13 '15 at 20:11
@Blizz thanks for introducing me to the yii2-audit, I would need to check and test this extension, at this moment I am not sure whether this tool help me achieve my task or not.. I will get back after testing..
– Sagar Guhe
Aug 14 '15 at 7:05
Hope it serves your needs. If you want to see it in action without having to install it, use the demo site.
– Blizz
Aug 14 '15 at 7:27
I just checked the demo site It looks the same as yii-debugger but this have represented the data in more understandable way but this also doesn't log the response content given to the requester :(
– Sagar Guhe
Aug 14 '15 at 7:46
That could be easily added by adding your own panel. I guess if this doesn't work for you, you are free to steal what you need to get it to do what you want. It is open source after all :)
– Blizz
Aug 14 '15 at 11:27