如何记录一些RESTful服务响应信息的Apache日志文件? [英] How to log some RESTful service response information apache log file?

查看:116
本文介绍了如何记录一些RESTful服务响应信息的Apache日志文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Java RESTful服务,该服务查询数据库并根据request参数将响应发送回去。我需要基于apache access_log生成一些简单的报告,例如每天的查询数量,类似的查询数量等。

I've a simple java RESTful service which queries database and send the response back based on the request parameter. I need to generate some simple report based on the apache access_log, for example number of queries/day, number of similar queries, etc.

我需要生成的报告之一是列出返回零结果的查询。我想知道如何实现这一目标。我不能依靠apache日志中的响应大小,因为仍然会返回结果为零的xml响应。

One of report I need to generate is to list queries which returns zero result. I'm wondering how to achieve this. I can't rely on the response size in apache log, since the the response xml with zero result will still be returned.

我正在考虑设置自定义Cookie如果查询没有返回结果并将其打印在apache日志中。.

I'm thinking of setting a custom cookie if the query returns no result and have it printed in apache log..


LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" \"%{Cookie}i\"" combined-cookie

不确定这是否可行还是说实话,这是正确的吗

Not sure if this will work or to be honest, is this is the right approach.

任何指针都会受到高度赞赏。

Any pointers will be highly appreciated.

谢谢

推荐答案

如果您确定无结果响应为 NNN 个字节,并且您知道其他任何响应都将有所不同(较大) ),那么您可能会在访问日志中查询大小为 NNN 的响应。但这是一个小技巧,如果空响应的大小由于任何原因而发生变化,都会很脆弱。

If you know for sure that the "no results" response is NNN bytes, and you know that any other response would be different (larger), then you could potentially query your access log for responses of size NNN. But that's a bit of a hack, and it's brittle if the size of an empty response changes for whatever reason.

我不认为Apache具有任何内置功能检查响应的内容并根据数据的某些属性设置变量。 (您可能会使用mod_ext_filter进行一些非常棘手的操作,但这不值得麻烦,并且性能可能会受到损害。)

I don't think Apache has any built-in capability to inspect the content of a response and set variables based on some property of the data. (You could potentially do something very hacky with mod_ext_filter, but it's not worth the hassle and the performance would likely suffer.)

听起来您已经具备了更改的能力产生响应的服务器代码。既然如此,我就不会尝试使用Apache日志记录。相反,我将向服务器添加一些其他日志记录功能。每个响应都可以将一行输出到另一个日志文件。这些行可能看起来像这样:

It sounds like you already have the ability to change the server code that's producing the response. Since that's the case, I would not try to use Apache logging. Instead, I would add some additional logging capability to your server. Every response could output a line to a different log file. The lines could look like this:

2012-06-14 14:02:15.345 count=0 status=Completed
2012-06-14 14:02:15.906 count=12 status=Completed
...

然后,您需要的报告类型将变得更容易。

Then the type of reporting you need becomes easier.

但是如果您绝对必须使用Apache,那么我的建议是发明一个新的HTTP标头,例如 X-Query-Result ,然后调整服务器以在每个响应上设置该标头。例如:

But if you absolutely have to do it with Apache, then my suggestion would be to invent a new HTTP header, something like X-Query-Result, and then tweak your server to set that header on every response. For example:

X-Query-Result: count=0 status=Completed

然后类似于您的建议,使用 \%{X-Query-Result} i\ 以您的日志格式。我会选择此选项而不是cookie,只是为了避免与cookie相关的额外重量。

Then similar to what you suggested, use \"%{X-Query-Result}i\" in your log format. I'd choose this over cookies just to avoid the extra "weight" associated with cookies.

这篇关于如何记录一些RESTful服务响应信息的Apache日志文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