Library
Module
Module type
Parameter
Class
Class type
Nloge is a asynchronous logger with Eio.
By default nloge logs with JSON format:
let () =
Eio_main.run @@ fun env ->
Eio.Switch.run @@ fun sw ->
Nloge.run ~clock:env#clock ~sw ~outputs:[ env#stdout ] ~level:`Debug @@ fun () ->
Nloge.debug
~__LOC__
~metadata:[ "now_posix", `Float (Eio.Time.now env#clock) ]
(fun m -> m "hello")
(* {"log_level":"DEBUG","message":"hello","label":"File \"examples/hello.ml\", line 9, characters 5-12","now_posix":1683056241.957986} *)
;;
Nloge writes logs, given by Emit
effect, to Eio.Flow.sink
val write : Level.t -> string -> unit
module Trans : sig ... end
val run :
clock:Eio.Time.clock ->
sw:Eio__core.Switch.t ->
outputs:Eio.Flow.sink list ->
level:Level.t ->
?trans:((unit -> 'r) -> 'r) ->
(unit -> 'r) ->
'r
run
is the handler to write logs to given outputs.
Nloge "emit"s logs to Writing logs by "perform"ing Emit
.
Emit
is aimed to emit log objects. Writing logs can be injected to handle the effect.
emit level ?metadata ?loc msgf
sends msgf
message object with level level
. metadata
and loc
can be sent optionally.
emit `Debug @@ fun m -> m "hello, %s" "world" (* -> hello, world *)
And the following emg
, alert
, etc. are wrapper for emit
with correspondng level
.
debug ~metadata:["Key", `String "Val" ] ~__LOC__ @@ fun m -> m "the answer: %d" 42
val emg : 'a logger
val alert : 'a logger
val crit : 'a logger
val err : 'a logger
val warn : 'a logger
val notice : 'a logger
val info : 'a logger
val debug : 'a logger
val make_emit_handler :
(unit -> 'r) ->
(float -> Level.t -> string option -> metadata -> string -> unit) ->
'r
make_emit_handler
is a utility to build a handler for Emit
.
let plain_transformer f =
Nloge.make_emit_handler f
@@ fun now level loc metadata msg ->
let json = Nloge.Trans.insert_info now level loc msg metadata in
let len = List.length json in
let k0 = Format.kasprintf (Nloge.write level) "%a:\n%s" Nloge.Level.pp level in
let k, _ =
ListLabels.fold_left json ~init:(k0, 0) ~f:(fun (k, idx) (label, v) ->
let idx' = idx + 1 in
let comma = if idx' = len then "" else "\n" in
( Format.kasprintf
k
"\t%-10s = %a%s%s"
label
(Yojson.Safe.pretty_print ~std:true)
v
comma
, idx' ))
in
k "\n"
;;
module Level : sig ... end