package b0
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha512=e9aa779e66c08fc763019f16d4706f465d16c05d6400b58fbd0313317ef33ddea51952e2b058db28e65f7ddb7012f328c8bf02d8f1da17bb543348541a2587f0
doc/b0.std/B0_std/Log/index.html
Module B0_std.Log
Source
Program log.
Examples:
let () = Log.warn (fun m -> m "The queue is full (%d elements)" count)
let () = Log.err (fun m -> m "The request timed out after %a" Mtime.pp dur)
let items =
Log.time (fun v m -> m "Purged, %d items remaining" (List.length v)) @@
(fun () -> purge items)
See also the cookbook on logging.
TODO. Think about implicit locations.
Reporting levels
type level =
| Quiet
(*Do not report anything.
*)| Stdout
(*Outputs to the
*)stdout
of the program. Using this allows the output to be silenced when thelevel
is set toQuiet
, which may be desirable, or not.| Stderr
(*Outputs to the
*)stderr
of the program. Using this allows the output to be silenced when thelevel
is set toQuiet
, which may be desirable, or not.| Error
(*For error conditions that prevent the program from running correctly.
*)| Warning
(*For suspicious conditions that do not prevent the program from running normally but may eventually lead to an error condition.
*)| Info
(*For conditions that allow the program user to get a better understanding of what the program is doing.
*)| Debug
(*For conditions that allow the program developer to get a better understanding of what the program is doing.
*)
The type for reporting levels.
level ()
is the reporting level. The initial level is set to Warning
.
set_level l
sets the reporting level to l
.
Use B0_std_cli.set_log_level
to set this from the command line.
level_of_string s
parses a level from s
according to the representation of level_to_string
.
Log functions
The type for client specified message formatting functions.
A message formatting function is called with a message construction function m
. The message formatting function must call the given message construction function with an optional header, a format string and its arguments to define the message contents. Here are a few examples of message formatting functions:
(fun m -> m "%d messages to send" n)
(fun m -> m ~header:"emails" "%d messages to send" n)
The interpretation of the optional header
argument of m
is up to the reporter but None
should automatically output a header that depend on the log level and Some ""
should not output any header, leaving full control of the log formatting to the client.
msg level (fun m -> m fmt …)
logs with level level
a message formatted with fmt
. For the semantics of levels see level
.
kmsg k level (fun m -> m fmt …)
logs with level level
a message formatted with fmt
and continues with k
.
Logging result
errors
if_error ~level ~use r
is:
v
, ifr
isOk v
use
ande
is logged usingFmt.lines
withlevel
(defaults toError
), ifr
isError e
.
val if_error_pp :
'b Fmt.t ->
?level:level ->
?header:string ->
use:'a ->
('a, 'b) result ->
'a
if_error_pp ~level pp ~use r
is
v
, ifr
isOk v
.use
ande
is logged withlevel
(defaults toError
) usingpp
, ifr
isError e
.
val if_error_pp' :
'b Fmt.t ->
?level:level ->
?header:string ->
use:'a ->
('a, 'b) result ->
('a, 'b) result
if_error_pp'
is if_error_pp'
wrapped by Result.ok
Logging timings
val time :
?level:level ->
('a -> (('b, Format.formatter, unit, 'a) format4 -> 'b) -> 'a) ->
(unit -> 'a) ->
'a
time ~level m f
logs m
with level level
(defaults to Info
) and the time f ()
took as the message header with Mtime.Span.pp
.
Note. The reporting level
is determined after f
has been called. This means f
can change it to affect the report. See for example b0_std_cookbook.logging_main
Logging values
value pp v
reports v
on level
(defaults to Stderr
) with pp
if id
is specified this is of the form "%s: %a" and returns v
Monitoring
err_count ()
is the number of messages logged with level Error
.
warn_count ()
is the number of messages logged with level Warning
.