make post-init Xlog.logs work

This commit is contained in:
xenia 2024-04-23 03:43:55 -04:00
parent 501da880cb
commit 48f761d210
3 changed files with 44 additions and 9 deletions

View File

@ -46,3 +46,23 @@ else
Xlog.init_pretty_writer stdout Xlog.init_pretty_writer stdout
~min_level:Xlog.DEBUG ~min_level:Xlog.DEBUG
``` ```
## usage with ppx
```ocaml
[%%xlog_import]
[%xlog info "log message %d" 1337]
try
fallible_task ()
with
| e -> [%xlog exn e "oop's"]
module Submodule = struct
[%%xlog_import]
let do_stuff () =
[%xlog warn "meow"]
end
```

View File

@ -12,7 +12,7 @@ type logger = {
let rec root_logger = { let rec root_logger = {
namespace = ""; namespace = "";
pnamespace = []; pnamespace = [];
min_level = 0; min_level = Int.max_int;
child = root_logger; child = root_logger;
sibling = root_logger; sibling = root_logger;
writers = None; writers = None;
@ -31,10 +31,10 @@ let make_logger parent_logger ns =
let rec logger = { let rec logger = {
namespace = String.concat "." (List.rev pnamespace); namespace = String.concat "." (List.rev pnamespace);
pnamespace = pnamespace; pnamespace = pnamespace;
min_level = Int.max_int; min_level = parent_logger.min_level;
child = logger; child = logger;
sibling = parent_logger.child; sibling = parent_logger.child;
writers = None; writers = parent_logger.writers;
} in } in
parent_logger.child <- logger; parent_logger.child <- logger;
logger logger

View File

@ -1,13 +1,28 @@
[%%xlog_import] [%%xlog_import]
let () = module Submodule = struct
Xlog.init_pretty_writer stdout ~min_level:Xlog.DEBUG [%%xlog_import]
let do_stuff () =
[%xlog warn "meow"]
end
let () = let () =
[%xlog info "meow meow %s %d" "test string" 1337] Xlog.init_pretty_writer stdout ~min_level:Xlog.DEBUG;
[%xlog info "meow meow %s %d" "test string" 1337];
let () = begin try
try
failwith "meow2" failwith "meow2"
with with
| e -> [%xlog exn e "oop's %d" 42069] | e -> [%xlog exn e "oop's %d" 42069]
end;
[%xlog info "awa"];
Submodule.do_stuff ()
module Submodule2 = struct
[%%xlog_import]
let do_stuff () =
[%xlog warn "meow"]
end
let () = Submodule2.do_stuff ()