aantron / dream

Tidy, feature-complete Web framework

Home Page:https://aantron.github.io/dream/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Creating a sub_log does not respect the initial log level

leflings opened this issue · comments

Given the folllowing code:

let my_log = Dream.sub_log ~level:`Error "my.log"

let () =
  Dream.run
  @@ Dream.logger
  @@ Dream.router
       [ Dream.get "/" (fun _ -> Dream.html "Welcome to my website")
       ; Dream.get "/echo/:word" (fun req ->
           my_log.warning (fun log -> log ~request:req "echo path");
           Dream.html (Printf.sprintf "Hello, %s" @@ Dream.param req "word"))
       ]
;;

I expect that echo path will not be logged, as it is a warning, and this sub_log is only configured with errors.

However, it is still being logged:

15.09.23 10:00:58.532       dream.log  INFO REQ 1 GET /echo/dream 127.0.0.1:45198 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/51
15.09.23 10:00:58.532          my.log  WARN REQ 1 echo path
15.09.23 10:00:58.532       dream.log  INFO REQ 1 200 in 87 μs

"Manually" setting the log level with the following line:

let () = Dream.set_log_level "my.log" `Error

Makes it behave as expected:

15.09.23 10:03:37.447       dream.log  INFO REQ 1 GET /echo/dream 127.0.0.1:33510 Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/51
15.09.23 10:03:37.447       dream.log  INFO REQ 1 200 in 56 μs

I am running OCaml 4.14.1 under WSL2

Thank you! This has been fixed in the linked commit and will be out in the next release.

The whole logging initialization code could probably use a rework, as it was already complex when only trying to integrate with Logs, but it is now even more complex because of Mirage support, and it's difficult to follow.