Skip to content

Commit 06db0f7

Browse files
committed
avoid modifying existing plain log messages
1 parent c87470a commit 06db0f7

3 files changed

Lines changed: 58 additions & 19 deletions

File tree

httpev.ml

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,13 @@ let teardown fd =
275275
Exn.suppress (Unix.shutdown fd) Unix.SHUTDOWN_ALL;
276276
Unix.close fd
277277

278+
let log_req ?exn ?backtrace ?saved_backtrace level req fmt =
279+
ksprintf (fun msg ->
280+
match Log.State.get_cur_format () with
281+
| `Plain, _ -> log #put level ?exn ?backtrace ?saved_backtrace "%s : %s" msg (show_request req)
282+
| `Logfmt, _ -> log #put level ?exn ?backtrace ?saved_backtrace "%s" msg ~pairs:(pairs_of_request req)
283+
) fmt
284+
278285
let finish ?(shutdown=true) c =
279286
decr_active c.server;
280287
Hashtbl.remove c.server.clients c.req_id;
@@ -284,7 +291,7 @@ let finish ?(shutdown=true) c =
284291
| Ready req ->
285292
Hashtbl.remove c.server.reqs req.id;
286293
if c.server.config.debug then
287-
log #info "finished" ~pairs:(pairs_of_request req)
294+
log_req `Info req "finished"
288295

289296
let write_f c (data,ack) ev fd _flags =
290297
let finish () = finish c; Ev.del ev in
@@ -324,7 +331,7 @@ let log_access_apache ch code size ?(background=false) req =
324331
(header_safe req "x-request-id")
325332
(if background then " (BG)" else "")
326333
with exn ->
327-
log #warn ~exn "access log" ~pairs:(pairs_of_request req)
334+
log_req ~exn `Warn req "access log"
328335

329336
let log_status_apache ch status size req =
330337
match status with
@@ -498,10 +505,10 @@ let handle_request c body answer =
498505
| `Ok -> answer c.server req k
499506
end
500507
| _ ->
501-
log #info "version %u.%u not supported" (fst req.version) (snd req.version) ~pairs:(pairs_of_request req);
508+
log_req `Info req "version %u.%u not supported" (fst req.version) (snd req.version);
502509
send_reply_async c Identity (`Version_not_supported,[],"HTTP/1.0 is supported")
503510
with exn ->
504-
log #error ~exn "answer" ~pairs:(pairs_of_request req);
511+
log_req ~exn `Error req "answer";
505512
match req.blocking with
506513
| None -> send_reply_async c Identity (`Not_found,[],"Not found")
507514
| Some _ -> Exn.suppress teardown c.fd
@@ -639,7 +646,7 @@ let check_hung_requests server =
639646
let now = Time.now () in
640647
server.reqs |> Hashtbl.iter begin fun _ req ->
641648
if req.recv -. now > Time.minutes 30 then
642-
log #warn "request takes too much time to process" ~pairs:(pairs_of_request req)
649+
log_req `Warn req "request takes too much time to process"
643650
end
644651

645652
let check_waiting_requests srv =
@@ -845,7 +852,7 @@ let answer_blocking ?(debug=false) srv req answer k =
845852
| Continue continue -> 200, Some continue
846853
| exn ->
847854
let saved_backtrace = Exn.get_backtrace () in
848-
log #warn ~exn ~backtrace:debug ~saved_backtrace "answer forked" ~pairs:(pairs_of_request req);
855+
log_req ~exn ~backtrace:debug ~saved_backtrace `Warn req "answer forked";
849856
-1, None
850857
in
851858
if srv.config.access_log_enabled then
@@ -873,7 +880,7 @@ let answer_forked ?debug srv req answer k =
873880
end;
874881
U.sys_exit 0
875882
| `Forked pid ->
876-
log #info "forked %d" pid ~pairs:(pairs_of_request req);
883+
log_req `Info req "forked %d" pid;
877884
k (`No_reply,[],""); (* close socket in parent immediately *)
878885
Hashtbl.add srv.h_childs pid ()
879886
end
@@ -883,7 +890,7 @@ let answer_forked ?debug srv req answer k =
883890
do_fork ()
884891
with
885892
exn ->
886-
log #warn ~exn "answer fork failed" ~pairs:(pairs_of_request req);
893+
log_req ~exn `Warn req "answer fork failed";
887894
k (`Internal_server_error,[],"")
888895
in
889896
if Hashtbl.length srv.h_childs < srv.config.max_data_childs then
@@ -899,7 +906,7 @@ let answer_forked ?debug srv req answer k =
899906
else
900907
begin
901908
incr nr_rejected;
902-
log #info "rejecting, overloaded" ~pairs:(pairs_of_request req);
909+
log_req `Info req "rejecting, overloaded";
903910
k (`Service_unavailable, ["Content-Type", "text/plain"], "overloaded")
904911
end
905912

