···110110111111(** {1 Trace File Reporter} *)
112112113113+let level_string = function
114114+ | Logs.App -> "app"
115115+ | Logs.Error -> "error"
116116+ | Logs.Warning -> "warning"
117117+ | Logs.Info -> "info"
118118+ | Logs.Debug -> "debug"
119119+120120+let write_json_trace ppf ~ts ~name ~level_s msg k =
121121+ Fmt.pf ppf {|{"ts":"%s","src":"%s","level":"%s","msg":"%s"}@.|} ts name
122122+ level_s (String.escaped msg);
123123+ k ()
124124+125125+let write_tracing_entry ~json ppf ts name level fmt k =
126126+ if json then
127127+ Fmt.kstr
128128+ (fun msg ->
129129+ write_json_trace ppf ~ts ~name ~level_s:(level_string level) msg k)
130130+ fmt
131131+ else Fmt.kpf k ppf ("%s %s " ^^ fmt ^^ "@.") ts name
132132+113133let trace_reporter ~json file_path =
114134 let oc = open_out file_path in
115135 let ppf = Format.formatter_of_out_channel oc in
···126146 k ()
127147 in
128148 let ts = Ptime_clock.now () |> Ptime.to_rfc3339 in
129129- if json then
130130- (* JSON format *)
131131- let buf = Buffer.create 256 in
132132- Format.kfprintf
133133- (fun _ ->
134134- let msg = Buffer.contents buf in
135135- let level_s =
136136- match level with
137137- | Logs.App -> "app"
138138- | Logs.Error -> "error"
139139- | Logs.Warning -> "warning"
140140- | Logs.Info -> "info"
141141- | Logs.Debug -> "debug"
142142- in
143143- Fmt.pf ppf {|{"ts":"%s","src":"%s","level":"%s","msg":"%s"}@.|} ts
144144- name level_s (String.escaped msg);
145145- k ())
146146- (Format.formatter_of_buffer buf)
147147- fmt
148148- else
149149- (* Plain text format *)
150150- Format.kfprintf k ppf ("%s %s " ^^ fmt ^^ "@.") ts name
149149+ write_tracing_entry ~json ppf ts name level fmt k
151150 else (
152151 over ();
153152 k ())
···183182 let doc = "Output as JSON (affects both data output and logs)." in
184183 Arg.(value & flag & info [ "json" ] ~doc)
185184185185+let err_invalid_tag s =
186186+ Error (`Msg (Fmt.str "Invalid tag format '%s', expected KEY=VALUE" s))
187187+186188let parse_tag s =
187189 match String.index_opt s '=' with
188188- | None ->
189189- Error (`Msg (Fmt.str "Invalid tag format '%s', expected KEY=VALUE" s))
190190+ | None -> err_invalid_tag s
190191 | Some i ->
191192 let key = String.sub s 0 i in
192193 let value = String.sub s (i + 1) (String.length s - i - 1) in
+7-6
lib/vlog.mli
···7878 For advanced use cases where you need more control. *)
79798080val quiet : bool Cmdliner.Term.t
8181-(** Term for [-q]/[--quiet] flag. *)
8181+(** [quiet] is the term for [-q]/[--quiet] flag. *)
82828383val verbosity : bool list Cmdliner.Term.t
8484-(** Term for [-v]/[--verbose] flags. Length indicates verbosity level. *)
8484+(** [verbosity] is the term for [-v]/[--verbose] flags. Length indicates
8585+ verbosity level. *)
85868687val log_term : string -> string option Cmdliner.Term.t
8788(** [log_term app_name] returns a term for [--log] with environment variable
···9192(** Term for [--trace FILE] flag. *)
92939394val json : bool Cmdliner.Term.t
9494-(** Term for [--json] flag. *)
9595+(** [json] is the term for [--json] flag. *)
95969697val log_tags : (string * string) list Cmdliner.Term.t
9798(** Term for [--log-tag KEY=VALUE] flags. *)
···106107 Examples:
107108 - ["debug"] -> [Some Debug, []]
108109 - ["info,http:debug"] -> [Some Info, [("http", Some Debug)]]
109109- - ["conpool:warning"] -> [None, [("conpool", Some Warning)]] *)
110110+ - ["conpool:warning"] -> [None, [("conpool", Some Warning)]]. *)
110111111112val apply_source_overrides : (string * Logs.level option) list -> unit
112113(** [apply_source_overrides overrides] sets log levels for matching sources.
···125126 - [quiet=true]: Error
126127 - [verbosity=[]]: Warning
127128 - [verbosity=[_]]: Info
128128- - [verbosity=[_;_]] or more: Debug *)
129129+ - [verbosity=[_;_]] or more: Debug. *)
129130130131val enable_tracing : verbosity:bool list -> bool
131132(** [enable_tracing ~verbosity] returns [true] if verbosity >= 3 (i.e., [-vvv]).
···169170 This follows patterns from other languages:
170171 - Rust: [RUST_LOG=warn,mycrate::module=debug]
171172 - Go (IPFS): [GOLOG_LOG_LEVEL=error,subsystem=debug]
172172- - Node.js: [DEBUG=prefix:*] or [LOG_LEVEL=debug] *)
173173+ - Node.js: [DEBUG=prefix:*] or [LOG_LEVEL=debug]. *)