Skip to content

Commit 84a7c78

Browse files
committed
log: use logfmt for access logs instead of apache format, when requested
it pollutes the service's logs with non-structured data otherwise. This is more consistent.
1 parent d1e80c7 commit 84a7c78

2 files changed

Lines changed: 40 additions & 8 deletions

File tree

httpev.ml

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -326,10 +326,42 @@ let log_access_apache ch code size ?(background=false) req =
326326
with exn ->
327327
log #warn ~exn "access log : %s" (show_request req) ~structured_pairs:(pairs_of_request req)
328328

329-
let log_status_apache ch status size req =
330-
match status with
331-
| `No_reply -> () (* ignore *)
332-
| #reply_status as code -> log_access_apache ch (status_code code) size req
329+
let log_access_logfmt ch code size ?(background=false) req =
330+
try
331+
let now = Time.now () in
332+
let msg = Logfmt.to_string [
333+
"time", Time.to_string ~gmt:!Log.State.utc_timezone ~ms:true now;
334+
"msg", "httpev.serve";
335+
"req_id", string_of_int req.id;
336+
"client_addr", show_client_addr req;
337+
"http_duration", sprintf "%.4f" (now -. req.conn);
338+
"http_recv_duration", sprintf "%.4f" (req.recv -. req.conn);
339+
"http_host", header_safe req "host";
340+
"url", req.url;
341+
"http_user_agent", header_safe req "user-agent";
342+
"http_req_id", header_safe req "x-request-id";
343+
"http_request_line", req.line;
344+
"http_status", string_of_int code;
345+
"size", string_of_int size;
346+
"http_referer", header_referer req;
347+
"background", string_of_bool background;
348+
] in
349+
fprintf ch "%s\n%!" msg
350+
with exn ->
351+
log #warn ~exn "access log : %s" (show_request req) ~structured_pairs:(pairs_of_request req)
352+
353+
let emit_accesslog ch status size ?background req =
354+
let code = match status with
355+
| `No_reply -> None
356+
| `Code code -> Some code
357+
| #reply_status as code -> Some (status_code code)
358+
in
359+
match code with
360+
| None -> ()
361+
| Some code ->
362+
if Log.State.is_structured_format ()
363+
then log_access_logfmt ch code size ?background req
364+
else log_access_apache ch code size ?background req
333365

334366
(** Wait until [fd] becomes readable and close it (for eventfd-backed notifications) *)
335367
let wait base fd k =
@@ -439,7 +471,7 @@ let send_reply_user c req (code,hdrs,body) =
439471
(* hack for answer_forked, which logs on its own *)
440472
| ("X-Disable-Log", "true") :: hs -> hs
441473
| _ ->
442-
if c.server.config.access_log_enabled then log_status_apache !(c.server.config.access_log) code (String.length body) req;
474+
if c.server.config.access_log_enabled then emit_accesslog !(c.server.config.access_log) code (String.length body) req;
443475
hdrs
444476
in
445477
let hdrs = maybe_allow_cors c hdrs in
@@ -849,7 +881,7 @@ let answer_blocking ?(debug=false) srv req answer k =
849881
-1, None
850882
in
851883
if srv.config.access_log_enabled then
852-
log_access_apache !(srv.config.access_log) code (Int64.to_int !count) ~background:(continue <> None) req;
884+
emit_accesslog !(srv.config.access_log) (`Code code) (Int64.to_int !count) ~background:(continue <> None) req;
853885
call_me_maybe continue ()
854886

855887
let stats = new Var.typ "httpev.forks" "k"
@@ -917,7 +949,7 @@ let send_reply c cout reply =
917949
begin match c.req with
918950
| Ready req ->
919951
let size = match body with `Body s -> String.length s | `Chunks _ -> 0 in
920-
if c.server.config.access_log_enabled then log_status_apache !(c.server.config.access_log) code size req
952+
if c.server.config.access_log_enabled then emit_accesslog !(c.server.config.access_log) code size req
921953
| _ -> () (* this can happen when sending back error reply on malformed HTTP input *)
922954
end;
923955
(* filter headers *)

web.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ open Prelude
66
open Control
77
open Ocamlnet_lite
88

9-
let log = Log.self
9+
let log = Log.from "devkit.web"
1010

1111
(** percent-encode (convert space into %20) *)
1212
let rawurlencode = Netencoding.Url.encode ~plus:false

0 commit comments

Comments
 (0)