From f08f42e9a399a467b27129fae8695ba850f0710c Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Thu, 25 Jul 2024 10:10:34 +0200 Subject: [PATCH 1/2] Resto: clean log interface --- resto/src/server.ml | 33 ++++++++++--------- resto/src/server.mli | 8 ++--- resto/test/acl_integration_test.ml | 8 ++--- resto/test/chunked_output_integration_test.ml | 8 ++--- resto/test/self_serving_client_test.ml | 8 ++--- src/lib_mockup_proxy/RPC_client.ml | 8 ++--- src/lib_rpc_http/RPC_server.ml | 8 ++--- 7 files changed, 42 insertions(+), 39 deletions(-) diff --git a/resto/src/server.ml b/resto/src/server.ml index d228f24cd88b..d99ef715320b 100644 --- a/resto/src/server.ml +++ b/resto/src/server.ml @@ -62,23 +62,23 @@ let wseq ic oc seq = Lwt.finalize (fun () -> wseq ic oc seq) (fun () -> Lwt_io.close ic) module type LOGGING = sig - val debug : ('a, Format.formatter, unit, unit) format4 -> 'a + val log_debug : ('a, Format.formatter, unit, unit) format4 -> 'a val log_info : ('a, Format.formatter, unit, unit) format4 -> 'a val log_notice : ('a, Format.formatter, unit, unit) format4 -> 'a - val warn : ('a, Format.formatter, unit, unit) format4 -> 'a + val log_warn : ('a, Format.formatter, unit, unit) format4 -> 'a val log_error : ('a, Format.formatter, unit, unit) format4 -> 'a - val lwt_debug : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a + val lwt_log_debug : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_info : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_notice : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a - val lwt_warn : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a + val lwt_log_warn : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_error : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a end @@ -214,13 +214,13 @@ module Make_selfserver (Encoding : Resto.ENCODING) (Log : LOGGING) = struct match answer with | `Ok o -> let body = output o in - Log.debug "server (%s) response code:200" con_string ; - Log.debug "server (%s) response body: %s" con_string body ; + Log.log_debug "server (%s) response code:200" con_string ; + Log.log_debug "server (%s) response body: %s" con_string body ; let encoding = Transfer.Fixed (Int64.of_int (String.length body)) in ( Response.make ~status:`OK ~encoding ?headers (), Cohttp_lwt.Body.of_string body ) | `No_content -> - Log.debug "server (%s) response code:204 (no content)" con_string ; + Log.log_debug "server (%s) response code:204 (no content)" con_string ; (Response.make ~status:`No_content (), Cohttp_lwt.Body.empty) | `Created s -> let headers = Header.init () in @@ -229,7 +229,7 @@ module Make_selfserver (Encoding : Resto.ENCODING) (Log : LOGGING) = struct | None -> headers | Some s -> Header.add headers "location" s in - Log.debug "server (%s) response code:201 (created)" con_string ; + Log.log_debug "server (%s) response code:201 (created)" con_string ; (Response.make ~status:`Created ~headers (), Cohttp_lwt.Body.empty) let handle_rpc_answer_error con_string ?headers error answer = @@ -375,13 +375,14 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct path_and_query >>= fun () -> let req_headers = Request.headers req in - Log.lwt_debug + Log.lwt_log_debug "server (%s) request headers: %s" con_string (Header.to_string req_headers) >>= fun () -> Cohttp_lwt.Body.to_string body >>= fun body -> - Log.lwt_debug "server (%s) request body: %s" con_string body >>= fun () -> + Log.lwt_log_debug "server (%s) request body: %s" con_string body + >>= fun () -> let path = Uri.path uri in let path = Resto.Utils.decode_split_path path in (match Request.meth req with @@ -392,7 +393,7 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct >>=? fun (Directory.Service s) -> Media.input_media_type ~headers:req_headers server.medias >>? fun input_media_type -> - Log.lwt_debug + Log.lwt_log_debug "server (%s) input media type %s" con_string (Media_type.name input_media_type) @@ -408,7 +409,7 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct Lwt.return_error (`Cannot_parse_query s) | query -> Lwt.return_ok query) >>=? fun query -> - Log.lwt_debug + Log.lwt_log_debug "server (%s) ouput media type %s" con_string (Media_type.name output_media_type) @@ -440,7 +441,7 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct lwt_return_ok_response response | `OkChunk _ as a -> let output_seq = output_media_type.construct_seq s.types.output in - Log.lwt_debug + Log.lwt_log_debug "server (%s) response code:200 (with chunk transfer\n\ \ encoding)" con_string @@ -451,7 +452,9 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct let output = output_media_type.construct s.types.output in let body = create_stream server con output o in let encoding = Transfer.Chunked in - Log.lwt_debug "server (%s) response code:200 (streamed)" con_string + Log.lwt_log_debug + "server (%s) response code:200 (streamed)" + con_string >>= fun () -> lwt_return_ok_response ( Response.make ~status:`OK ~encoding ~headers (), @@ -510,7 +513,7 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct (let conn_closed ((_, con) as c) = let () = conn_closed c in let con_string = Connection.to_string con in - Log.debug "server (%s) got conn closed" con_string ; + Log.log_debug "server (%s) got conn closed" con_string ; try ConnectionMap.find con server.streams () with Not_found -> () and on_exn = function | Unix.Unix_error (Unix.EADDRINUSE, "bind", _) -> diff --git a/resto/src/server.mli b/resto/src/server.mli index 3e94d12f68ce..15dd0dd8eb98 100644 --- a/resto/src/server.mli +++ b/resto/src/server.mli @@ -27,23 +27,23 @@ (** Serving a directory of registered services. *) module type LOGGING = sig - val debug : ('a, Format.formatter, unit, unit) format4 -> 'a + val log_debug : ('a, Format.formatter, unit, unit) format4 -> 'a val log_info : ('a, Format.formatter, unit, unit) format4 -> 'a val log_notice : ('a, Format.formatter, unit, unit) format4 -> 'a - val warn : ('a, Format.formatter, unit, unit) format4 -> 'a + val log_warn : ('a, Format.formatter, unit, unit) format4 -> 'a val log_error : ('a, Format.formatter, unit, unit) format4 -> 'a - val lwt_debug : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a + val lwt_log_debug : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_info : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_notice : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a - val lwt_warn : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a + val lwt_log_warn : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a val lwt_log_error : ('a, Format.formatter, unit, unit Lwt.t) format4 -> 'a end diff --git a/resto/test/acl_integration_test.ml b/resto/test/acl_integration_test.ml index 2ba2984702e9..55a46fc417b4 100644 --- a/resto/test/acl_integration_test.ml +++ b/resto/test/acl_integration_test.ml @@ -58,17 +58,17 @@ let json = let media_types = [json] module Logger : Resto_cohttp_server.Server.LOGGING = struct - let debug fmt = Format.kasprintf (Format.printf "[DEBUG]: %s\n%!") fmt + let log_debug fmt = Format.kasprintf (Format.printf "[DEBUG]: %s\n%!") fmt let log_info fmt = Format.kasprintf (Printf.printf "[INFO]: %s\n%!") fmt let log_notice fmt = Format.kasprintf (Printf.printf "[NOTICE]: %s\n%!") fmt - let warn fmt = Format.kasprintf (Printf.printf "[WARN]: %s\n%!") fmt + let log_warn fmt = Format.kasprintf (Printf.printf "[WARN]: %s\n%!") fmt let log_error fmt = Format.kasprintf (Printf.eprintf "[ERROR]: %s\n%!") fmt - let lwt_debug fmt = + let lwt_log_debug fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[DEBUG]: %s\n%!") fmt let lwt_log_info fmt = @@ -77,7 +77,7 @@ module Logger : Resto_cohttp_server.Server.LOGGING = struct let lwt_log_notice fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[NOTICE]: %s\n%!") fmt - let lwt_warn fmt = + let lwt_log_warn fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[WARN]: %s\n%!") fmt let lwt_log_error fmt = diff --git a/resto/test/chunked_output_integration_test.ml b/resto/test/chunked_output_integration_test.ml index 4dd9c3b89add..61fba94f9dd6 100644 --- a/resto/test/chunked_output_integration_test.ml +++ b/resto/test/chunked_output_integration_test.ml @@ -70,17 +70,17 @@ let json chunk_size = let media_types chunk_size = [json chunk_size] module Logger : Resto_cohttp_server.Server.LOGGING = struct - let debug fmt = Format.kasprintf (Format.printf "[DEBUG]: %s\n%!") fmt + let log_debug fmt = Format.kasprintf (Format.printf "[DEBUG]: %s\n%!") fmt let log_info fmt = Format.kasprintf (Printf.printf "[INFO]: %s\n%!") fmt let log_notice fmt = Format.kasprintf (Printf.printf "[NOTICE]: %s\n%!") fmt - let warn fmt = Format.kasprintf (Printf.printf "[WARN]: %s\n%!") fmt + let log_warn fmt = Format.kasprintf (Printf.printf "[WARN]: %s\n%!") fmt let log_error fmt = Format.kasprintf (Printf.eprintf "[ERROR]: %s\n%!") fmt - let lwt_debug fmt = + let lwt_log_debug fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[DEBUG]: %s\n%!") fmt let lwt_log_info fmt = @@ -89,7 +89,7 @@ module Logger : Resto_cohttp_server.Server.LOGGING = struct let lwt_log_notice fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[NOTICE]: %s\n%!") fmt - let lwt_warn fmt = + let lwt_log_warn fmt = Format.kasprintf Lwt_fmt.(fprintf stdout "[WARN]: %s\n%!") fmt let lwt_log_error fmt = diff --git a/resto/test/self_serving_client_test.ml b/resto/test/self_serving_client_test.ml index 13cd7cd56ea4..06bf295f829a 100644 --- a/resto/test/self_serving_client_test.ml +++ b/resto/test/self_serving_client_test.ml @@ -28,23 +28,23 @@ module Encoding = Resto_json.Encoding module Media_type = Resto_cohttp.Media_type.Make (Encoding) module NullLogger : Resto_cohttp_server.Server.LOGGING = struct - let debug fmt = Format.kasprintf ignore fmt + let log_debug fmt = Format.kasprintf ignore fmt let log_info fmt = Format.kasprintf ignore fmt let log_notice fmt = Format.kasprintf ignore fmt - let warn fmt = Format.kasprintf ignore fmt + let log_warn fmt = Format.kasprintf ignore fmt let log_error fmt = Format.kasprintf ignore fmt - let lwt_debug fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt + let lwt_log_debug fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_info fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_notice fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt - let lwt_warn fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt + let lwt_log_warn fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_error fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt end diff --git a/src/lib_mockup_proxy/RPC_client.ml b/src/lib_mockup_proxy/RPC_client.ml index 996da594127e..3381f13b0039 100644 --- a/src/lib_mockup_proxy/RPC_client.ml +++ b/src/lib_mockup_proxy/RPC_client.ml @@ -29,23 +29,23 @@ module Service = Tezos_rpc.Service let media_types = [Tezos_rpc_http.Media_type.json] module NullLogger = struct - let debug fmt = Format.kasprintf ignore fmt + let log_debug fmt = Format.kasprintf ignore fmt let log_info fmt = Format.kasprintf ignore fmt let log_notice fmt = Format.kasprintf ignore fmt - let warn fmt = Format.kasprintf ignore fmt + let log_warn fmt = Format.kasprintf ignore fmt let log_error fmt = Format.kasprintf ignore fmt - let lwt_debug fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt + let lwt_log_debug fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_info fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_notice fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt - let lwt_warn fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt + let lwt_log_warn fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt let lwt_log_error fmt = Format.kasprintf (fun _ -> Lwt.return_unit) fmt end diff --git a/src/lib_rpc_http/RPC_server.ml b/src/lib_rpc_http/RPC_server.ml index 26d4e7a18773..50530986972d 100644 --- a/src/lib_rpc_http/RPC_server.ml +++ b/src/lib_rpc_http/RPC_server.ml @@ -69,23 +69,23 @@ module RPC_logging = struct (Printf.sprintf "[pid:%d][resto] %s" (Unix.getpid ()) message)) fmt - let debug f = emit_async rpc_http_event_debug f + let log_debug f = emit_async rpc_http_event_debug f let log_info f = emit_async rpc_http_event_info f let log_notice f = emit_async rpc_http_event_notice f - let warn f = emit_async rpc_http_event_warning f + let log_warn f = emit_async rpc_http_event_warning f let log_error f = emit_async rpc_http_event_error f - let lwt_debug f = emit_lwt rpc_http_event_debug f + let lwt_log_debug f = emit_lwt rpc_http_event_debug f let lwt_log_info f = emit_lwt rpc_http_event_info f let lwt_log_notice f = emit_lwt rpc_http_event_notice f - let lwt_warn f = emit_lwt rpc_http_event_warning f + let lwt_log_warn f = emit_lwt rpc_http_event_warning f let lwt_log_error f = emit_lwt rpc_http_event_error f end -- GitLab From 240a6064e19f140ae6f6aac6d830ba5feec937ad Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Thu, 25 Jul 2024 10:39:13 +0200 Subject: [PATCH 2/2] Resto: move log to debug --- resto/src/server.ml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/resto/src/server.ml b/resto/src/server.ml index d99ef715320b..632ba3961d60 100644 --- a/resto/src/server.ml +++ b/resto/src/server.ml @@ -529,7 +529,7 @@ module Make (Encoding : Resto.ENCODING) (Log : LOGGING) = struct (Printexc.get_backtrace ()) and callback (io, con) req body = let con_string = Connection.to_string con in - Log.log_info "server (%s) connection accepted" con_string ; + Log.log_debug "server (%s) connection accepted" con_string ; Lwt.catch (fun () -> callback (io, con) req body) (function -- GitLab