@@ -989,11 +996,11 @@ let handle_request_lwt c req answer =
989996
try%lwt
990997
answer c.server req
991998
with exn ->
992-
log #error ~exn "answer" ~pairs:(pairs_of_request req);
999+
log_req ~exn `Error req "answer";
9931000
return (`Not_found,[],"Not found")
9941001
end
9951002
| _ ->
996-
log #info "version %u.%u not supported" (fst req.version) (snd req.version) ~pairs:(pairs_of_request req);
1003+
log_req `Info req "version %u.%u not supported" (fst req.version) (snd req.version);
9971004
return (`Version_not_supported,[],"HTTP/1.0 is supported")
9981005

9991006
let read_buf ic buf =
@@ -1163,7 +1170,7 @@ let rest ~show_exn req answer =
11631170
| Arg.Bad s -> bad_request @@ sprintf "bad parameter %s in %s" s req.url
11641171
| exn ->
11651172
let ref = random_ref () in
1166-
log#warn ~exn "failed ref:%Ld" ref ~pairs:(pairs_of_request req);
1173+
log_req ~exn `Warn req "failed ref:%Ld" ref;
11671174
if show_exn then
11681175
internal_error @@ sprintf "internal error ref:%Ld : %s" ref (match exn with Failure s -> s | _ -> Exn.str exn)
11691176
else

log.ml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ open Prelude
4040

4141
(** Global logger state *)
4242
module State = struct
43-
4443
let all = Hashtbl.create 10
4544
let default_level = ref (`Info : Logger.level)
4645

@@ -109,13 +108,16 @@ module State = struct
109108
Buffer.add_char buf '\n';
110109
Buffer.contents buf
111110

112-
let cur_format = Atomic.make format_simple_full
113-
let set_cur_format f = Atomic.set cur_format f
114-
let set_plaintext () = set_cur_format format_simple_full
115-
let set_logfmt () = set_cur_format format_logfmt
111+
open struct
112+
let cur_format: ([`Plain|`Logfmt]*_) Atomic.t = Atomic.make (`Plain, format_simple_full)
113+
let set_cur_format f = Atomic.set cur_format f
114+
end
115+
let get_cur_format () = Atomic.get cur_format
116+
let set_plaintext () = set_cur_format (`Plain, format_simple_full)
117+
let set_logfmt () = set_cur_format (`Logfmt, format_logfmt)
116118

117119
let format level facil ts pairs msg =
118-
(Atomic.get cur_format) level facil ts pairs msg
120+
(snd (Atomic.get cur_format)) level facil ts pairs msg
119121

120122
let format_simple level facil msg =
121123
format level facil (Unix.gettimeofday()) [] msg

web.ml

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,32 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
236236
| code -> `Error code
237237
end
238238

239-
let verbose_curl_result nr_http action t h code =
239+
let verbose_curl_result_plain nr_http action t h code =
240+
let open Curl in
241+
let b = Buffer.create 10 in
242+
bprintf b "%s #%d %s ⇓%s ⇑%s %s "
243+
(string_of_http_action action) nr_http (Time.compact_duration t#get)
244+
(Action.bytes_string_f @@ get_sizedownload h)
245+
(Action.bytes_string_f @@ get_sizeupload h)
246+
(get_primaryip h)
247+
;
248+
begin match code with
249+
| CURLE_OK ->
250+
bprintf b "HTTP %d %s" (get_httpcode h) (get_effectiveurl h);
251+
begin match get_redirecturl h with
252+
| "" -> ()
253+
| s -> bprintf b " -> %s" s
254+
end;
255+
begin match get_redirectcount h with
256+
| 0 -> ()
257+
| n -> bprintf b " after %d redirects" n
258+
end
259+
| _ ->
260+
bprintf b "error (%d) %s (errno %d)" (errno code) (strerror code) (Curl.get_oserrno h)
261+
end;
262+
log #info_s (Buffer.contents b)
263+
264+
let verbose_curl_result_logfmt nr_http action t h code =
240265
let open Curl in
241266
let size_down = get_sizedownload h in
242267
let size_up = get_sizeupload h in
@@ -269,6 +294,11 @@ module Http (IO : IO_TYPE) (Curl_IO : CURL with type 'a t = 'a IO.t) : HTTP with
269294
in
270295
log #info ~pairs "http error"
271296

297+
let verbose_curl_result nr_http action t h code =
298+
match Log.State.get_cur_format () with
299+
| `Plain, _ -> verbose_curl_result_plain nr_http action t h code
300+
| `Logfmt, _ -> verbose_curl_result_logfmt nr_http action t h code
301+
272302
(* Given a list of strings, check pre-existing entry starting with `~name`; and adds the concatenation of `~name` and `~value` if not. *)
273303
let add_if_absent ~name ~value strs =
274304
match strs with

0 commit comments

Comments
 (0)