在日志文件中包含POST请求的内容

时间:2018-10-24 01:18:29

标签: prolog swi-prolog

我有一个HTTP服务器,该服务器接受{"a": 3, "b": 4}形式的JSON请求并产生{"answer": 7}作为JSON响应:

:- use_module(library(http/thread_httpd)).
:- use_module(library(http/http_dispatch)).
:- use_module(library(http/http_json)).
:- use_module(library(http/http_log)).

:- http_handler('/', handle_request, []).

% Start server on specified port.
server(Port) :-
    http_server(http_dispatch, [port(Port)]).

% Calculate a + b.
solve(_{a:A, b:B}, _{answer:N}) :-
    number_codes(X, A),  % Convert from string to number.
    number_codes(Y, B),
    N is X + Y.

handle_request(Request) :-
    http_log('~w~n', Request),  % <--- ATTENTION.
    http_read_json_dict(Request, Query),
    solve(Query, Solution),
    reply_json_dict(Solution).

:- server(9000).

我已经添加了http_log('~w~n', Request)行以将所有请求记录到文件中。但是,生成的日志条目不包含发布请求的内容(即{"a": 3, "b": 4}),我希望该内容用于调试。日志文件如下所示:

server(started, 1540001234).
/*Wed Oct 24 01:23:45 2018*/ request(1, 1540123456.789, [peer(ip(127,0,0,1)),method(post),request_uri(/),path(/),http_version(1-1),host(localhost),port(9000),user_agent('wizard/1.2.3'),connection('keep-alive'),content_type('application/json'),content_length(20)]).
protocol(http)
completed(1, 0.00123456, 12, 200, ok).

问题是:POST请求的内容如何包含在日志文件中?

1 个答案:

答案 0 :(得分:1)

例如添加以下指令:

:- initialization(set_setting(http:log_post_data, 2_000)).

要了解更多信息,请参阅add_post_data/2的文档:

添加请求字段post_data(Data)  如果设置http:log_post_data是大于0的整数,则内容长度<此设置,并且nolog_post_content_type / 1在提供的内容类型上不成功。