Here’s an example that uses kfprintf
:
let indirect =
{ Logs.report =
(fun src level ~over k msgf ->
msgf
@@ fun ?header ?tags fmt ->
let k = Format.(kfprintf (fun _ -> k ()) str_formatter fmt) in
let msg = Format.flush_str_formatter () in
(* in real usage, the log messages aren't going to stdout/stderr *)
print_endline @@ "Log message: " ^ msg;
over ();
k)
}
let () =
Logs.set_level ~all:true @@ Some Logs.Debug;
Logs.set_reporter indirect;
Logs.info (fun m -> m "No format annotations");
for i = 1 to 5 do
Logs.info (fun m -> m "message %d" i)
done;
Logs.info (fun m -> m "All done")
Outputs:
$ ocamlc -I `ocamlfind query logs` logs.cma -o loggingtest src/exe/test.ml && ./loggingtest
Log message: No format annotations
Log message:
Log message: message 1
Log message: message 2
Log message: message 3
Log message: message 4
Log message: message 5All done