package nloge

  1. Overview
  2. Docs

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} *)
;;

Writing logs

Nloge writes logs, given by Emit effect, to Eio.Flow.sink

type Stdlib.Effect.t +=
  1. | Write : string * Level.t -> unit Stdlib.Effect.t
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.

  • parameter clock

    Eio.Time.clock clock objet

  • parameter sw

    Eio.Switch.t switch object to write output asynchronously

  • parameter outputs

    Eio.Flow.sink list the targets to write logs

  • parameter level

    Level.t option

Emitting log

Nloge "emit"s logs to Writing logs by "perform"ing Emit.

type 'a msgf = (('a, Stdlib.Format.formatter, unit, unit) Stdlib.format4 -> 'a) -> unit
type metadata = (string * Yojson.Safe.t) list
type 'a t = Level.t * string option * metadata * 'a msgf
type Stdlib.Effect.t +=
  1. | Emit : 'a t -> unit Stdlib.Effect.t

Emit is aimed to emit log objects. Writing logs can be injected to handle the effect.

type 'a logger = ?metadata:metadata -> ?__LOC__:string -> 'a msgf -> unit

Log functions

val emit : Level.t -> 'a msgf -> string option -> metadata -> unit

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

Utilities

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"
;;

Log Level and utilities

module Level : sig ... end
OCaml

Innovation. Community. Security.